diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2023-05-12 17:42:09 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2023-05-13 12:48:36 -0300 |
commit | 6ea74c268a12325296a1d2e7fc31b02030ddb8bc (patch) | |
tree | 5da94d93acb64e7ab650d240d6cb23c659ad02ca /util/CommandLineInterfacer.js | |
parent | e783bcf8522fa68e6b221afd18469c3c265b1bb7 (diff) |
use ESM module syntax & minor cleanups
The biggest change here is moving various element classes under more scope-specific directories, which helps to avoid circular dependencies and is just cleaner to navigate and expand in the future. Otherwise this is a largely uncritical port to ESM module syntax! There are probably a number of changes and other cleanups that remain much needed. Whenever I make changes to tui-lib it's hard to believe it's already been <INSERT COUNTING NUMBER HERE> years since the previous time. First commits are from January 2017, and the code originates a month earlier in KAaRMNoD!
Diffstat (limited to 'util/CommandLineInterfacer.js')
-rw-r--r-- | util/CommandLineInterfacer.js | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/util/CommandLineInterfacer.js b/util/CommandLineInterfacer.js deleted file mode 100644 index d2007fb..0000000 --- a/util/CommandLineInterfacer.js +++ /dev/null @@ -1,91 +0,0 @@ -const EventEmitter = require('events') -const waitForData = require('./waitForData') -const ansi = require('./ansi') - -module.exports = class CommandLineInterfacer extends EventEmitter { - constructor(inStream = process.stdin, outStream = process.stdout, proc = process) { - super() - - this.inStream = inStream - this.outStream = outStream - this.process = proc - - inStream.on('data', buffer => { - this.emit('inputData', buffer) - }) - - inStream.setRawMode(true) - - proc.on('SIGWINCH', async buffer => { - this.emit('resize', await this.getScreenSize()) - }) - } - - async getScreenSize() { - const waitUntil = cond => waitForData(this.inStream, cond) - - // Get old cursor position.. - this.outStream.write(ansi.requestCursorPosition()) - const { options: oldCoords } = this.parseANSICommand( - await waitUntil(buf => ansi.isANSICommand(buf, 82)) - ) - - // Move far to the bottom right of the screen, then get cursor position.. - // (We could use moveCursor here, but the 0-index offset isn't really - // relevant.) - this.outStream.write(ansi.moveCursorRaw(9999, 9999)) - this.outStream.write(ansi.requestCursorPosition()) - const { options: sizeCoords } = this.parseANSICommand( - await waitUntil(buf => ansi.isANSICommand(buf, 82)) - ) - - // Restore to old cursor position.. (Using moveCursorRaw is actaully - // necessary here, since we'll be passing the coordinates returned from - // another ANSI command.) - this.outStream.write(ansi.moveCursorRaw(oldCoords[0], oldCoords[1])) - - // And return dimensions. - const [ sizeLine, sizeCol ] = sizeCoords - return { - lines: sizeLine, cols: sizeCol, - width: sizeCol, height: sizeLine - } - } - - parseANSICommand(buffer) { - // Typically ANSI commands are written ESC[1;2;3;4C - // ..where ESC is the ANSI escape code, equal to hexadecimal 1B and - // decimal 33 - // ..where [ and ; are the literal strings "[" and ";" - // ..where 1, 2, 3, and 4 are decimal integer arguments written in ASCII - // that may last more than one byte (e.g. "15") - // ..where C is some number representing the code of the command - - if (buffer[0] !== 0x1b || buffer[1] !== 0x5b) { - throw new Error('Not an ANSI command') - } - - const options = [] - let curOption = '' - let commandCode = null - for (const val of buffer.slice(2)) { - if (48 <= val && val <= 57) { // 0124356789 - curOption = curOption.concat(val - 48) - } else { - options.push(parseInt(curOption)) - curOption = '' - - if (val !== 59) { // ; - commandCode = val - break - } - } - } - - return {code: commandCode, options: options} - } - - write(data) { - this.outStream.write(data) - } -} |