Make current highlight path different

This commit is contained in:
Anton Medvedev 2018-12-15 01:13:39 +07:00
parent 9c1a7e8ae2
commit 6a7a628e03
3 changed files with 28 additions and 23 deletions

View File

@ -10,15 +10,16 @@ const list = {
}
module.exports = {
space: global.FX_STYLE_SPACE || 2,
null: global.FX_STYLE_NULL || chalk.grey.bold,
number: global.FX_STYLE_NUMBER || chalk.cyan.bold,
boolean: global.FX_STYLE_BOOLEAN || chalk.yellow.bold,
string: global.FX_STYLE_STRING || chalk.green.bold,
key: global.FX_STYLE_KEY || chalk.blue.bold,
bracket: global.FX_STYLE_BRACKET || noop,
comma: global.FX_STYLE_COMMA || noop,
colon: global.FX_STYLE_COLON || noop,
highlight: global.FX_STYLE_HIGHLIGHT || chalk.black.bgYellow,
list: global.FX_STYLE_LIST || list,
space: global.FX_STYLE_SPACE || 2,
null: global.FX_STYLE_NULL || chalk.grey.bold,
number: global.FX_STYLE_NUMBER || chalk.cyan.bold,
boolean: global.FX_STYLE_BOOLEAN || chalk.yellow.bold,
string: global.FX_STYLE_STRING || chalk.green.bold,
key: global.FX_STYLE_KEY || chalk.blue.bold,
bracket: global.FX_STYLE_BRACKET || noop,
comma: global.FX_STYLE_COMMA || noop,
colon: global.FX_STYLE_COLON || noop,
list: global.FX_STYLE_LIST || list,
highlight: global.FX_STYLE_HIGHLIGHT || chalk.black.bgYellow,
highlightCurrent: global.FX_STYLE_HIGHLIGHT_CURRENT || chalk.inverse,
}

11
fx.js
View File

@ -24,6 +24,7 @@ module.exports = function start(filename, source) {
// Current search regexp and generator.
let highlight = null
let findGen = null
let currentPath = null
const ttyFd = fs.openSync('/dev/tty', 'r+')
const program = blessed.program({
@ -178,6 +179,7 @@ module.exports = function start(filename, source) {
findNext()
} else {
findGen = null
currentPath = null
}
search.hide()
@ -192,6 +194,7 @@ module.exports = function start(filename, source) {
search.on('cancel', function () {
highlight = null
currentPath = null
search.hide()
search.setValue('')
@ -441,14 +444,14 @@ module.exports = function start(filename, source) {
}
const {value: path, done} = findGen.next()
if (!done) {
let value = ''
currentPath = ''
for (let p of path) {
expanded.add(value += p)
expanded.add(currentPath += p)
}
render()
for (let [k, v] of index) {
if (v === value) {
if (v === currentPath) {
const y = box.getScreenNumber(k)
box.scrollTo(y)
screen.render()
@ -459,7 +462,7 @@ module.exports = function start(filename, source) {
function render() {
let content
[content, index] = print(json, {expanded, highlight})
[content, index] = print(json, {expanded, highlight, currentPath})
if (typeof content === 'undefined') {
content = 'undefined'

View File

@ -3,18 +3,19 @@ const indent = require('indent-string')
const config = require('./config')
function print(input, options = {}) {
const {expanded, highlight} = options
const {expanded, highlight, currentPath} = options
const index = new Map()
let row = 0
function format(text, style) {
function format(text, style, path) {
if (!highlight) {
return style(text)
}
const highlightStyle = (currentPath === path) ? config.highlightCurrent : config.highlight
return text
.replace(highlight, s => '<fx>' + s + '<fx>')
.split(/<fx>/g)
.map((s, i) => i % 2 !== 0 ? config.highlight(s) : style(s))
.map((s, i) => i % 2 !== 0 ? highlightStyle(s) : style(s))
.join('')
}
@ -31,20 +32,20 @@ function print(input, options = {}) {
}
if (v === null) {
return format('null', config.null)
return format('null', config.null, path)
}
if (typeof v === 'number' && Number.isFinite(v)) {
return format(v.toString(), config.number)
return format(v.toString(), config.number, path)
}
if (typeof v === 'boolean') {
return format(v.toString(), config.boolean)
return format(v.toString(), config.boolean, path)
}
if (typeof v === 'string') {
return format(JSON.stringify(v), config.string)
return format(JSON.stringify(v), config.string, path)
}
if (Array.isArray(v)) {
@ -82,7 +83,7 @@ function print(input, options = {}) {
output += eol()
let i = 0
for (let [key, value] of entries) {
const part = format(JSON.stringify(key), config.key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
const part = format(JSON.stringify(key), config.key, path + '.' + key) + config.colon(':') + ' ' + doPrint(value, path + '.' + key)
output += indent(part, config.space)
output += i++ < len - 1 ? config.comma(',') : ''
output += eol()