-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·71 lines (62 loc) · 1.55 KB
/
index.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
#!/usr/bin/env node
'use strict'
const path = require('path')
const openFile = require('./lib/open-file')
const blessed = require('blessed')
const table = require('./lib/table')
const formattingMappings = {
number: num => num + '',
string: str => str,
// TODO: Use momentJs to pretty format dates.
date: dt => (dt instanceof Date ? dt.toISOString() : dt),
boolean: b => (b ? 'true' : 'false')
}
const formatData = sheet => {
return sheet.rows.map(row => {
return row.map(cell => {
return formattingMappings[cell.type](cell.value)
})
})
}
const display = (exports.display = sheet => {
const rows = formatData(sheet)
const screen = blessed.screen({
autoPadding: false,
log:
process.env.SHEET_CLI_LOGFILE ||
path.join(process.env.HOME, '.sheetcli.log'),
smartCSR: true
})
screen._listenedMouse = true
table({
parent: screen,
rows: rows
})
screen.key('q', () => {
process.exit(0)
})
screen.render()
})
const selectSheet = (exports.selectSheet = (sheetName, sheets) => {
if (sheetName) {
const search = sheetName.toLowerCase().trim()
const found = sheets.filter(sheet => {
return (
sheet.name
.toLowerCase()
.trim()
.indexOf(search) > -1
)
})[0]
if (found) return found
}
return sheets[0]
})
const main = (exports.main = process => {
const file = process.argv[2]
const sheets = openFile(path.join(process.cwd(), file))
display(selectSheet(process.argv[3], sheets) || [])
})
if (require.main === module) {
main(process)
}