From 695149a96299f9f1d3d0cc0747a7390a82d69525 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 23 Oct 2019 11:01:49 -0300 Subject: Correctly enable ANSI compression in tuiApp util --- util/tui-app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/tui-app.js b/util/tui-app.js index a695e57..fe1cd03 100644 --- a/util/tui-app.js +++ b/util/tui-app.js @@ -14,7 +14,7 @@ module.exports = async function tuiApp(callback) { const flushable = new Flushable(process.stdout, true); - const root = new Root(interfacer); + const root = new Root(interfacer, flushable); const size = await interfacer.getScreenSize(); root.w = size.width; -- cgit 1.3.0-6-gf8a5 From 3bd328e68894a7819aa26e2325d7984eaa3959e0 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 15 Jul 2020 22:08:34 -0300 Subject: don't include ANSI escape codes in measureColumns This allows pretty much any code based around measureColumns (e.g. buttons, labels, word-wrapping code) to handle ANSI-formatted text without any miscalculated measurements. --- util/ansi.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/ansi.js b/util/ansi.js index ac511ed..f921349 100644 --- a/util/ansi.js +++ b/util/ansi.js @@ -153,7 +153,12 @@ const ansi = { }, measureColumns(text) { - // Returns the number of columns the given text takes. + // Returns the number of columns the given text takes. Accounts for escape + // codes (by not including them in the returned width). + + if (text.includes(ESC)) { + text = text.replace(new RegExp(ESC + '\\[\\??[0-9]*.', 'g'), '') + } return wcwidth(text) }, -- cgit 1.3.0-6-gf8a5 From cff17d1f056a73714ff1ba5a735987d140a6b51c Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 16 Jul 2020 14:02:17 -0300 Subject: handle semicolons in measureColumns correctly too! --- util/ansi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/ansi.js b/util/ansi.js index f921349..d4ed71d 100644 --- a/util/ansi.js +++ b/util/ansi.js @@ -157,7 +157,7 @@ const ansi = { // codes (by not including them in the returned width). if (text.includes(ESC)) { - text = text.replace(new RegExp(ESC + '\\[\\??[0-9]*.', 'g'), '') + text = text.replace(new RegExp(ESC + '\\[\\??[0-9;]*.', 'g'), '') } return wcwidth(text) -- cgit 1.3.0-6-gf8a5 From 7f0579fc6e5771bbcad36591ab54119c4fe66dbd Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 16 Jul 2020 14:04:25 -0300 Subject: improve & use our own word wrapping code --- util/wrap.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/wrap.js b/util/wrap.js index 3c381d4..71a1f1c 100644 --- a/util/wrap.js +++ b/util/wrap.js @@ -1,3 +1,5 @@ +const ansi = require('./ansi') + module.exports = function wrap(str, width) { // Wraps a string into separate lines. Returns an array of strings, for // each line of the text. @@ -6,13 +8,17 @@ module.exports = function wrap(str, width) { const words = str.split(' ') let curLine = words[0] + let curColumns = ansi.measureColumns(curLine) for (const word of words.slice(1)) { - if (curLine.length + word.length > width) { + const wordColumns = ansi.measureColumns(word) + if (curColumns + wordColumns > width) { lines.push(curLine) curLine = word + curColumns = wordColumns } else { curLine += ' ' + word + curColumns += 1 + wordColumns } } -- cgit 1.3.0-6-gf8a5 From 3614ce99f2eaa910a2b11b0f2f4a8a291ee3a4b0 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 25 Mar 2022 17:39:41 -0300 Subject: fix ancient waitForData util listener memory leak Particularly, this added a new event listener (i.e. function) every time the screen was resized, which would never be garbage collected. Oops. --- util/waitForData.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'util') diff --git a/util/waitForData.js b/util/waitForData.js index bf40c52..ed88402 100644 --- a/util/waitForData.js +++ b/util/waitForData.js @@ -1,9 +1,11 @@ module.exports = function waitForData(stream, cond = null) { return new Promise(resolve => { - stream.on('data', data => { + const listener = data => { if (cond ? cond(data) : true) { resolve(data) + stream.removeListener('data', listener) } - }) + } + stream.on('data', listener) }) } -- cgit 1.3.0-6-gf8a5