« get me outta code hell

list-scroll-form.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/list-scroll-form.js
blob: cb7fa2700a3f0d58dc56bb2024a7d0af92ca8eeb (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
const ansi = require('../util/ansi')
const Root = require('../ui/Root')
const CommandLineInterfacer = require('../util/CommandLineInterfacer')
const ListScrollForm = require('../ui/form/ListScrollForm')
const Button = require('../ui/form/Button')

const interfacer = new CommandLineInterfacer()

interfacer.getScreenSize().then(size => {
  const root = new Root(interfacer)
  root.w = size.width
  root.h = size.height

  const list = new ListScrollForm()
  root.addChild(list)

  list.x = 2
  list.y = 2
  list.w = root.contentW - 4
  list.h = root.contentH - 4

  for (let item of ['Foo', 'Bar', 'Baz']) {
    const button = new Button(item)
    list.addInput(button)

    button.on('pressed', () => {
      process.stdout.write(ansi.cleanCursor())
      process.stdout.write(ansi.clearScreen())
      console.log(item)
      process.exit(0)
    })

    button.fixLayout()
  }

  list.fixLayout()

  root.select(list)

  setInterval(() => root.render(), 100)
}).catch(error => {
  console.error(error)
  process.exit(1)
})