Skip to content

Commit

Permalink
🐛 Ignore CSI 6 n response when keypress reading (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
844196 authored Sep 14, 2024
2 parents 91b1a19 + 2e4c707 commit 80663a4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { renderPrompt } from './ui.ts'
import { renderTable } from './ui.ts'
import { Eta } from '@eta-dev/eta'
import widgetTemplate from './widget.generated.json' with { type: 'json' }
import { keypress } from '@cliffy/keypress'

const cli = new Command()
.name('wk')
Expand Down Expand Up @@ -99,7 +98,7 @@ const run = new Command()
}

const deps: Dependencies = {
receiveKeyPress: keypress,
keypress: tui.keypress,
draw: (inputKeys, bindings) => tui.draw(renderPrompt(ctx, inputKeys), renderTable(ctx, bindings).toString()),
setTimeoutTimer: () => {
if (ctx.timeout > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { keypress, KeyPressEvent } from '@cliffy/keypress'
import { type KeyPressEvent } from '@cliffy/keypress'
import { PRINTABLE_ASCII } from './const.ts'
import { AbortError, KeyParseError, UndefinedKeyError } from './errors.ts'
import { type Binding, type Command } from './types/Binding.ts'

export type Dependencies = {
receiveKeyPress: () => AsyncIterator<KeyPressEvent, PromiseLike<KeyPressEvent>>
keypress: () => AsyncIterable<KeyPressEvent>
draw: (inputKeys: string[], bindings: Binding[]) => unknown
setTimeoutTimer: () => unknown
clearTimeoutTimer: () => unknown
Expand All @@ -17,7 +17,7 @@ export async function main(deps: Dependencies, bindings: Binding[]): Promise<Com
deps.draw(inputKeys, bindings)
deps.setTimeoutTimer()

for await (const key of keypress()) {
for await (const key of deps.keypress()) {
deps.clearTimeoutTimer()

if (typeof key.key === 'undefined') {
Expand Down
10 changes: 10 additions & 0 deletions src/tui.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ansi } from '@cliffy/ansi'
import { tty as ttyFactory } from '@cliffy/ansi/tty'
import { keypress, KeyPressEvent } from '@cliffy/keypress'

export class TUI {
#reader: Deno.FsFile
Expand Down Expand Up @@ -65,4 +66,13 @@ export class TUI {
.bytes(),
)
}

async *keypress(): AsyncIterable<KeyPressEvent> {
for await (const key of keypress()) {
if (key.sequence?.match(/\[\d+;\d+R/)) { // CSI 6 n response
continue
}
yield key
}
}
}

0 comments on commit 80663a4

Please sign in to comment.