From 74cf26fd0d4a89d70bfa14c868f190f9c223c834 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Sun, 2 Dec 2018 00:29:44 +0700 Subject: [PATCH] Improved rendering speed --- fx.js | 90 +++++++++++++++++++++---------------------------------- print.js | 5 ++-- reduce.js | 22 +++++++------- 3 files changed, 49 insertions(+), 68 deletions(-) diff --git a/fx.js b/fx.js index c19a578..cf7e993 100644 --- a/fx.js +++ b/fx.js @@ -40,13 +40,6 @@ module.exports = function start(filename, source) { scrollable: true, }) - const test = blessed.box({ - parent: screen, - hidden: true, - tags: false, - width: '100%', - }) - const input = blessed.textbox({ parent: screen, bottom: 0, @@ -125,43 +118,48 @@ module.exports = function start(filename, source) { box.key('up', function () { program.showCursor() - const pos = box.childBase + program.y - const rest = [...index.keys()].filter(i => i < pos) + const [n] = getLine(program.y) + const rest = [...index.keys()].filter(i => i < n) if (rest.length > 0) { const next = Math.max(...rest) - const y = next - box.childBase + + let y = box.getScreenNumber(next) - box.childBase if (y <= 0) { box.scroll(-1) screen.render() + y = 0 } - const line = box.getScreenLines()[next] - const x = line.search(/\S/) - program.cursorPos(y, x) + + const line = box.getScreenLine(y + box.childBase) + program.cursorPos(y, line.search(/\S/)) } }) box.key('down', function () { program.showCursor() - const pos = box.childBase + program.y - const rest = [...index.keys()].filter(i => i > pos) + const [n] = getLine(program.y) + const rest = [...index.keys()].filter(i => i > n) if (rest.length > 0) { const next = Math.min(...rest) - const y = next - box.childBase + + let y = box.getScreenNumber(next) - box.childBase if (y >= box.height) { box.scroll(1) screen.render() + y = box.height - 1 } - const line = box.getScreenLines()[next] - const x = line.search(/\S/) - program.cursorPos(y, x) + + const line = box.getScreenLine(y + box.childBase) + program.cursorPos(y, line.search(/\S/)) } }) box.key('right', function () { + const [n, line] = getLine(program.y) program.showCursor() - const pos = box.childBase + program.y - const path = index.get(pos) + program.cursorPos(program.y, line.search(/\S/)) + const path = index.get(n) if (!expanded.has(path)) { expanded.add(path) render() @@ -169,9 +167,10 @@ module.exports = function start(filename, source) { }) box.key('left', function () { + const [n, line] = getLine(program.y) program.showCursor() - const pos = box.childBase + program.y - const path = index.get(pos) + program.cursorPos(program.y, line.search(/\S/)) + const path = index.get(n) if (expanded.has(path)) { expanded.delete(path) render() @@ -179,18 +178,15 @@ module.exports = function start(filename, source) { }) box.on('click', function (mouse) { - program.hideCursor() - - const pos = box.childBase + mouse.y - const line = box.getScreenLines()[pos] + const [n, line] = getLine(mouse.y) if (mouse.x >= stringWidth(line)) { return } - const x = line.search(/\S/) - program.cursorPos(mouse.y, x) + program.hideCursor() + program.cursorPos(mouse.y, line.search(/\S/)) - const path = index.get(pos) + const path = index.get(n) if (expanded.has(path)) { expanded.delete(path) } else { @@ -199,39 +195,21 @@ module.exports = function start(filename, source) { render() }) + function getLine(y) { + const dy = box.childBase + y + const n = box.getNumber(dy) + const line = box.getScreenLine(dy) + return [n, line] + } + function render() { let content - [content, index] = print(json, expanded) + [content, index] = print(json, {expanded}) if (typeof content === 'undefined') { content = 'undefined' } - // TODO: Move to own fork of blessed. - let row = 0 - for (let line of content.split('\n')) { - if (stringWidth(line) > box.width) { - test.setContent(line) - const pad = test.getScreenLines().length - 1 - - const update = new Map() - for (let [i, path] of index.entries()) { - if (i > row) { - index.delete(i) - update.set(i + pad, path) - } - } - - row += pad - - for (let [i, path] of update.entries()) { - index.set(i, path) - } - - } - row++ - } - box.setContent(content) screen.render() } diff --git a/print.js b/print.js index d0b8db7..e24c031 100644 --- a/print.js +++ b/print.js @@ -2,7 +2,8 @@ const indent = require('indent-string') const config = require('./config') -function print(input, expanded = null) { +function print(input, options = {}) { + const {expanded} = options const index = new Map() let row = 0 @@ -77,7 +78,7 @@ function print(input, expanded = null) { return output + config.bracket('}') } - return v.toString() + return void 0 } return [doPrint(input), index] diff --git a/reduce.js b/reduce.js index 9ffea1b..68f882d 100644 --- a/reduce.js +++ b/reduce.js @@ -7,14 +7,12 @@ function reduce(json, code) { } if (/yield/.test(code)) { - const fx = eval(` - function fn() { - const gen = (function*(){ - ${code.replace(/\\\n/g, '')} - }).call(this) - return [...gen] - }; fn - `) + const fx = eval(`function fn() { + const gen = (function*(){ + ${code.replace(/\\\n/g, '')} + }).call(this) + return [...gen] + }; fn`) return fx.call(json) } @@ -23,11 +21,15 @@ function reduce(json, code) { } if (/^\./.test(code)) { - const fx = eval(`function fn() { return ${code === '.' ? 'this' : 'this' + code} }; fn`) + const fx = eval(`function fn() { + return ${code === '.' ? 'this' : 'this' + code} + }; fn`) return fx.call(json) } - const fx = eval(`function fn() { return ${code} }; fn`) + const fx = eval(`function fn() { + return ${code} + }; fn`) return fx.call(json) }