fx/fx.js

371 lines
7.8 KiB
JavaScript
Raw Normal View History

'use strict'
2018-11-02 18:24:56 +00:00
const fs = require('fs')
const tty = require('tty')
const blessed = require('@medv/blessed')
2018-11-02 18:24:56 +00:00
const stringWidth = require('string-width')
const reduce = require('./reduce')
const print = require('./print')
2018-12-02 16:02:31 +00:00
const config = require('./config')
module.exports = function start(filename, source) {
let json = source
let index = new Map()
const expanded = new Set()
expanded.add('') // Root of JSON
2018-11-02 18:24:56 +00:00
const ttyFd = fs.openSync('/dev/tty', 'r+')
const program = blessed.program({
input: tty.ReadStream(ttyFd),
output: tty.WriteStream(ttyFd),
})
const screen = blessed.screen({
program: program,
smartCSR: true,
fullUnicode: true,
})
const box = blessed.box({
parent: screen,
2018-11-08 02:48:06 +00:00
tags: false,
2018-11-02 18:24:56 +00:00
left: 0,
top: 0,
width: '100%',
height: '100%',
mouse: true,
2018-11-02 18:24:56 +00:00
keys: true,
vi: true,
ignoreArrows: true,
2018-11-02 18:24:56 +00:00
alwaysScroll: true,
scrollable: true,
})
const input = blessed.textbox({
parent: screen,
bottom: 0,
left: 0,
height: 1,
width: '100%',
})
2018-11-02 18:24:56 +00:00
2018-12-02 12:43:11 +00:00
const autocomplete = blessed.list({
parent: screen,
width: 6,
height: 7,
left: 1,
bottom: 1,
2018-12-02 16:02:31 +00:00
style: config.list,
2018-12-02 12:43:11 +00:00
})
screen.title = filename
2018-12-02 12:43:11 +00:00
box.focus()
input.hide()
2018-12-02 12:43:11 +00:00
autocomplete.hide()
2018-12-02 12:43:11 +00:00
screen.key(['escape', 'q', 'C-c'], function () {
2018-12-01 05:17:38 +00:00
program.disableMouse() // If exit program immediately, stdin may still receive
setTimeout(() => process.exit(0), 10) // mouse events which will be printed in stdout.
2018-11-02 18:24:56 +00:00
})
2018-12-01 05:17:38 +00:00
screen.on('resize', function () {
render()
})
2018-12-02 12:43:11 +00:00
input.on('submit', function () {
if (autocomplete.hidden) {
apply()
} else {
// Autocomplete selected
let code = input.getValue()
let replace = autocomplete.getSelected()
2018-12-02 13:25:57 +00:00
if (/^[a-z]\w+$/.test(replace)) {
replace = '.' + replace
} else {
2018-12-02 13:25:57 +00:00
replace = `["${replace}"]`
}
code = code.replace(/\.\w*$/, replace)
2018-12-02 12:43:11 +00:00
input.setValue(code)
autocomplete.hide()
update(code)
// Keep editing code
input.readInput()
}
})
input.on('cancel', function () {
if (autocomplete.hidden) {
apply()
} else {
2018-12-02 12:43:11 +00:00
// Autocomplete not selected
autocomplete.hide()
screen.render()
// Keep editing code
input.readInput()
}
2018-11-02 18:24:56 +00:00
})
input.on('update', function (code) {
2018-12-02 12:43:11 +00:00
update(code)
complete(code)
})
input.key('up', function () {
if (!autocomplete.hidden) {
autocomplete.up()
screen.render()
}
2018-12-02 12:43:11 +00:00
})
input.key('down', function () {
if (!autocomplete.hidden) {
autocomplete.down()
screen.render()
}
})
input.key('C-u', function () {
input.setValue('')
update('')
render()
})
2018-11-02 18:24:56 +00:00
2018-12-02 12:43:11 +00:00
input.key('C-w', function () {
let code = input.getValue()
code = code.replace(/[\.\[][^\.\[]*$/, '')
2018-12-02 12:43:11 +00:00
input.setValue(code)
update(code)
render()
})
2018-12-01 05:17:38 +00:00
box.key('.', function () {
box.height = '100%-1'
input.show()
2018-12-01 05:17:38 +00:00
if (input.getValue() === '') {
input.setValue('.')
2018-12-02 12:43:11 +00:00
complete('.')
2018-12-01 05:17:38 +00:00
}
input.readInput()
render()
})
2018-11-02 18:24:56 +00:00
box.key('e', function () {
2018-12-02 12:43:11 +00:00
expanded.clear()
walk(json, path => expanded.size < 1000 && expanded.add(path))
2018-11-02 18:24:56 +00:00
render()
})
2018-11-02 18:24:56 +00:00
box.key('S-e', function () {
expanded.clear()
expanded.add('')
render()
})
2018-12-04 19:34:54 +00:00
box.key(['up', 'k'], function () {
program.showCursor()
2018-12-01 17:29:44 +00:00
const [n] = getLine(program.y)
const rest = [...index.keys()].filter(i => i < n)
if (rest.length > 0) {
const next = Math.max(...rest)
2018-12-01 17:29:44 +00:00
let y = box.getScreenNumber(next) - box.childBase
if (y <= 0) {
box.scroll(-1)
screen.render()
2018-12-01 17:29:44 +00:00
y = 0
2018-11-02 18:24:56 +00:00
}
2018-12-01 17:29:44 +00:00
const line = box.getScreenLine(y + box.childBase)
program.cursorPos(y, line.search(/\S/))
2018-11-02 18:24:56 +00:00
}
})
2018-11-02 18:24:56 +00:00
2018-12-04 19:34:54 +00:00
box.key(['down', 'j'], function () {
program.showCursor()
2018-12-01 17:29:44 +00:00
const [n] = getLine(program.y)
const rest = [...index.keys()].filter(i => i > n)
if (rest.length > 0) {
const next = Math.min(...rest)
2018-12-01 17:29:44 +00:00
let y = box.getScreenNumber(next) - box.childBase
if (y >= box.height) {
box.scroll(1)
screen.render()
2018-12-01 17:29:44 +00:00
y = box.height - 1
2018-11-02 18:24:56 +00:00
}
2018-12-01 17:29:44 +00:00
const line = box.getScreenLine(y + box.childBase)
program.cursorPos(y, line.search(/\S/))
2018-11-02 18:24:56 +00:00
}
})
2018-11-02 18:24:56 +00:00
2018-12-04 19:34:54 +00:00
box.key(['right', 'l'], function () {
2018-12-01 17:29:44 +00:00
const [n, line] = getLine(program.y)
program.showCursor()
2018-12-01 17:29:44 +00:00
program.cursorPos(program.y, line.search(/\S/))
const path = index.get(n)
if (!expanded.has(path)) {
expanded.add(path)
render()
2018-11-02 18:24:56 +00:00
}
})
2018-11-02 18:24:56 +00:00
2018-12-04 19:34:54 +00:00
box.key(['left', 'h'], function () {
2018-12-01 17:29:44 +00:00
const [n, line] = getLine(program.y)
program.showCursor()
2018-12-01 17:29:44 +00:00
program.cursorPos(program.y, line.search(/\S/))
const path = index.get(n)
if (expanded.has(path)) {
expanded.delete(path)
render()
2018-11-02 18:24:56 +00:00
}
})
2018-11-02 18:24:56 +00:00
box.on('click', function (mouse) {
2018-12-01 17:29:44 +00:00
const [n, line] = getLine(mouse.y)
2018-11-02 18:24:56 +00:00
if (mouse.x >= stringWidth(line)) {
return
}
2018-12-01 17:29:44 +00:00
program.hideCursor()
program.cursorPos(mouse.y, line.search(/\S/))
2018-12-02 12:43:11 +00:00
autocomplete.hide()
2018-12-01 17:29:44 +00:00
const path = index.get(n)
2018-11-02 18:24:56 +00:00
if (expanded.has(path)) {
expanded.delete(path)
} else {
expanded.add(path)
}
render()
})
2018-12-01 17:29:44 +00:00
function getLine(y) {
const dy = box.childBase + y
const n = box.getNumber(dy)
const line = box.getScreenLine(dy)
return [n, line]
}
2018-12-02 12:43:11 +00:00
function apply() {
const code = input.getValue()
if (code && code.length !== 0) {
try {
json = reduce(source, code)
} catch (e) {
// pass
}
} else {
box.height = '100%'
input.hide()
json = source
}
box.focus()
program.cursorPos(0, 0)
render()
}
function complete(inputCode) {
const match = inputCode.match(/\.(\w*)$/)
const code = /^\.\w*$/.test(inputCode) ? '.' : inputCode.replace(/\.\w*$/, '')
let json
try {
json = reduce(source, code)
} catch (e) {
}
if (match) {
if (typeof json === 'object' && json.constructor === Object) {
const keys = Object.keys(json).filter(key => key.startsWith(match[1]))
2018-12-02 16:16:25 +00:00
// Hide if there is nothing to show or
// don't show if there is complete match.
if (keys.length === 0 || (keys.length === 1 && keys[0] === match[1])) {
2018-12-02 12:43:11 +00:00
autocomplete.hide()
return
}
autocomplete.width = Math.max(...keys.map(key => key.length)) + 1
autocomplete.height = Math.min(7, keys.length)
autocomplete.left = Math.min(
screen.width - autocomplete.width,
code.length === 1 ? 1 : code.length + 1
)
let selectFirst = autocomplete.items.length !== keys.length
autocomplete.setItems(keys)
if (selectFirst) {
autocomplete.select(autocomplete.items.length - 1)
}
if (autocomplete.hidden) {
autocomplete.show()
}
} else {
autocomplete.clearItems()
autocomplete.hide()
}
}
}
function update(code) {
if (code && code.length !== 0) {
try {
const pretender = reduce(source, code)
if (typeof pretender !== 'undefined' && typeof pretender !== 'function') {
json = pretender
}
} catch (e) {
// pass
}
}
if (code === '') {
json = source
}
render()
}
2018-11-02 18:24:56 +00:00
function render() {
let content
2018-12-01 17:29:44 +00:00
[content, index] = print(json, {expanded})
if (typeof content === 'undefined') {
content = 'undefined'
}
2018-11-02 18:24:56 +00:00
box.setContent(content)
screen.render()
}
render()
}
function walk(v, cb, path = '') {
if (!v) {
return
}
if (Array.isArray(v)) {
cb(path)
let i = 0
for (let item of v) {
walk(item, cb, path + '[' + (i++) + ']')
}
}
if (typeof v === 'object' && v.constructor === Object) {
cb(path)
let i = 0
for (let [key, value] of Object.entries(v)) {
walk(value, cb, path + '.' + key)
}
}
}