From 269d0a7ddecc0a354c9cb720813738c45c170390 Mon Sep 17 00:00:00 2001 From: GavinRay97 Date: Sun, 15 Dec 2019 21:30:32 -0500 Subject: [PATCH] WIP: Add multiline support This will allow you to press shift + enter by maintaining a queue of 2 keycodes from last pressed keys. I had to use `iohook` library to be able to capture JUST shift key presses. I did not take a ton of time to set it up properly, so it does not teardown listener and has a memory leak. Also, I cant get the evaluate to work properly with multiline, but this is a good start I think. ```ts ioHook.on('keypress', (event: IOHookEvent) => { if (lastKeys.length < 2) lastKeys.push(event) else { lastKeys.pop() lastKeys.unshift(event) } });``` ```ts } else if (name === 'return') { const lastKey = lastKeys.concat().pop() if (lastKey!.shiftKey) { stdout.write('\n') rl.write('\n') } else { ``` --- source/prompt.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/source/prompt.ts b/source/prompt.ts index 03d99c9..44ac45d 100644 --- a/source/prompt.ts +++ b/source/prompt.ts @@ -20,6 +20,21 @@ import {CompletionsMeta} from './complete' import {PureEvaluator} from './eval' import {debug, getColumnSizes, time} from './util' +import ioHook from 'iohook' + +interface IOHookEvent { + type: string + keychar?: number + keycode?: number + rawcode?: number + shiftKey?: boolean + ctrlKey?: boolean + altKey?: boolean + metaKey?: boolean +} + +let lastKeys: IOHookEvent[]= [{type: ``}] + export interface KeypressDetails { sequence: string name: string @@ -382,8 +397,14 @@ export default function promptLine({ stdout.write('\n') process.kill(process.pid, 'SIGTSTP') } else if (name === 'return') { - stop() - return resolve([Commands.Line, {line: rl.line}]) + const lastKey = lastKeys.concat().pop() + if (lastKey!.shiftKey) { + stdout.write('\n') + rl.write('\n') + } else { + stop() + return resolve([Commands.Line, {line: rl.line}]) + } } else if (hasPrePair(sequence) && sequence === relativeCharacter(0)) { // Next character is a pair and is the same as the one entered, ghost it // input `)`: `(|)` -> `()|` @@ -464,6 +485,17 @@ export default function promptLine({ stdout.on('resize', resizeListener) stdin.on('keypress', keypressListener) + ioHook.on('keypress', (event: IOHookEvent) => { + if (lastKeys.length < 2) lastKeys.push(event) + else { + lastKeys.pop() + lastKeys.unshift(event) + } + }); + + + ioHook.start(false); + rerender() function stop(): void {