-
Notifications
You must be signed in to change notification settings - Fork 5.6k
fix(runtime): Make native modal keyboard interaction consistent with browsers #18453
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
Merged
bartlomieju
merged 21 commits into
denoland:main
from
lionel-rowe:alert-prompt-confirm-keyboard-interaction
Dec 13, 2023
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d279a07
Rebase and fix tests
lionel-rowe 8b237e9
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju d31fe0e
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
lionel-rowe 780797d
breaking - throw instead of returning early if !isatty
lionel-rowe 8861563
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
lionel-rowe d679281
Implement changes from code review
lionel-rowe dc369c5
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
lionel-rowe 06ebf48
Implement changes from code review
lionel-rowe 88317d4
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju b79f443
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju 67a5e25
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju d3b7253
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
lionel-rowe 94e192f
Exclude ctrl+c/ctrl+d tests from macos
lionel-rowe 27d449a
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
lionel-rowe 9c2c740
Fix test on Windows
dsherret f8f13de
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
dsherret 1332048
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju 6778a12
update rustyline
bartlomieju ebb3f63
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju 2634dc6
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju 445c895
Merge branch 'main' into alert-prompt-confirm-keyboard-interaction
bartlomieju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,95 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { core, primordials } from "ext:core/mod.js"; | ||
const ops = core.ops; | ||
import { isatty } from "ext:runtime/40_tty.js"; | ||
import { stdin } from "ext:deno_io/12_io.js"; | ||
const { ArrayPrototypePush, StringPrototypeCharCodeAt, Uint8Array } = | ||
primordials; | ||
const LF = StringPrototypeCharCodeAt("\n", 0); | ||
const CR = StringPrototypeCharCodeAt("\r", 0); | ||
import { getNoColor } from "ext:deno_console/01_console.js"; | ||
const { Uint8Array, StringFromCodePoint } = primordials; | ||
|
||
const ESC = "\x1b"; | ||
const CTRL_C = "\x03"; | ||
const CTRL_D = "\x04"; | ||
|
||
const bold = ansi(1, 22); | ||
const italic = ansi(3, 23); | ||
const yellow = ansi(33, 0); | ||
function ansi(start, end) { | ||
return (str) => getNoColor() ? str : `\x1b[${start}m${str}\x1b[${end}m`; | ||
} | ||
|
||
function alert(message = "Alert") { | ||
if (!isatty(stdin.rid)) { | ||
return; | ||
} | ||
|
||
core.print(`${message} [Enter] `, false); | ||
core.print( | ||
`${yellow(bold(`${message}`))} [${italic("Press any key to continue")}] `, | ||
); | ||
|
||
try { | ||
stdin.setRaw(true); | ||
stdin.readSync(new Uint8Array(1024)); | ||
} finally { | ||
stdin.setRaw(false); | ||
} | ||
|
||
readLineFromStdinSync(); | ||
core.print("\n"); | ||
} | ||
|
||
function confirm(message = "Confirm") { | ||
function prompt(message = "Prompt", defaultValue = "") { | ||
if (!isatty(stdin.rid)) { | ||
return false; | ||
return null; | ||
} | ||
|
||
core.print(`${message} [y/N] `, false); | ||
|
||
const answer = readLineFromStdinSync(); | ||
|
||
return answer === "Y" || answer === "y"; | ||
return ops.op_read_line_prompt( | ||
`${message} `, | ||
`${defaultValue}`, | ||
); | ||
} | ||
|
||
function prompt(message = "Prompt", defaultValue) { | ||
defaultValue ??= null; | ||
const inputMap = new primordials.Map([ | ||
["Y", true], | ||
["y", true], | ||
["\r", true], | ||
["\n", true], | ||
["\r\n", true], | ||
["N", false], | ||
["n", false], | ||
[ESC, false], | ||
[CTRL_C, false], | ||
[CTRL_D, false], | ||
]); | ||
|
||
function confirm(message = "Confirm") { | ||
if (!isatty(stdin.rid)) { | ||
return null; | ||
return false; | ||
} | ||
|
||
if (defaultValue) { | ||
message += ` [${defaultValue}]`; | ||
} | ||
core.print(`${yellow(bold(`${message}`))} [${italic("Y/n")}] `); | ||
|
||
message += " "; | ||
let val = false; | ||
try { | ||
stdin.setRaw(true); | ||
|
||
// output in one shot to make the tests more reliable | ||
core.print(message, false); | ||
while (true) { | ||
const b = new Uint8Array(1024); | ||
stdin.readSync(b); | ||
let byteString = ""; | ||
|
||
return readLineFromStdinSync() || defaultValue; | ||
} | ||
let i = 0; | ||
while (b[i]) byteString += StringFromCodePoint(b[i++]); | ||
|
||
function readLineFromStdinSync() { | ||
const c = new Uint8Array(1); | ||
const buf = []; | ||
|
||
while (true) { | ||
const n = stdin.readSync(c); | ||
if (n === null || n === 0) { | ||
break; | ||
} | ||
if (c[0] === CR) { | ||
const n = stdin.readSync(c); | ||
if (c[0] === LF) { | ||
break; | ||
} | ||
ArrayPrototypePush(buf, CR); | ||
if (n === null || n === 0) { | ||
if (inputMap.has(byteString)) { | ||
val = inputMap.get(byteString); | ||
break; | ||
} | ||
} | ||
if (c[0] === LF) { | ||
break; | ||
} | ||
ArrayPrototypePush(buf, c[0]); | ||
} finally { | ||
stdin.setRaw(false); | ||
} | ||
return core.decode(new Uint8Array(buf)); | ||
|
||
core.print(`${val ? "y" : "n"}\n`); | ||
return val; | ||
} | ||
|
||
export { alert, confirm, prompt }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're supposed to read only a single key here, why do we need a buffer with size 1024? Could you also explain why you're setting the terminal to raw mode here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single logical keypress (most notably, combos such as shift+insert/ctrl+v) can send an arbitrarily large amount of input to stdin. 1024 is just an arbitrary size that should typically be large enough to avoid problems but small enough to avoid undue memory usage.
For example, if we used a smaller buffer size like
10
:That can be broken (Deno exits with
Error: Io(Kind(InvalidData))
) by copying字字字字
to the clipboard and pasting it with the next keypress, because the 3 UTF-8 bytes of the last字
lie across the boundary.Raw mode is necessary to capture ESC keypresses and to progress immediately after a key is pressed.