diff --git a/config.js b/config.js index e7ad30b..c562a99 100644 --- a/config.js +++ b/config.js @@ -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, } diff --git a/fx.js b/fx.js index 66ce55f..9487b73 100644 --- a/fx.js +++ b/fx.js @@ -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' diff --git a/print.js b/print.js index b9a3ea4..771ed61 100644 --- a/print.js +++ b/print.js @@ -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 => '' + s + '') .split(//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()