-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (114 loc) · 3.55 KB
/
index.js
File metadata and controls
135 lines (114 loc) · 3.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
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
134
135
var neatLog = require('neat-log')
var chalk = require('chalk')
var blit = require('txt-blit')
var hg = require('hyper-graph-db')
var C = require('./src/cli-utils.js')
var Tree = require('./src/tree.js')
var path = require('path')
function HrDebug (dbs, query) {
if (!Array.isArray(dbs)) return HrDebug([dbs], query)
var self = this
this.dbs = dbs.map((db) => typeof db === 'object' ? db : {name: path.parse(db).base, path: db})
console.log(this.dbs)
this.query = query
// Init neat
this.neat = neatLog(view, {fullscreen: true})
this.neat.use(init)
// Init widgets (todo)
var bLog = new C.Box(this.neat)
// Init input
this.neat.input.on('down', () => {
if (self.state.mode === 'select') {
self.state.selected = self.state.selected + 1
if (self.state.selected > self.state.selection.length - 1) self.state.selected = 0
} else if (self.state.mode === 'browse') {
self.state.tree.down()
}
})
this.neat.input.on('up', () => {
if (self.state.mode === 'select') {
self.state.selected = self.state.selected - 1
if (self.state.selected < 0) self.state.selected = self.state.selection.length - 1
} else if (self.state.mode === 'browse') {
self.state.tree.up()
}
})
this.neat.input.on('enter', () => {
if (self.state.mode === 'select') {
browse(self.state.selection[self.state.selected])
} else if (self.state.mode === 'browse') {
self.state.tree.enter()
}
})
this.neat.input.on('keypress', (ch, key) => {
if (key && key.name === 'escape') {
self.state.mode = 'select'
}
if (key && key.name === 'l') {
self.state.showLog = !self.state.showLog
}
self.bus.emit('render')
})
function init (state, bus) {
self.bus = bus
self.state = Object.assign(state, {
mode: 'select',
selected: 0,
scroll: 0,
log: [],
showLog: false,
selection: self.dbs.map((obj) => obj.name)
})
if (self.state.selection.length === 1) {
browse(self.state.selection[self.state.selected])
}
bus.emit('render')
}
function view (state) {
var lines = []
lines.push('HyperGraph CLI Browser')
lines.push('')
if (state.mode === 'select') {
lines.push('Select a database:')
lines.push('')
state.selection.forEach((line, idx) => {
if (idx === state.selected) lines.push(chalk.red('* ' + line))
else lines.push(' ' + line)
})
}
if (state.mode === 'browse') {
lines.push('Browsing ' + state.selection[state.selected])
lines.push('')
if (state.tree) lines = lines.concat(state.tree.render())
}
var screen = []
var size = [process.stdout.columns, process.stdout.rows]
var mainBox = C.formatLines(lines, {border: false})
blit(screen, mainBox, 0, 0)
if (state.showLog) {
var box = bLog.render(state.log, {cols: size[0] - 10, rows: size[1] - 10, border: true})
blit(screen, box, 5, 5)
}
return screen.join('\n')
}
function browse (name) {
self.state.mode = 'browse'
var path = self.dbs.filter((obj) => obj.name === name)[0].path
if (self.path !== path) {
self.path = path
self.graph = hg(path, {valueEncoding: 'utf-8'})
self.graph.on('ready', () => {
self.state.tree = new Tree(self.graph, self.query)
self.state.tree.on('log', (msg) => {
log(msg)
})
self.state.tree.on('update', () => self.bus.emit('render'))
})
}
}
function log (line) {
self.state.log.push(line)
self.bus.emit('render')
}
}
module.exports = HrDebug