textual-console
Console widgets for Textual: color output with
word and line mouse selection and OSC 52 copy, a multi-line command input with
history and autocomplete (also from argparse parsers), and a plain-text transcript.
ConsoleOutput: read-only output for Rich renderables. Drag, double-click (word) and triple-click (line) select text.ctrl+ccopies it.ConsoleInput: a command prompt (aTextArea) that grows with its text, with a prompt string, history, a quiet Tab and middle-click paste.CommandAutoComplete: completes the word at the cursor. Tab opens it.ArgparseCompleter: givesCommandAutoCompleteits candidates fromargparseparsers.ConsoleLog: a plain-text transcript file of the output.
Python 3.11 or later. Tested on Textual 8.x.
Install
pip install textual-console
Try the demo:
python -m textual_console
python -m textual_console --log session.log --max-lines 200
Quick start
from rich.text import Text
from textual import on
from textual.app import App, ComposeResult
from textual_console import CommandAutoComplete, ConsoleInput, ConsoleOutput
class Repl(App[None]):
def compose(self) -> ComposeResult:
yield ConsoleOutput(id="output", max_lines=5000)
yield ConsoleInput(prompt="> ", placeholder="command", id="prompt")
yield CommandAutoComplete("#prompt", lambda line: ["help", "quit"])
@on(ConsoleInput.Submitted)
def run_command(self, event: ConsoleInput.Submitted) -> None:
self.query_one(ConsoleOutput).write(Text(f"> {event.value}", style="green"))
Repl().run()
More programs are in examples/: basic.py (text, tables, copy),
repl.py (all widgets, argparse completion and a transcript) and ansi_stream.py (ANSI output of a
subprocess).
ConsoleOutput
A RichLog that takes part in the Textual screen selection.
output = ConsoleOutput(max_lines=5000, copy_on_select=False)
output.write(Text("done", style="bold green"))
output.write_ansi("\x1b[31mred\x1b[0m")
- Drag to select. The view scrolls at the edges.
- Double-click selects a word. Triple-click selects a line.
- Shift-click moves the end of the selection. The start stays, so you can select more than one screen: start a selection, scroll, then shift-click. With no selection, nothing happens. Many terminals keep shift-click for their own selection and never send it to the app. Then use ctrl-click, which does the same.
ctrl+ccopies the selection withApp.copy_to_clipboard(OSC 52).Escapeor a single click clears the selection.copy_on_select=Truecopies when a selection ends and postsConsoleOutput.SelectionCopied.ctrl+cdoes not postSelectionCopied: onlycopy_on_selectandcopy_selection()do. To react to every copy, overrideApp.copy_to_clipboard(the demo does this to show a notification).- The widget cannot take focus, so the focus stays on your prompt.
- Copied text has one line for each rendered line, without trailing spaces.
- Set
max_linesfor long-running apps. The default is no limit, so memory grows with the session. - To change the highlight, style the
console-output--selectioncomponent class. If you do not, the themescreen--selectioncolors are used.
ConsoleInput
A TextArea that works as a command prompt. It is one row high when it is empty, grows
row by row up to max_height rows (the default is 10), and then scrolls.
history = CommandHistory(["help", "quit"])
prompt = ConsoleInput(prompt="> ", history=history, max_height=10)
Shift-click moves the end of the selection and the start stays, also when the start is scrolled out of view. Ctrl-click does the same, for terminals that keep shift-click for themselves. With no selection, both do nothing.
| Key | Action |
|---|---|
| Enter | Submit the whole text as one command. |
| shift+enter, alt+enter, ctrl+j | Insert a newline. |
| Up, down | Move the cursor between rows. On the first row up walks the history, and on the last row down does. |
| Tab | Nothing, so the focus stays in the prompt. Use tab_moves_focus=True to move the focus. |
| Esc Esc | Clear the input if both presses come within double_escape_clear seconds (default 0.5). Undo with ctrl+z. An Escape that closes the dropdown does not count. |
| Right | Accept the ghost text of a suggester. |
promptis drawn in the left gutter of the first row. It is not part of the text. Other rows get blank padding of the same width. It can change at run time, and theconsole-input--promptcomponent class styles it.suggesteris a TextualSuggester. While the text is one line and the cursor is at its end, the suggestion shows as ghost text.max_heightis checked when the widget is created (a value below 1 raisesValueError) and applied when the widget is mounted.max_height=Nonesets no limit, so a CSSmax-heightapplies.- The text is
prompt.value. The history isprompt.command_history, aCommandHistory. Save it withprompt.command_history.entries. - The first up saves the text you typed, and down past the newest entry restores it. A recalled multi-line command is shown with all its rows.
- Submit records the command, clears the input (unless
clear_on_submit=False) and postsConsoleInput.Submittedwithvalue, the whole text. - A
read_onlyinput keeps its text: paste, history, submit, completion and Esc Esc do nothing. ConsoleInput.ChangedisTextArea.Changed: usetextual.widgets.TextArea.Changed.ConsoleInputis not anInputany more. The arguments that onlyInputhas (password,restrict,type,max_length,validators,validate_on,valid_empty,select_on_focus) raiseTypeError. The editor settings ofTextArea(soft_wrap, line numbers,tab_behavior) are fixed.
Multi-line text
Enter submits the whole text, newlines included, as one command. One submit gives one
Submitted message and one history entry. To run each line as its own command, split the
value in your handler. This is the choice of your app, not of the widget:
@on(ConsoleInput.Submitted)
def run_commands(self, event: ConsoleInput.Submitted) -> None:
for line in event.value.splitlines():
if line.strip():
self.run_command(line)
Terminals and the newline keys. Many terminals send shift+enter as a plain Enter,
so the app cannot see the difference. Only terminals with the kitty keyboard protocol (or
a similar extension) report it. alt+enter and ctrl+j work in more terminals. Use
them as the fallback. In herdr, shift+enter arrives as Enter and submits.
Middle-click paste
Middle-click pastes text from inside the app. It pastes the text selected in the
output. If nothing is selected, it pastes the last text that the app copied.
All lines of a multi-line selection are pasted.
To paste text from other apps, use the terminal paste (cmd+v or ctrl+shift+v).
It keeps all lines too. ctrl+v is different: it pastes the last text that the app
copied (app.clipboard), with all lines, and not the system clipboard.
Some terminals use the middle-click for their own paste and do not send it to the app. Then you get the terminal paste. Check 4 in docs/manual-testing.md shows which one your terminal does.
CommandAutoComplete
CommandAutoComplete("#prompt", lambda line: ["open", "close", "help"])
The function gets the line of the cursor, up to the cursor, and returns the options for the word that ends at the cursor. The text before the word stays as typed, and the text after the cursor is kept. A space is added only at the end of the line. A completion that needs quotes is quoted. In a multi-line input only the cursor's line is used, and a completion changes only that line. When the cursor is inside a word, there is no completion and Tab does what the input does.
- Automatic opening: the dropdown opens while you type a word. It does not open after a space, in an empty line, or while you walk the history.
- Tab opens the dropdown when it is closed, also after a space. If there is one option, Tab applies it at once. If there are none, Tab does what the input does.
- Key order: when the dropdown is open, up and down move in the dropdown, and Tab or Enter accept the option. If the word already equals the highlighted option, Enter submits the line and only Tab accepts it. When it is closed, up and down move between rows or walk the history.
- Items: a function can return strings,
DropdownItems,CommandItem(value, help)(showshelpin a dim second column, aligned) andCompletionHint(text)(a dim, disabled row that tells what to type. It is never applied). - The candidate source is synchronous. It runs on the event loop. If the source is slow (files, network), cache the result.
- The candidate source gets any line. On Tab it can be empty, only spaces, or end in
a space, a closed quote or a backslash. It is never a line with an unclosed quote. Do
not assume that there is a first word. If the source raises, there are no candidates
and the error is logged to the
textual_consolelogger. To see it, set uplogging(for example to a file, ortextual.logging.TextualHandlerwithtextual console). To type your source, usefrom textual_console import CandidateSource. - The package needs
textual-autocomplete4.0.x, becauseCommandAutoCompleteoverrides some of its private methods.
Argparse completion
ArgparseCompleter reads argparse parsers and gives the candidates for the word at the
cursor. It never calls parse_args, so nothing prints and nothing exits.
import argparse
from textual_console import ArgparseCompleter, CommandAutoComplete, set_completer
flood = argparse.ArgumentParser(prog="flood", description="Write many lines")
flood.add_argument("count", nargs="?", type=int, metavar="COUNT", help="how many lines")
flood.add_argument("--style", choices=["plain", "red", "green"], help="line style")
open_ = argparse.ArgumentParser(prog="open", description="Open a file")
set_completer(open_.add_argument("file", metavar="FILE", help="the file"), list_files)
parsers = {"flood": flood, "open": open_}
CommandAutoComplete("#prompt", ArgparseCompleter(parsers))
- Describe the commands with a mapping
{name: parser}(read on every call, so you can add commands later) or with one root parser that has subparsers. - It offers the command names, the options not used yet (with their help), the
choicesof an option or positional (also after--option=), and the next positional, counted withnargs. It descends into subparsers. After a lone--it offers only positionals. set_completer(action, function)attaches a function that gets the current word and returns strings or items. It is stored inaction.completer, the name thatargcompleteuses. If it raises, there are no candidates and the error is logged.hints=True(the default) adds aCompletionHintsuch asFILE: the filewhere the user must type a value.show_help_option=Trueoffers-hand--help.- It reads some private
argparseattributes that have been stable since Python 3.2. If they change, completion is reduced and the error is logged.
Run the command with the same parsers. parse_args prints for -h and on errors, and
then raises SystemExit. Catch it and write the text to the output, so the app keeps
running:
import contextlib
import io
import shlex
name, *args = shlex.split(line)
buffer = io.StringIO()
try:
with contextlib.redirect_stdout(buffer), contextlib.redirect_stderr(buffer):
namespace = parsers[name].parse_args(args)
except SystemExit:
output.write(buffer.getvalue())
The demo (python -m textual_console) does this, so flood -h shows the usage.
ConsoleLog
A plain-text copy of the output, with ANSI codes removed.
log = ConsoleLog("session.log")
yield ConsoleOutput(log=log)
ConsoleInput does not write to the log. To put commands in the transcript, echo
them into the output. Then each command is written once, in order with its result:
@on(ConsoleInput.Submitted)
def run_command(self, event: ConsoleInput.Submitted) -> None:
output = self.query_one(ConsoleOutput)
output.write(f"> {event.value}")
...
ConsoleOutput.clear() does not change the file. If the file cannot be written,
ConsoleOutput.LogFailed is posted once and the app keeps running. strip_ansi(text)
is also public.
Terminals and OSC 52
Copy uses OSC 52, which asks the terminal to set the clipboard. The app cannot detect
whether it worked. ctrl+c is the reliable key. cmd+c reaches the app only on
terminals with the kitty keyboard protocol. Native terminal selection (shift-drag or
option-drag) is not changed.
Shift-click. Many terminals (xterm, kitty, WezTerm, Ghostty, GNOME Terminal,
Alacritty) use shift-click for their own selection, so the app never sees it. Use
ctrl-click there, or change the terminal setting (for example Ghostty
mouse-shift-capture = true, kitty terminal_select_modifiers, WezTerm
bypass_mouse_reporting_modifiers).
Maintainers test these things by hand in each terminal. The steps and the results table are in docs/manual-testing.md.
tmux: add set -g set-clipboard on to ~/.tmux.conf. Without it, OSC 52 copy does
not reach the outer terminal. Some terminals limit the size of the copied text or ask
for permission first.
Development
uv sync --group test --group lint
uv run pytest
uv run ruff check . && uv run ruff format --check . && uv run mypy && uv run pyright
Repository: https://github.com/thespicybyte/textual-console
For maintainers: see RELEASING.md for the release steps.
License
MIT. See LICENSE.
Metadata
Release files for textual-console 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| textual_console-0.3.0.tar.gz | 34.7 kB | With provenance | Details for textual_console-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| textual_console-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for textual_console-0.3.0-py3-none-any.whl |
Total release size: 74.4 kB
Release files / textual_console-0.3.0.tar.gz
| Download URL | textual_console-0.3.0.tar.gz |
|---|---|
| Size | 34.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7730ad74d78365fa5dcc9182b35c458c75d5596fbd7d8ccf423ceb6730827715
|
|
BLAKE2b-256 checksum How to use checksums |
81189df633d7f12aea8eda04d7ae234a009a326f7780f27fa280ceb2c5f7f06d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.
Transparency logRelease files / textual_console-0.3.0-py3-none-any.whl
| Download URL | textual_console-0.3.0-py3-none-any.whl |
|---|---|
| Size | 39.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c5fd6a3f7940c28c1e6df2a48a055797f0b4302e5e28284f819c787bd1643175
|
|
BLAKE2b-256 checksum How to use checksums |
b689c5cb43aed68e532d9b79f429c3fb75e6cf4706cf191455d2aa8cbba70c4e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.
Transparency logSecurity for textual-console 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for textual_console-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.