Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add multiline support #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions source/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 `)`: `(|)` -> `()|`
Expand Down Expand Up @@ -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 {
Expand Down