« get me outta code hell

Add backspace and shift-arrows to telchars - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-12-08 11:55:40 -0400
committerFlorrie <towerofnix@gmail.com>2017-12-08 11:55:40 -0400
commit1a0a5e16f303d6450b1a9762a5e3298a124489c9 (patch)
tree9dbcc30d86c71e81eacde1327e23b47f6ae30509
parent12f5eb1f7b6c02972ad23e698a418210ed743632 (diff)
Add backspace and shift-arrows to telchars
-rw-r--r--util/telchars.js10
1 files changed, 10 insertions, 0 deletions
diff --git a/util/telchars.js b/util/telchars.js
index 8cf414c..7f4d8e4 100644
--- a/util/telchars.js
+++ b/util/telchars.js
@@ -1,10 +1,15 @@
 // Useful telnet key detection.
 
+const compareBufStr = (buf, str) => {
+  return buf.equals(Buffer.from(str.split('').map(c => c.charCodeAt(0))))
+}
+
 const telchars = {
   isSpace: buf => buf[0] === 0x20,
   isEnter: buf => buf[0] === 0x0d && buf[1] === 0x00,
   isTab: buf => buf[0] === 0x09,
   isBackTab: buf => buf[0] === 0x1b && buf[2] === 0x5A,
+  isBackspace: buf => buf[0] === 0x7F,
 
   // isEscape is hard because it's just send as ESC (the ANSI escape code),
   // so we need to make sure that the escape code is all on its own
@@ -25,6 +30,11 @@ const telchars = {
   isDown: buf => buf[0] === 0x1b && buf[2] === 0x42,
   isRight: buf => buf[0] === 0x1b && buf[2] === 0x43,
   isLeft: buf => buf[0] === 0x1b && buf[2] === 0x44,
+
+  isShiftUp: buf => buf[0] === 0x1b && compareBufStr(buf.slice(1), '[1;2A'),
+  isShiftDown: buf => buf[0] === 0x1b && compareBufStr(buf.slice(1), '[1;2B'),
+  isShiftRight: buf => buf[0] === 0x1b && compareBufStr(buf.slice(1), '[1;2C'),
+  isShiftLeft: buf => buf[0] === 0x1b && compareBufStr(buf.slice(1), '[1;2D'),
 }
 
 module.exports = telchars