« get me outta code hell

A long-due cleanup + examples + things - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/ui
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-07-03 18:59:57 -0300
committerliam4 <towerofnix@gmail.com>2017-07-03 19:00:01 -0300
commit769413468e88acba1a180baa0113139d929a3b9f (patch)
treef29af36826077178259b7bcc8bf9927cebfe71e3 /ui
parent489e4d0c78d5f393729cda0e1f6ac9a0a1237b4a (diff)
A long-due cleanup + examples + things
..Obviously this breaks old things (particularly, see changes in
FocusElement).
Diffstat (limited to 'ui')
-rw-r--r--ui/DisplayElement.js2
-rw-r--r--ui/Label.js2
-rw-r--r--ui/Pane.js4
-rw-r--r--ui/Root.js104
-rw-r--r--ui/Sprite.js2
-rw-r--r--ui/form/Button.js6
-rw-r--r--ui/form/CancelDialog.js4
-rw-r--r--ui/form/ConfirmDialog.js4
-rw-r--r--ui/form/FocusBox.js6
-rw-r--r--ui/form/FocusElement.js12
-rw-r--r--ui/form/Form.js4
-rw-r--r--ui/form/HorizontalForm.js4
-rw-r--r--ui/form/TextInput.js6
13 files changed, 34 insertions, 126 deletions
diff --git a/ui/DisplayElement.js b/ui/DisplayElement.js
index c8352ed..3a97ed9 100644
--- a/ui/DisplayElement.js
+++ b/ui/DisplayElement.js
@@ -1,5 +1,5 @@
 const EventEmitter = require('events')
-const exception = require('../exception')
+const exception = require('../util/exception')
 
 module.exports = class DisplayElement extends EventEmitter {
   // A general class that handles dealing with screen coordinates, the tree
diff --git a/ui/Label.js b/ui/Label.js
index 60ece15..850edc0 100644
--- a/ui/Label.js
+++ b/ui/Label.js
@@ -1,4 +1,4 @@
-const ansi = require('../ansi')
+const ansi = require('../util/ansi')
 
 const DisplayElement = require('./DisplayElement')
 
diff --git a/ui/Pane.js b/ui/Pane.js
index b4fad57..4e08c55 100644
--- a/ui/Pane.js
+++ b/ui/Pane.js
@@ -1,5 +1,5 @@
-const ansi = require('../ansi')
-const unic = require('../unichars')
+const ansi = require('../util/ansi')
+const unic = require('../util/unichars')
 
 const DisplayElement = require('./DisplayElement')
 
diff --git a/ui/Root.js b/ui/Root.js
index 06e3ecd..b170f99 100644
--- a/ui/Root.js
+++ b/ui/Root.js
@@ -1,6 +1,6 @@
 const iac = require('iac')
 
-const ansi = require('../ansi')
+const ansi = require('../util/ansi')
 
 const DisplayElement = require('./DisplayElement')
 
@@ -10,88 +10,23 @@ module.exports = class Root extends DisplayElement {
   // An element to be used as the root of a UI. Handles lots of UI and
   // socket stuff.
 
-  constructor(socket) {
+  constructor(interfacer) {
     super()
 
-    this.socket = socket
-    this.initTelnetOptions()
+    this.interfacer = interfacer
 
     this.selected = null
 
     this.cursorBlinkOffset = Date.now()
 
-    socket.on('data', buf => this.handleData(buf))
+    interfacer.on('inputData', buf => this.handleData(buf))
   }
 
-  initTelnetOptions() {
-    // Initializes various socket options, using telnet magic.
-
-    // Disables linemode.
-    this.socket.write(Buffer.from([
-      255, 253, 34,  // IAC DO LINEMODE
-      255, 250, 34, 1, 0, 255, 240,  // IAC SB LINEMODE MODE 0 IAC SE
-      255, 251, 1    // IAC WILL ECHO
-    ]))
-
-    // Will SGA. Helps with putty apparently.
-    this.socket.write(Buffer.from([
-      255, 251, 3  // IAC WILL SGA
-    ]))
-
-    this.socket.write(ansi.hideCursor())
-  }
-
-  cleanTelnetOptions() {
-    // Resets the telnet options and magic set in initTelnetOptions.
-
-    this.socket.write(ansi.resetAttributes())
-    this.socket.write(ansi.showCursor())
-  }
-
-  requestTelnetWindowSize() {
-    // See RFC #1073 - Telnet Window Size Option
-
-    return new Promise((res, rej) => {
-      this.socket.write(Buffer.from([
-        255, 253, 31  // IAC WILL NAWS
-      ]))
-
-      this.once('telnetsub', function until(sub) {
-        if (sub[0] !== 31) { // NAWS
-          this.once('telnetsub', until)
-        } else {
-          res({lines: sub[4], cols: sub[2]})
-        }
-      })
-    })
+  render() {
+    this.renderTo(this.interfacer)
   }
 
   handleData(buffer) {
-    if (buffer[0] === 255) {
-      // Telnet IAC (Is A Command) - ignore
-
-      // Split the data into multiple IAC commands if more than one IAC was
-      // sent.
-      const values = Array.from(buffer.values())
-      const commands = []
-      const curCmd = [255]
-      for (let value of values) {
-        if (value === 255) { // IAC
-          commands.push(Array.from(curCmd))
-          curCmd.splice(1, curCmd.length)
-          continue
-        }
-        curCmd.push(value)
-      }
-      commands.push(curCmd)
-
-      for (let command of commands) {
-        this.interpretTelnetCommand(command)
-      }
-
-      return
-    }
-
     if (this.selected) {
       const els = this.selected.directAncestors.concat([this.selected])
       for (let el of els) {
@@ -106,29 +41,6 @@ module.exports = class Root extends DisplayElement {
     }
   }
 
-  interpretTelnetCommand(command) {
-    if (command[0] !== 255) { // IAC
-      // First byte isn't IAC, which means this isn't a command, so do
-      // nothing.
-      return
-    }
-
-    if (command[1] === 251) { // WILL
-      // Do nothing because I'm lazy
-      const willWhat = command[2]
-      //console.log('IAC WILL ' + willWhat)
-    }
-
-    if (command[1] === 250) { // SB
-      this.telnetSub = command.slice(2)
-    }
-
-    if (command[1] === 240) { // SE
-      this.emit('telnetsub', this.telnetSub)
-      this.telnetSub = null
-    }
-  }
-
   drawTo(writable) {
     writable.write(ansi.moveCursor(0, 0))
     writable.write(' '.repeat(this.w * this.h))
@@ -164,11 +76,11 @@ module.exports = class Root extends DisplayElement {
     // element, if there is one.
 
     if (this.selected) {
-      this.selected.unfocus()
+      this.selected.unfocused()
     }
 
     this.selected = el
-    this.selected.focus()
+    this.selected.focused()
 
     this.cursorMoved()
   }
diff --git a/ui/Sprite.js b/ui/Sprite.js
index cd6528c..62b0172 100644
--- a/ui/Sprite.js
+++ b/ui/Sprite.js
@@ -1,4 +1,4 @@
-const ansi = require('../ansi')
+const ansi = require('../util/ansi')
 
 const DisplayElement = require('./DisplayElement')
 
diff --git a/ui/form/Button.js b/ui/form/Button.js
index 9a3d2f7..86347a0 100644
--- a/ui/form/Button.js
+++ b/ui/form/Button.js
@@ -1,5 +1,5 @@
-const ansi = require('../../ansi')
-const telc = require('../../telchars')
+const ansi = require('../../util/ansi')
+const telc = require('../../util/telchars')
 
 const FocusElement = require('./FocusElement')
 
@@ -29,7 +29,7 @@ module.exports = class ButtonInput extends FocusElement {
   }
 
   drawTo(writable) {
-    if (this.isSelected) {
+    if (this.isFocused) {
       writable.write(ansi.invert())
     }
 
diff --git a/ui/form/CancelDialog.js b/ui/form/CancelDialog.js
index ba9faf8..c5eb7d3 100644
--- a/ui/form/CancelDialog.js
+++ b/ui/form/CancelDialog.js
@@ -1,4 +1,4 @@
-const telc = require('../../telchars')
+const telc = require('../../util/telchars')
 
 const FocusElement = require('./FocusElement')
 
@@ -47,7 +47,7 @@ module.exports = class ConfirmDialog extends FocusElement {
     this.cancelBtn.y = this.pane.contentH - 2
   }
 
-  focus() {
+  focused() {
     this.root.select(this.cancelBtn)
   }
 
diff --git a/ui/form/ConfirmDialog.js b/ui/form/ConfirmDialog.js
index 614dede..3614cf9 100644
--- a/ui/form/ConfirmDialog.js
+++ b/ui/form/ConfirmDialog.js
@@ -1,4 +1,4 @@
-const telc = require('../../telchars')
+const telc = require('../../util/telchars')
 
 const FocusElement = require('./FocusElement')
 
@@ -59,7 +59,7 @@ module.exports = class ConfirmDialog extends FocusElement {
     this.cancelBtn.y = this.form.contentH - 2
   }
 
-  focus() {
+  focused() {
     this.root.select(this.form)
   }
 
diff --git a/ui/form/FocusBox.js b/ui/form/FocusBox.js
index c259f23..51e961b 100644
--- a/ui/form/FocusBox.js
+++ b/ui/form/FocusBox.js
@@ -1,4 +1,4 @@
-const ansi = require('../../ansi')
+const ansi = require('../../util/ansi')
 
 const FocusElement = require('./FocusElement')
 
@@ -19,13 +19,13 @@ module.exports = class FocusBox extends FocusElement {
   }
 
   drawTo(writable) {
-    if (this.isSelected) {
+    if (this.isFocused) {
       writable.write(ansi.invert())
     }
   }
 
   didRenderTo(writable) {
-    if (this.isSelected) {
+    if (this.isFocused) {
       writable.write(ansi.resetAttributes())
     }
   }
diff --git a/ui/form/FocusElement.js b/ui/form/FocusElement.js
index 25a0693..5967e26 100644
--- a/ui/form/FocusElement.js
+++ b/ui/form/FocusElement.js
@@ -9,19 +9,19 @@ module.exports = class FocusElement extends DisplayElement {
     this.cursorX = 0
     this.cursorY = 0
 
-    this.isSelected = false
+    this.isFocused = false
   }
 
-  focus(socket) {
-    // Do something with socket. Should be overridden in subclasses.
+  focused() {
+    // Should be overridden in subclasses.
 
-    this.isSelected = true
+    this.isFocused = true
   }
 
-  unfocus() {
+  unfocused() {
     // Should be overridden in subclasses.
 
-    this.isSelected = false
+    this.isFocused = false
   }
 
   keyPressed(keyBuf) {
diff --git a/ui/form/Form.js b/ui/form/Form.js
index 49fa075..9274da4 100644
--- a/ui/form/Form.js
+++ b/ui/form/Form.js
@@ -1,4 +1,4 @@
-const telc = require('../../telchars')
+const telc = require('../../util/telchars')
 
 const FocusElement = require('./FocusElement')
 
@@ -45,7 +45,7 @@ module.exports = class Form extends FocusElement {
     }
   }
   
-  focus() {
+  focused() {
     this.root.select(this.inputs[this.curIndex])
   }
 }
diff --git a/ui/form/HorizontalForm.js b/ui/form/HorizontalForm.js
deleted file mode 100644
index 141bb17..0000000
--- a/ui/form/HorizontalForm.js
+++ /dev/null
@@ -1,4 +0,0 @@
-const Form = require('./DisplayElement')
-
-module.exports = class HorizontalBox extends Box {
-}
diff --git a/ui/form/TextInput.js b/ui/form/TextInput.js
index d09480f..fc59cbb 100644
--- a/ui/form/TextInput.js
+++ b/ui/form/TextInput.js
@@ -1,6 +1,6 @@
-const ansi = require('../../ansi')
-const unic = require('../../unichars')
-const telc = require('../../telchars')
+const ansi = require('../../util/ansi')
+const unic = require('../../util/unichars')
+const telc = require('../../util/telchars')
 
 const FocusElement = require('./FocusElement')