« get me outta code hell

basic-app.js « examples - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/examples/basic-app.js
blob: bf8aa41835606ab683f8c52925e39f247c4a6ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Basic app demo:
// - Structuring a basic element tree
// - Creating a pane and text input
// - Using content width/height to layout elements
// - Subclassing a FocusElement and using its focused method
// - Sending a quit-app request via Control-C
//
// This script cannot actually be used on its own; see the examples on
// interfacers (interfacer-command-line.js and inerfacer-telnet.js) for a
// working demo.

const Pane = require('../ui/Pane')
const FocusElement = require('../ui/form/FocusElement')
const TextInput = require('../ui/form/TextInput')

module.exports = class AppElement extends FocusElement {
  constructor() {
    super()

    this.pane = new Pane()
    this.addChild(this.pane)

    this.textInput = new TextInput()
    this.pane.addChild(this.textInput)
  }

  fixLayout() {
    this.w = this.parent.contentW
    this.h = this.parent.contentH

    this.pane.w = this.contentW
    this.pane.h = this.contentH

    this.textInput.x = 4
    this.textInput.y = 2
    this.textInput.w = this.pane.contentW - 8
  }

  focused() {
    this.root.select(this.textInput)
  }

  keyPressed(keyBuf) {
    if (keyBuf[0] === 0x03) { // 0x03 is Control-C
      this.emit('quitRequested')
      return 
    }

    super.keyPressed(keyBuf)
  }
}