Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/puny-rats-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform-node-shared": patch
---

Allow `Terminal.readLine` to more closely emulate native behavior by echoing user input as well as responding to signals entered by the user
52 changes: 42 additions & 10 deletions packages/platform-node-shared/src/internal/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ export const make = Effect.fnUntraced(function*(
const stdin = process.stdin
const stdout = process.stdout

// Acquire readline interface with TTY setup/cleanup inside the scope
const makeUserInput = (char: string | undefined, key: readline.Key): Terminal.UserInput => ({
input: Option.fromNullable(char),
key: {
name: key.name ?? "",
ctrl: !!key.ctrl,
meta: !!key.meta,
shift: !!key.shift
}
})

// Acquire readline interface with raw mode for readInput
const rlRef = yield* RcRef.make({
acquire: Effect.acquireRelease(
Effect.sync(() => {
const rl = readline.createInterface({ input: stdin, escapeCodeTimeout: 50 })
readline.emitKeypressEvents(stdin, rl)

if (stdin.isTTY) {
stdin.setRawMode(true)
}
Expand All @@ -40,16 +49,27 @@ export const make = Effect.fnUntraced(function*(
)
})

// Acquire readline interface with echo for readLine
const rlRefWithEcho = yield* RcRef.make({
acquire: Effect.acquireRelease(
Effect.sync(() =>
readline.createInterface({
input: stdin,
output: stdout,
escapeCodeTimeout: 50
})
),
(rl) => Effect.sync(() => rl.close())
)
})

const columns = Effect.sync(() => stdout.columns ?? 0)

const readInput = Effect.gen(function*() {
yield* RcRef.get(rlRef)
const mailbox = yield* Mailbox.make<Terminal.UserInput>()
const handleKeypress = (s: string | undefined, k: readline.Key) => {
const userInput = {
input: Option.fromNullable(s),
key: { name: k.name ?? "", ctrl: !!k.ctrl, meta: !!k.meta, shift: !!k.shift }
}
const handleKeypress = (char: string | undefined, key: readline.Key) => {
const userInput = makeUserInput(char, key)
mailbox.unsafeOffer(userInput)
if (shouldQuit(userInput)) {
mailbox.unsafeDone(Exit.void)
Expand All @@ -60,12 +80,24 @@ export const make = Effect.fnUntraced(function*(
return mailbox as Mailbox.ReadonlyMailbox<Terminal.UserInput>
})

const readLine = RcRef.get(rlRef).pipe(
const readLine = RcRef.get(rlRefWithEcho).pipe(
Effect.flatMap((readlineInterface) =>
Effect.async<string, Terminal.QuitException>((resume) => {
const onLine = (line: string) => resume(Effect.succeed(line))
const onLine = (line: string) => {
resume(Effect.succeed(line))
}
const onKeypress = (char: string | undefined, key: readline.Key) => {
const userInput = makeUserInput(char, key)
if (shouldQuit(userInput)) {
resume(Effect.fail(new Terminal.QuitException()))
}
}
readlineInterface.once("line", onLine)
return Effect.sync(() => readlineInterface.off("line", onLine))
stdin.on("keypress", onKeypress)
return Effect.sync(() => {
readlineInterface.off("line", onLine)
stdin.off("keypress", onKeypress)
})
})
),
Effect.scoped
Expand Down