-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.js
More file actions
72 lines (64 loc) · 1.55 KB
/
display.js
File metadata and controls
72 lines (64 loc) · 1.55 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const blessed = require('blessed')
class Display {
constructor () {
this.userInputCallback = () => {}
this.screen = blessed.screen({
ignoreLocked: ['C-c']
})
this.body = blessed.box({
top: 0,
left: 0,
height: '100%-1',
width: '100%',
alwaysScroll: true,
scrollable: true,
scrollbar: {
ch: ' ',
bg: 'white'
}
})
this.inputLineMarker = blessed.box({
bottom: 0,
left: 0,
height: 1,
width: 2
})
this.inputBar = blessed.textbox({
bottom: 0,
left: 2,
height: 1,
width: '100%-2',
keys: true,
mouse: true,
inputOnFocus: true
})
// Add body to blessed screen
this.screen.append(this.body)
// Add input marker and bar to blessed screen
this.screen.append(this.inputLineMarker)
this.screen.append(this.inputBar)
// Close the program on ctrl+c
this.screen.key(['C-c'], (ch, key) => process.exit(0))
// Handle user input
this.inputBar.on('submit', (text) => {
this.log(`> ${text}`)
this.userInputCallback(text)
this.inputBar.clearValue()
this.inputBar.focus()
this.inputBar.render()
})
// Focus input
this.inputBar.focus()
// Set the marker value
this.inputLineMarker.setContent('> ')
// Show our screen right now
this.screen.render()
this.body.pushLine(this)
}
// Logging function. Use this instead of console.log
log (text) {
this.body.pushLine(text)
this.screen.render()
}
}
module.exports = Display