Skip to content

Commit

Permalink
In raw mode, return Escape immediately. (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt authored Sep 2, 2024
1 parent 941a636 commit 8e8b678
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- **Breaking Change** Moved `Terminal.info.width` and `height` to `Terminal.size.width` and `height`.
- **Breaking Change** `TerminalInterface.info` is now a method with parameters instead of a property.
- **Breaking Change** Moved `Markdown` widget to separate `mordant-markdown` module, which is not included by default. If you use markdown rendering, you need to add that module to you dependencies.
- In raw mode on POSIX systems, pressing the escape key once will now immediately return an `Escape` event. [(#193)](https://github.com/ajalt/mordant/issues/193)

### Removed
- Removed constructor overloads for `Terminal`. There is now one constructor with all default parameters.
Expand Down
6 changes: 1 addition & 5 deletions docs/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,7 @@ app and operating system. Some things to keep in mind:
other than Windows.
- Some key combinations aren't reported because they're intercepted by the terminal app to perform
actions like switching tabs or closing the window.
- On Linux and macOS, the Escape key isn't reported as a key press; instead, it begins a "VTI escape
sequence" that the terminal uses to report key presses. For example if you press `Escape`, then `[`,
then `d`, the terminal will report that as the left arrow key being pressed. It's up to you whether
you consider this a feature or a limitation.
- Raw mode is supported on JS or wasmJS targets on Node.js only. You can also use Node.js's
- For JS or wasmJS targets, raw mode is supported on Node.js only. You can also use Node.js's
`readline` module to read input with callbacks instead of blocking, or in the browser you can use
the `keydown` and `mousedown` events.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.ajalt.mordant.input.KeyboardEvent
import com.github.ajalt.mordant.input.MouseEvent
import com.github.ajalt.mordant.internal.codepointToString
import com.github.ajalt.mordant.internal.readBytesAsUtf8
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.TimeMark
import kotlin.time.TimeSource
Expand Down Expand Up @@ -36,8 +37,8 @@ internal class PosixEventParser(
val s = StringBuilder()
var ch: Char

fun read(): Char? {
ch = readRawByte(timeout)?.toChar() ?: return null
fun read(t:TimeMark = timeout): Char? {
ch = readRawByte(t)?.toChar() ?: return null
s.append(ch)
return ch
}
Expand All @@ -51,7 +52,10 @@ internal class PosixEventParser(

if (ch == ESC) {
escaped = true
read() ?: return KeyboardEvent("Escape")
// If there's nothing else in the buffer, return "Escape" immediately. This means that
// you can't manually type in escape sequences, but that's a pretty rare use case
// compared to just pressing escape.
read(TimeSource.Monotonic.markNow() + 5.milliseconds) ?: return KeyboardEvent("Escape")
if (ch == ESC) {
return KeyboardEvent("Escape")
}
Expand Down

0 comments on commit 8e8b678

Please sign in to comment.