Skip to content

Accept typed input in the Console - #5

Merged
freakboy3742 merged 33 commits into
mainfrom
console-input-desktop
Nov 10, 2026
Merged

freakboy3742 merged 33 commits into
mainfrom
console-input-desktop

Conversation

@kattni

@kattni kattni commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

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 reaches handler(widget, data) as bytes, encoded with the console's encoding:

Key data
Return the line and b"\n", after it is echoed into the scrollback on stream "stdin"
a paste with line breaks all its complete lines in one call
Ctrl-C b"\x03", at once; a half-typed line stays
Ctrl-D b"\x04", at once; a half-typed line stays

The 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 SIGINT on 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.

Backend How edits are guarded Ctrl-C and Ctrl-D
macOS NSTextView delegate, shouldChangeTextInRange keyDown: on a text view subclass
iOS UITextView delegate, shouldChangeTextInRange UIKeyCommands, from a hardware keyboard
Android an InputFilter; edits it cannot make in place are posted to the next turn an OnKeyListener, from a hardware keyboard
GTK the buffer's insert-text and delete-range signals; edits they cannot make in place run on idle key-press-event
Qt a QPlainTextEdit subclass: keyPressEvent, insertFromMimeData, and a context menu without Cut and Delete over output keyPressEvent (Meta, the Control key, on macOS)
Windows the RichTextBox's KeyDown and KeyPress events; Return is entered on KeyDown, since the control breaks the line before the character arrives KeyDown

macOS 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 raises KeyboardInterrupt in it, and Ctrl-D at the prompt ends it. Its self-test now also types 6 * 7 into 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.
  • CI passes: the unit tests on Ubuntu and Windows for Python 3.11 to 3.15, and the demo's self-test on GTK, Qt, and Windows.
  • The demo's self-test also passes locally on macOS (Cocoa), the iOS simulator, the Android emulator, and Qt on macOS.
  • Typed by hand as well: on the iOS simulator with its keyboard, and on the Android emulator with injected key events, including Ctrl-C and Ctrl-D.
  • GTK and Windows have only run in CI, not by hand. On Windows the self-test sends Ctrl-C and Ctrl-D to the console's key handler directly, since a sent message cannot carry the Control key's state, so Windows' own routing of those keys is untested.
  • On Android, typing on the on-screen keyboard, which inserts whole words rather than single keys, is untested.
  • Qt's demo prints an event loop traceback as it exits; the same happens on Add a Windows backend #4's branch without these changes.

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool
    Assisted-by: Claude Code (Claude Opus 5.5)

kattni and others added 23 commits October 17, 2026 15:11
…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
…sole into windows-backend

# Conflicts:
#	.github/workflows/ci.yml
#	README.md
…nd Ctrl-C and Ctrl-D, with the demo's self-test typing through each backend's own key handling
@kattni
kattni marked this pull request as ready for review October 29, 2026 22:34
freakboy3742 and others added 4 commits November 9, 2026 15:41
…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
Base automatically changed from windows-backend to main November 9, 2026 07:55
…onsole-input-desktop

# Conflicts:
#	src/toga_console/platforms/winforms.py

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/toga_console/platforms/cocoa.py Resolved
…Toga key, caret_to(), and text(), each backend driving its native view as a user's keys would

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM!

@freakboy3742
freakboy3742 merged commit 8b08eb1 into main Nov 10, 2026
19 checks passed
@freakboy3742
freakboy3742 deleted the console-input-desktop branch November 10, 2026 03:23
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.

2 participants