From bc5199c288bb93451927af0cc31a98cf0a25f843 Mon Sep 17 00:00:00 2001 From: Florrie Date: Wed, 5 Dec 2018 16:27:59 -0400 Subject: Wide character support in ANSI compressor --- util/ansi.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'util') diff --git a/util/ansi.js b/util/ansi.js index 04722c0..f0d7bf7 100644 --- a/util/ansi.js +++ b/util/ansi.js @@ -1,3 +1,5 @@ +const wcwidth = require('wcwidth') + const ESC = '\x1b' const isDigit = char => '0123456789'.indexOf(char) >= 0 @@ -138,6 +140,7 @@ const ansi = { for (let charI = 0; charI < text.length; charI++) { const cursorIndex = (cursorRow - 1) * scrCols + (cursorCol - 1) + if (text[charI] === ESC) { charI++ @@ -170,8 +173,8 @@ const ansi = { // CUP - Cursor Position (moveCursor) if (text[charI] === 'H') { - cursorRow = args[0] - cursorCol = args[1] + cursorRow = parseInt(args[0]) + cursorCol = parseInt(args[1]) } // SM - Set Mode @@ -264,7 +267,17 @@ const ansi = { char: text[charI], attributes } - cursorCol++ + // Some characters take up multiple columns, e.g. Japanese text. Take + // this into consideration when drawing. + const charColumns = wcwidth(text[charI]) + cursorCol += charColumns + + // If the character takes up 2+ columns, treat columns past the first + // one (where the character is) as empty. (Note this is different from + // "blank", which represents an empty space character ' '.) + for (let i = 1; i < charColumns; i++) { + chars[cursorIndex + i] = {char: '', attributes: []} + } if (cursorCol > scrCols) { cursorCol = 1 -- cgit 1.3.0-6-gf8a5