« get me outta code hell

Mouse support - 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/telchars.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-12-08 02:37:15 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-08 02:37:15 -0400
commit1f434c1ef11fa55bab1718ea4e3ca8d115c0dfb1 (patch)
tree341321a95d376faf720dda8d4e0ca4b69880f3e6 /util/telchars.js
parent013835d81e5e56f59faf17e215a73f9e8dc4310b (diff)
Mouse support
Not exactly the most elegant implementation, but it definitely works and
isn't really difficult to code around!
Diffstat (limited to 'util/telchars.js')
-rw-r--r--util/telchars.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/util/telchars.js b/util/telchars.js
index dcb840f..b099f65 100644
--- a/util/telchars.js
+++ b/util/telchars.js
@@ -31,6 +31,42 @@ const telchars = {
   isRight: buf => buf[0] === 0x1b && buf[2] === 0x43,
   isLeft: buf => buf[0] === 0x1b && buf[2] === 0x44,
 
+  // Mouse constants!
+  mapMouseActionNum: num => {
+    let button = null
+
+    if (num & 64) {
+      if (num & 1) button = 'scroll-down'
+      else button = 'scroll-up'
+    } else {
+      const bits = num & 3
+      if (bits === 0) button = 'left'
+      else if (bits === 1) button = 'middle'
+      else if (bits === 2) button = 'right'
+      else if (bits === 3) button = 'release'
+    }
+
+    const shift = !!(num & 4)
+    const ctrl = !!(num & 16)
+
+    return {button, shift, ctrl}
+  },
+
+  isMouse: buf => buf[0] === 0x1b && buf[2] === 0x4d,
+  parseMouse: buf => {
+    if (!telchars.isMouse(buf)) {
+      return null
+    }
+
+    const actionNum = buf[3] - 32
+    const col = buf[4] - 32
+    const line = buf[5] - 32
+
+    const { button, shift, ctrl } = telchars.mapMouseActionNum(actionNum)
+
+    return {button, shift, ctrl, col, line, actionNum}
+  },
+
   isShiftUp: buf => compareBufStr(buf, '\x1b[1;2A'),
   isShiftDown: buf => compareBufStr(buf, '\x1b[1;2B'),
   isShiftRight: buf => compareBufStr(buf, '\x1b[1;2C'),