forked from max-mapper/datop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.js
133 lines (109 loc) · 2.91 KB
/
screen.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var blessed = require('blessed')
var through = require('through2')
var xtend = require('xtend')
var dataChart = require('./chart.js')
module.exports = Screen
function Screen(host, theme) {
if (!(this instanceof Screen)) return new Screen(host, theme)
var self = this
var opts = {}
if (process.env.DEBUG) opts.log = './datop.log'
var program = blessed.program(opts)
process.on('SIGINT', function() {
self.kill()
})
this.program = program
this.host = host
this.screen = blessed.screen()
this.headerText = ' {bold}datop{/bold} - ' + host
var header = blessed.text({
top: 'top',
left: 'left',
width: this.headerText.length + 10,
height: '1',
fg: theme.title.fg,
content: this.headerText,
tags: true
})
this.screen.append(header)
this.screen.on('resize', function() {
for (var i = 0; i < self.renderList.length; i++) {
var item = self.renderList[i]
if (item.chart) {
dataChart.resize(item.chart, item.box)
updateHeader(item.chart)
}
}
})
this.theme = theme
this.renderList = []
setInterval(draw, 1000)
function draw() {
if (self.renderList.length === 0) return
var updatedHeader = self.headerText
for (var i = 0; i < self.renderList.length; i++) {
var item = self.renderList[i]
item.render()
if (item.chart) updateHeader(item.chart)
}
header.content = updatedHeader
self.screen.render()
}
function updateHeader(chart) {
var min = ~~(chart.width / 60)
var sec = chart.width % 60
self.headerText = ' {bold}datop{/bold} - ' + self.host + ' - showing ' + min + 'm' + sec + 's'
}
}
Screen.prototype.kill = function() {
this.program.clear()
this.program.disableMouse()
this.program.showCursor()
this.program.normalBuffer()
this.process.exit(0)
}
Screen.prototype.createBox = function(opts) {
var theme = this.theme
var screen = this.screen
if (!opts) opts = {}
var defaults = {
top: 1,
left: 'left',
width: '100%',
height: '99%',
content: '',
fg: theme.chart.fg,
tags: true,
border: theme.chart.border
}
var box = blessed.box(xtend(defaults, opts))
screen.append(box)
screen.render()
if (opts.title) box.setLabel(opts.title)
return box
}
Screen.prototype.createChart = function(opts) {
var self = this
var box = this.createBox(opts)
var chart = dataChart.create(box)
var position = 0
var stream = through.obj(
function write(obj, enc, next) {
chart.ready = true
chart.value = obj
next()
},
function end() {
chart.ready = false
}
)
stream.chart = chart
stream.box = box
stream.render = function() {
if (process.env.DEBUG) self.program.log([chart.min, chart.max, chart.average, chart.values[chart.values.length - 1]])
position++
box.setContent(dataChart.draw(chart, position))
}
this.renderList.push(stream)
return stream
}