tui
tuprolog.tui
¶
2ppy TUI: a terminal IDE for Prolog, built with Textual.
Run it with 2ppy-tui or python -m tuprolog.tui (--help lists the options: files to open,
theory, query, standard input, and how solvers are built: dialect, libraries, dynamic knowledge
base, unificator, flags, timeout; see parser). As 2P-Kt's Swing IDE, it shows pages (as tabs),
each with a theory to edit, a query to solve against it (both colored by syntax), the timeout of
its resolutions, and panels (as tabs, at the bottom) for
the solutions, the standard input, output and error of the solver, its warnings, the problems
found in the theory, and what the solver holds: operators (more can be added), flags (editable),
libraries, static and dynamic knowledge bases (flags and libraries with their documentation, F1). Arrow keys move among controls, unless the focused one uses them
(e.g. editors). Pages either have their own text (scratch pages, ctrl+t) or edit files
(ctrl+n creates one, ctrl+o opens one, ctrl+s saves it). F5, F6 and F7 ask for 1, 10
and 100 solutions (the Solve all button, for all of them) (then for the next ones), F8 stops a resolution, F9 resets the solver, and
ctrl+q quits (asking to confirm if files have unsaved changes).
The IDE holds no logic of its own: it renders the state of 2P-Kt's GUI application (see
tuprolog.gui), sends it the actions of the user, and fulfils its effects (e.g. asking for a path,
then writing the file). It talks to it through a GuiBridge: JvmGuiBridge runs 2P-Kt's,
converting its JVM states into snapshots (Snapshot, Page, ...), and the IDE's actions (e.g.
Solve) into JVM ones. ReloadDecision, Solution and Solve are importable by name, but
left out of __all__, as other tuprolog modules export the same names.
Examples:
$ 2ppy-tui
See Also
tuprolog.gui, the GUI application the IDE renders.tuprolog.repl, for a command-line shell.
PANELS
module-attribute
¶
PANELS: tuple[Panel, ...] = (
"SOLUTIONS",
"STDIN",
"STDOUT",
"STDERR",
"WARNINGS",
"DIAGNOSTICS",
"OPERATORS",
"FLAGS",
"LIBRARIES",
"STATIC_KB",
"DYNAMIC_KB",
)
The panels the IDE shows, in order (as 2P-Kt's Swing IDE).
PrologTui
¶
Bases: App[None]
A terminal IDE for Prolog: pages of theories to edit and queries to solve against them.
It renders the state of 2P-Kt's GUI application, sends it actions, and fulfils its effects (file dialogs, reading and writing files, confirmations, clipboard), through a bridge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bridge
|
GuiBridge | None
|
The bridge to the GUI application; by default, a |
None
|
startup
|
Startup | None
|
What to open at startup; by default, a scratch page. |
None
|
render_state
async
¶
render_state(message: StateChanged) -> None
Renders a new snapshot (on Textual's event loop).
diagnostic_selected
¶
diagnostic_selected(event: OptionSelected) -> None
Moves the cursor of the editor where the selected diagnostic is.
flag_selected
¶
flag_selected(event: RowSelected) -> None
Asks for a new value of the selected flag.
timeout_changed
¶
timeout_changed(event: Submitted | Blurred) -> None
Sends the edited timeout to the model, if valid.
library_symbol_highlighted
¶
library_symbol_highlighted() -> None
Previews the documentation of the highlighted library, or symbol.
library_symbol_selected
¶
library_symbol_selected(event: NodeSelected) -> None
Shows the documentation of the selected symbol (libraries expand, or collapse).
documentation_pressed
¶
documentation_pressed(event: Pressed) -> None
Shows the documentation of what the panel of the button highlights.
action_documentation
¶
action_documentation() -> None
Shows the documentation of what the flags or libraries panel highlights, if shown.
add_operator
¶
add_operator() -> None
Asks for an operator, and declares it at the end of the theory (with op/3).
action_solve
¶
action_solve(mode: Mode) -> None
Solves the query of the current page, or asks for more solutions.
action_request_exit
¶
action_request_exit() -> None
Asks to quit (confirming if files have unsaved changes).
handle_effect
¶
handle_effect(message: EffectRequested) -> None
Fulfils an effect (on Textual's event loop).
GuiBridge
¶
Bases: Protocol
What the terminal IDE needs from a GUI application.
Callbacks may be called on any thread: the IDE forwards them to its own event loop.
start
¶
start(
on_state: Callable[[Snapshot], object],
on_effect: Callable[[Effect], object],
on_error: Callable[[str], object],
) -> None
Starts the application, which reports states, effects and errors to the callbacks.
is_timeout
¶
Whether text is a timeout (e.g. "2s 500ms", or "none"), for tuprolog.tui.ChangeTimeout.
JvmGuiBridge
¶
JvmGuiBridge(
profiles: Iterable[SolverProfile] | None = None,
timeout: float = 5.0,
flags: Mapping[str, str | Term] | None = None,
scheduling: Scheduling = "concurrent",
)
A GuiBridge running 2P-Kt's GUI application (a tuprolog.gui.GuiFacade), converting its JVM states and effects into tuprolog.tui snapshots and effects, and
actions the other way round.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profiles
|
Iterable[SolverProfile] | None
|
The solver profiles (see |
None
|
timeout
|
float
|
The default timeout of resolutions, in seconds. |
5.0
|
flags
|
Mapping[str, str | Term] | None
|
The flags of every solver (names to values, as terms or their Prolog text). |
None
|
scheduling
|
Scheduling
|
Whether resolutions of different pages may run concurrently
( |
'concurrent'
|
Resolution
dataclass
¶
Resolution(
status: str = "IDLE",
query: str | None = None,
solutions: tuple[Solution, ...] = (),
error: str | None = None,
can_solve: bool = True,
can_continue: bool = False,
can_stop: bool = False,
)
The current resolution of a page, and the commands it allows.
HistoryEntry
dataclass
¶
A concluded resolution.
SyntaxToken
dataclass
¶
A classified span of Prolog text, for syntax highlighting: category is the name of a
tuprolog.gui.SemanticCategory, start and end (exclusive) are 0-based (line, column).
DiagnosticEntry
dataclass
¶
A problem found in the theory, at a 0-based line and column if known.
LibraryEntry
dataclass
¶
LibraryEntry(
alias: str,
predicates: tuple[str, ...] = (),
operators: tuple[str, ...] = (),
functions: tuple[str, ...] = (),
help: str = "",
documentation: tuple[tuple[str, str], ...] = (),
)
A library of a solver: its alias, the predicates, operators and functions it defines, its
help, and the documentation of its symbols (as pairs of a symbol, as listed, and its help).
Helps are in Markdown.
Inspection
dataclass
¶
Inspection(
operators: tuple[tuple[int, str, str], ...] = (),
flags: tuple[tuple[str, str, str], ...] = (),
libraries: tuple[LibraryEntry, ...] = (),
static_kb: str = "",
dynamic_kb: str = "",
)
What the solver of a page holds: its operators (as (priority, specifier, name)), flags
(as (name, value, help), with help in Markdown), libraries, and static and dynamic knowledge bases (as Prolog text).
Page
dataclass
¶
Page(
id: str,
title: str,
source: str = "",
document_id: str | None = None,
dirty: bool = False,
query: str = "",
resolution: Resolution = Resolution(),
history: tuple[HistoryEntry, ...] = (),
stdin: str = "",
timeout: str = "5s",
stdout: str = "",
stderr: str = "",
warnings: tuple[str, ...] = (),
diagnostics: tuple[DiagnosticEntry, ...] = (),
inspection: Inspection = Inspection(),
unread: frozenset[Panel] = frozenset(),
)
A page: its source (from its scratch text or its document), query, resolution and panels.
Snapshot
dataclass
¶
Startup
dataclass
¶
Startup(
documents: tuple[tuple[Path, str], ...] = (),
theory: str | None = None,
query: str | None = None,
stdin: str | None = None,
solve: Mode | None = None,
)
What the IDE opens at startup (e.g. as asked on the command line).
It opens a page per document (a path, and its text: empty if the file does not exist yet),
then a scratch page with theory if given, or if no documents are; it gives each of these
pages query and stdin, if given, and finally solves the query of the selected page (the
last one) with solve, if given.
ExitConfirmed
dataclass
¶
ExitConfirmed()
NewScratchPage
dataclass
¶
NewDocumentPage
dataclass
¶
OpenDocumentLoaded
dataclass
¶
ClosePageDecision
dataclass
¶
ChangeScratchText
dataclass
¶
ChangeDocumentText
dataclass
¶
ChangeQuery
dataclass
¶
ChangeStdin
dataclass
¶
ChangeTimeout
dataclass
¶
ChangeFlag
dataclass
¶
RequestSave
dataclass
¶
SaveDestinationSelected
dataclass
¶
SaveSucceeded
dataclass
¶
SaveFailed
dataclass
¶
ReloadSucceeded
dataclass
¶
ReloadFailed
dataclass
¶
PickSaveDestination
dataclass
¶
WriteDocument
dataclass
¶
ReadDocument
dataclass
¶
ConfirmCloseDirtyPage
dataclass
¶
ConfirmReloadDirtyDocument
dataclass
¶
ConfirmExitWithDirtyDocuments
dataclass
¶
CopyToClipboard
dataclass
¶
main
¶
Runs the terminal IDE, as asked by the command-line arguments (see parser).
parse_command_line
¶
parse_command_line(
argv: Sequence[str] | None = None,
environ: Mapping[str, str] | None = None,
) -> tuple[JvmGuiBridge, Startup]
Parses the command-line arguments into a bridge to the GUI application, and a startup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
argv
|
Sequence[str] | None
|
The arguments; by default, those of the process. |
None
|
environ
|
Mapping[str, str] | None
|
The environment variables standing for arguments, before them (see
|
None
|
Returns:
| Type | Description |
|---|---|
JvmGuiBridge
|
The bridge (with the solver profile, flags, timeout and scheduling asked for) and the |
Startup
|
startup (the pages, query and standard input asked for). |
Raises:
| Type | Description |
|---|---|
SystemExit
|
If the arguments are invalid (after printing why), or after printing the help or the version. |