-
Notifications
You must be signed in to change notification settings - Fork 1
/
nb.js
executable file
·362 lines (337 loc) · 13.6 KB
/
nb.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env node
// node.js imports
import process from 'process'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
import { readFile, rm, stat } from 'fs/promises'
// third-party imports
// databases
import flatfile from 'flat-file-db' // docs: https://github.com/mafintosh/flat-file-db#api
const db = flatfile.sync(path.join(__dirname, 'database.db'))
// CLI helpers
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import updateNotifier from 'update-notifier'
const pkg = JSON.parse(await readFile(new URL('./package.json', import.meta.url)))
// TUI helpers
import AsciiTable from 'ascii-table'
import sparkly from 'sparkly'
import chart from 'chart'
import { default as windowSize } from 'window-size'
import {
defaultStream,
formatStreamName,
formatTime,
indexOrNot,
parseTimestamp,
generateDimension
} from './helpers.js'
updateNotifier({ pkg }).notify()
yargs(hideBin(process.argv))
.command({
command: 'note <stream> <value>',
description: 'record something worth remembering',
builder: yargs => {
return yargs
.positional('stream', { describe: 'the id of the stream to write to', type: 'string' })
.positional('value', { describe: 'the value to remember' })
.option('timestamp', { alias: 'ts' })
},
handler: args => {
let stream = db.get(args.stream)
if (stream === undefined) {
stream = { id: args.stream, ...defaultStream }
console.log(`stream ${args.stream} does not exist; creating it now.`)
}
if (args.timestamp) {
const parsedTimestamp = parseTimestamp(args.timestamp)
let values = [...stream.values, [ parsedTimestamp, args.value ]].sort((a, b) => a[0] - b[0])
db.put(args.stream, { ...stream, values })
console.log(`noted ${args.value} in stream ${formatStreamName(stream)} at time ${args.timestamp}.`)
} else {
db.put(args.stream, { ...stream, values: [...stream.values, [ Date.now(), args.value ]] })
console.log(`noted ${args.value} in stream ${formatStreamName(stream)}.`)
}
}
})
.command({
command: 'denote <stream> [index]',
description: 'delete a memory not worthy of rememberance',
builder: yargs => {
return yargs
},
handler: args => {
let stream = db.get(args.stream)
if (stream === undefined) throw Error(`stream ${args.stream} does not exist.`)
let values = [...stream.values]
if (args.note === undefined) {
// no note specified? delete the newest
values.splice(values.length - 1, 1)
} else if (indexOrNot(args.note)) {
// note is small? delete the one at that index
values.splice(args.note, 1)
} else {
// note is big? delete the one with that timestamp
values = values.filter((value) => value[0] !== args.note)
}
db.put(args.stream, { ...stream, values })
if (args.note === undefined) {
console.log(`deleted latest note ${values.length} in stream ${formatStreamName(stream)}.`)
} else {
console.log(`deleted note ${args.note} in stream ${formatStreamName(stream)}.`)
}
}
})
.command({
command: 'correct <stream> [index] [note]',
description: 'correct a memory',
builder: yargs => {
return yargs
.positional('stream', { describe: 'the stream containing the memory to replace' })
.positional('index', { describe: 'the memory to replace; reference it by its timestamp or index. if not specified, the newest note will be corrected' })
.option('note', { describe: 'the new note value' })
},
handler: args => {
let stream = db.get(args.stream)
if (stream === undefined)
throw Error(`stream ${args.stream} does not exist.`)
let values = [...stream.values]
let correctionIndex
if (args.index !== undefined) {
for (let i = 0; i < values.length; i++) {
if (values[i][0] === args.index || i === args.index) {
correctionIndex = i
break
}
}
} else {
correctionIndex = values.length - 1
}
if (correctionIndex === undefined)
throw Error(`stream does not contain memory with index ${index}.`)
const newValues = values
newValues[correctionIndex] = [values[correctionIndex][0], args.note]
db.put(args.stream, { ...stream, values: newValues })
if (args.index === undefined) {
console.log(`corrected latest note ${values.length} in stream ${formatStreamName(stream)}.`)
} else {
console.log(`corrected note ${args.index} in stream ${formatStreamName(stream)}.`)
}
}
})
.command({
command: 'stream',
description: 'manage streams of memory',
builder: yargs => {
return yargs
.command({
command: 'list',
description: 'list streams',
builder: yargs => {
return yargs
},
handler: args => {
const streams = db.keys()
console.log(streams.map((key) => `${key} (${db.get(key).values.length})`).sort().join('\n'))
}
})
.command({
command: 'delete <stream>',
description: 'delete a stream',
builder: yargs => {
return yargs
},
handler: args => {
const streams = db.keys()
if (db.has(args.stream)) {
db.del(args.stream)
console.log('Deleted stream.')
} else {
console.log('Stream does not exist.')
}
}
})
.command({
command: 'merge <from> <to>',
description: 'merge two streams together, deleting the "from" stream. if the "to" stream does not exist, this will effectively rename "from". no attempt will be made to dedupe entries.',
builder: yargs => {
return yargs
},
handler: args => {
if (!db.has(args.from)) throw new Error(`stream ${args.from} does not exist. specify a different "from" stream.`)
const from = db.get(args.from)
const to = db.get(args.to)
// could be time optimized to avoid the sort
let result = from.values.concat(to.values)
result = result.sort((a, b) => a[0] - b[0])
db.put(args.to, { ...to, values: result })
db.del(args.from)
console.log(`merged stream ${args.from} into stream ${args.to}.`)
}
})
.command({
command: 'show <stream>',
description: 'display memories',
builder: yargs => {
return yargs
.positional('stream', { describe: 'the id of the stream you wish to recall', type: 'string' })
// TODO: Implement timeline format
.option('format', { describe: 'the format of the output', choices: ['csv', 'table', 'chart', 'graph', 'json', 'timeline'], default: 'csv' })
.option('time-format', { describe: 'the format of timestamps in the output', choices: ['unix', 'relative', 'date'], default: 'relative' })
},
handler: args => {
const stream = db.get(args.stream)
if (stream === undefined) throw Error(`stream ${args.stream} does not exist.`)
if (args.format === 'csv') {
for(let i = 0; i < stream.values.length; i++) {
console.log(`${i}, ${formatTime(stream.values[i][0], args.timeFormat)}, ${stream.values[i][1]}`)
}
} else if (args.format === 'chart') {
console.log(chart(stream.values.map((value) => value[1]), {
width: generateDimension(args.width, 'width'),
height: generateDimension(args.height, 'height'),
dense: true
}))
} else if (args.format === 'graph') {
console.log(sparkly(stream.values.map((value) => value[1]), { minimum: 0 }))
} else if (args.format === 'table') {
const table = new AsciiTable(formatStreamName(stream))
table.setHeading('index', 'time', 'value')
table.addRowMatrix(stream.values.map((value, index) => [index, formatTime(value[0], args.timeFormat), value[1]]))
console.log(table.toString())
} else if (args.format === 'json') {
console.log(JSON.stringify({ ...stream, values: stream.values.map((value, index) => [index, ...value]) }, null, 2))
} else {
throw Error(`format ${args.format} not implemented yet.`)
}
}
})
.command({
command: 'update <stream>',
description: 'update a stream',
builder: yargs => {
return yargs
.option('name', { alias: 'n' })
},
handler: args => {
const stream = db.get(args.stream)
if (stream === undefined) throw Error(`stream ${args.stream} does not exist.`)
if (args.name) { db.put(args.stream, {... stream, name: args.name }) }
console.log(`updated stream ${formatStreamName(stream)}.`)
}
})
.command({
command: 'dashboard',
description: 'show an overview of streams',
builder: yargs => {
return yargs
},
handler: async (args) => {
const streamNames = db.keys()
let streams = streamNames.map((streamName) => {
return db.get(streamName)
})
streams = streams.sort((a, b) => (a.id).localeCompare(b.id));
const chartsPerRow = 4
const rowCount = Math.ceil(streams.length / chartsPerRow)
const charts = []
const width = Math.floor(windowSize.width / chartsPerRow)
const height = Math.floor((windowSize.height - 1) / rowCount)
const chartArgs = {
width: width,
height: height,
dense: true
}
for (let i = 0; i < streams.length; i++) {
let temp = chart(streams[i].values.map((value) => value[1]), chartArgs)
let tempArr = temp.split('\n')
tempArr = tempArr.map((line, lineIndex) => {
if (lineIndex === height - 4) {
return ` ${formatStreamName(streams[i])}`.slice(0, width).padEnd(width, ' ')
} else {
return line.slice(0, width).padEnd(width, ' ')
}
})
charts.push(tempArr)
}
for (let metaRow = 0; metaRow < rowCount; metaRow++) {
for (let literalRow = 0; literalRow < height - 2; literalRow++) {
let row = ''
for(let column = 0; column < chartsPerRow; column++) {
if (metaRow * chartsPerRow + column < streams.length) {
row += charts[metaRow * chartsPerRow + column][literalRow]
}
}
console.log(row)
}
}
}
})
.demandCommand()
.help()
},
handler: args => {
}
})
.command({
command: 'db',
description: 'manage the database',
builder: yargs => {
return yargs
.command({
command: 'merge <from> <to>',
description: 'merge two databases together, deleting the "from" database. if the "to" database does not exist, this will effectively rename "from". each entry in each stream of the "from" database will be copied to the same stream in the "to" database, given that there does not already exist a note with that timestamp in that stream of the "to" database.',
builder: yargs => {
return yargs
},
handler: async (args) => {
let from, to
try {
await stat(path.resolve(args.from))
from = flatfile.sync(path.resolve(args.from))
} catch (e) {
throw new Error(`error reading "from" database at "${args.from}":\n${e}`)
}
try {
await stat(path.resolve(args.from))
to = flatfile.sync(path.resolve(args.to))
} catch (e) {
throw new Error(`error reading "to" database at "${args.from}":\n${e}`)
}
let fromStreams = from.keys()
for(let i = 0; i < fromStreams.length; i++) {
if (!to.has(fromStreams[i])) {
to.put(fromStreams[i], { id: fromStreams[i], ...defaultStream })
console.log(`stream ${fromStreams[i]} does not exist; creating it now.`)
}
let fromValues = from.get(fromStreams[i]).values
let toValues = from.get(fromStreams[i]).values
let updates = []
for (let i = 0; i < fromValues.length; i++) {
let match = toValues.filter((note) => note[0] === fromValues[i][0])
if (!match) {
// console.log(`Did not find match for value ${i} at timestamp ${match[0]}`)
updates.push(fromValues[i])
}
}
if (updates.length) {
let existing = db.get(fromStreams[i])
existing.values = [...existing.values, ...updates].sort((a, b) => a[0] - b[0])
db.put(fromStreams[i], existing)
console.log(`Made updates to stream ${fromStreams[i]}`)
}
}
await rm(path.resolve(args.from))
console.log(`merged database ${args.from} into database ${args.to}.`)
}
})
.demandCommand()
.help()
},
handler: args => {
}
})
.demandCommand()
.help()
.argv