From 1f434c1ef11fa55bab1718ea4e3ca8d115c0dfb1 Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 8 Dec 2018 02:37:15 -0400 Subject: Mouse support Not exactly the most elegant implementation, but it definitely works and isn't really difficult to code around! --- util/telchars.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'util/telchars.js') 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'), -- cgit 1.3.0-6-gf8a5