From d809ba2081041504998ad3b77c66b24e051f9458 Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 22 Aug 2019 11:39:32 -0300 Subject: Add trimToColumns ansi utility function --- util/ansi.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'util') diff --git a/util/ansi.js b/util/ansi.js index d58684f..6f3e23e 100644 --- a/util/ansi.js +++ b/util/ansi.js @@ -143,6 +143,21 @@ const ansi = { return wcwidth(text) }, + trimToColumns(text, cols) { + // Trims off the end of the passed text so that its width doesn't exceed + // the size passed in columns. + + let out = '' + for (const char of text) { + if (ansi.measureColumns(out + char) <= cols) { + out += char + } else { + break + } + } + return out + }, + isANSICommand(buffer, code = null) { return ( buffer[0] === 0x1b && buffer[1] === 0x5b && -- cgit 1.3.0-6-gf8a5