« get me outta code hell

tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/ansi.js19
1 files changed, 16 insertions, 3 deletions
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