Repository navigation
Accept typed input in the Console - #5
Conversation
…ols_scm versions, 100% coverage, and BeeWare's shared CI, release, and publish workflows
…are/toga-console into gtk-qt-backends # Conflicts: # README.md # pyproject.toml
# Conflicts: # .github/workflows/ci.yml # README.md # examples/console_demo/pyproject.toml # pyproject.toml # tests/test_package.py
…a helper thread on Windows, where Toga's event loop cannot watch a descriptor
…_CLEAR, which a read-only RichTextBox ignores
…each entered line is echoed on stdin and sent to the Console.stdin pipe, and the demo runs an in-app REPL
…rl-C and Ctrl-D as single bytes, in place of the stdin pipe; the demo REPL interrupts on Ctrl-C and ends on Ctrl-D
…rdware-keyboard Ctrl-C and Ctrl-D, with the TextKit input logic shared by macOS and iOS, and the demo self-test typing on every platform
…gh briefcase dev --test
…sole into windows-backend # Conflicts: # .github/workflows/ci.yml # README.md
# Conflicts: # README.md
…nd Ctrl-C and Ctrl-D, with the demo's self-test typing through each backend's own key handling
…fore its character arrives
…indows-backend # Conflicts: # .github/workflows/ci.yml # README.md # examples/console_demo/pyproject.toml # examples/console_demo/src/console_demo/app.py # examples/console_demo/tests/test_selftest.py # pyproject.toml # tests/test_package.py
…sole into console-input-desktop # Conflicts: # README.md
…onsole-input-desktop # Conflicts: # src/toga_console/platforms/winforms.py
freakboy3742
left a comment
There was a problem hiding this comment.
I think this all makes sense. There's some complexity in the REPL handling of the demo app, but I think that's born out of complexity of Python's REPL, not anything in this PR.
I've pushed a couple of minor updates (the biggest one being the use of double backticks in docstrings), plus a minor configuration change to make use of a feature that has just been added to Briefcase to simplify starting the demo app.
The only big question/rework needed is around the native_keyboard object in the demo app. It seems to me that these methods should all be capabilities of the main widget, as almost every implementation of a REPL will have some need to send structured input to the keyboard, or to retrieve the current contents of the console. I think it would make sense to make code in the keyboard.py file the implementation ends of a public API on Console that exposes the ability to type, enter, control etc.
…Toga key, caret_to(), and text(), each backend driving its native view as a user's keys would
Lets the user type into a Console, on every backend. Stacked on #4 (
windows-backend, itself on #2), so the diff shows only the input work; it retargets as those merge.The API.
Console(readonly=False, on_input=handler)lets the user type after the last output, the way a terminal does. What they enter reacheshandler(widget, data)as bytes, encoded with the console's encoding:datab"\n", after it is echoed into the scrollback on stream"stdin"b"\x03", at once; a half-typed line staysb"\x04", at once; a half-typed line staysThe console gives these bytes no meaning and does not echo the control keys; the handler decides what they do, such as writing to a subprocess's stdin, closing it on Ctrl-D, or sending
SIGINTon Ctrl-C. A read-only console, the default, delivers nothing, and Ctrl-C copies there as before.The input area. Every backend keeps the output and the input apart the same way: it tracks where the input starts, the output calls (
replace_tail,trim_lines,clear) edit only the text before that point, and user edits are allowed only after it. Backspace stops at the output, typing with the caret in the output goes to the end of the input, and output that arrives while the user types goes in before the input, leaving the caret where it was.NSTextViewdelegate,shouldChangeTextInRangekeyDown:on a text view subclassUITextViewdelegate,shouldChangeTextInRangeUIKeyCommands, from a hardware keyboardInputFilter; edits it cannot make in place are posted to the next turnOnKeyListener, from a hardware keyboardinsert-textanddelete-rangesignals; edits they cannot make in place run on idlekey-press-eventQPlainTextEditsubclass:keyPressEvent,insertFromMimeData, and a context menu without Cut and Delete over outputkeyPressEvent(Meta, the Control key, on macOS)RichTextBox'sKeyDownandKeyPressevents; Return is entered onKeyDown, since the control breaks the line before the character arrivesKeyDownmacOS and iOS share their TextKit logic in
platforms/textkit.py.The demo gains a REPL button that runs a Python REPL inside the app, on a thread fed from
on_input: Ctrl-C raisesKeyboardInterruptin it, and Ctrl-D at the prompt ends it. Its self-test now also types6 * 7into the native view through each backend's own key handling and checks the answer, that Backspace cannot reach the prompt, that typing with the caret in the output lands in the input, that Ctrl-C interrupts a busy loop and a prompt, and that Ctrl-D ends the REPL.Testing
tox -m test: all tests pass, 100% coverage. Pre-commit is clean.PR Checklist:
Assisted-by: Claude Code (Claude Opus 5.5)