« 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: fc319a621a47780686419789f70508d4b900ab48 (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
import {Root} from 'tui-lib/ui/primitives'
import {Button, ListScrollForm} from 'tui-lib/ui/controls'

import {CommandLineInterface} from 'tui-lib/util/interfaces'
import * as ansi from 'tui-lib/util/ansi'

const clInterface = new CommandLineInterface()

clInterface.getScreenSize().then(size => {
  const root = new Root(clInterface)
  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 (const 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)
})