Skip to content

Add an &on-finish callback, and time out the terminal queries - #15

Open
cognominal wants to merge 2 commits into
japhb:mainfrom
cognominal:on-finish
Open

cognominal wants to merge 2 commits into
japhb:mainfrom
cognominal:on-finish

Conversation

@cognominal

Copy link
Copy Markdown

Summary

Two commits on RawTerminalInput.rakumod, both in CLIInput.read-input.

1. &on-finish. A caller can now look at the buffer when the user presses
Enter, and keep editing instead of returning. A REPL can use it to rewrite or
annotate a line before it runs, or to ask for a second Enter.

 read-input(Str :$mask, :&on-suspend, :&on-continue,
+           :&on-finish --> Str)
 prompt(Str:D $prompt = '', Str :$mask,
+       :&on-finish --> Str)

 when 'finish'
-  self.set-done; done
+  $verdict = &on-finish ?? on-finish($buffer) !! True
+  if $verdict ~~ Str:D
+    replace the input field with $verdict, keep editing
+  elsif $verdict
+    self.set-done; done
+  # a false verdict keeps editing, buffer unchanged

Without &on-finish the behavior is unchanged.

2. Query timeout. read-input asks the terminal for the cursor position and
the screen size, then waits without a limit. A terminal or pty that never
answers hangs the prompt forever.

+has Real:D $.query-timeout = 1
 read-input
-  $col  = await detect-cursor-pos
-  $cols = await detect-terminal-size
+  send both queries, then await both (concurrent)
+  each answer is Str after $.query-timeout
+  $col  //= 1;  $cols //= 80

A late answer stays matched to its own query record, so it is consumed and
never mistaken for typed input or another query's answer.

Evidence

  • &on-finish, before: Enter always returns the buffer.
    After: with -> $b { $b eq 'x' ?? True !! 'x' }, typing abc Enter
    shows x in the field; a second Enter returns x.
  • Timeout, before: in a pty that never answers \e[6n, prompt hangs.
    After: the prompt appears after 1 s with an 80-column field; a terminal
    that answers sees no delay, since both queries are now in flight at once.
  • zef test .: all tests pass, before and after.
  • Used by the rak REPL (irak, [RFE] in irak add feedback before submission cognominal/rak#1): the first Enter inserts my
    before undeclared variables and colors the line, a second Enter within a
    second runs it. Tested there in a pty and with t/02-rakudo/repl.t.

Merge Danger

Door: two-way

Both changes are additive; reverting them restores the old behavior.

Blast Radius: small

Callers that pass no &on-finish see no change. The timeout only affects
terminals that do not answer within 1 s; they get default sizes instead of a
hang. $.query-timeout is configurable at construction.

🤖 Generated with Claude Code

cognominal and others added 2 commits November 21, 2026 20:32
read-input awaited the cursor position and terminal size queries with
no time limit, so a terminal that does not answer \e[6n (or answers
slowly) left the prompt without an input field forever.

query-terminal, detect-cursor-pos and detect-terminal-size take an
optional :timeout; on timeout the Promise is kept with Str (Empty for
the detect methods). The query record stays active so that a late
answer is consumed by its own query, not taken as the answer to a
later one. CLIInput gets a query-timeout attribute (1 second) and
read-input sends both queries before waiting, falling back to the
first column of an 80-column terminal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Enter always ended input, with no way for the caller to validate or
rewrite the buffer first. &on-finish, if given, is called with the
buffer contents on the finish action and returns True to finish,
False to ignore the request, or a string that replaces the buffer
contents while editing goes on.

A REPL can use it to finish only complete statements, or, as the irak
REPL of rak does, to show a rewritten submission on a first Enter and
run it on a second.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cognominal added a commit to cognominal/pr-drafts that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant