pub struct Console<C> { /* private fields */ }Expand description
A console: a command line under a log, in a window of its own. A
command is registered with its usage, which names it and its arguments,
a line of help, and command, the caller’s value for it (an enum of the
caller’s commands, or a function), which the console hands back with the
arguments when a line names it, for the caller to run.
- Lines are words split by whitespace, a double quote grouping
words (
spawn "big rock"),\"and\\in it a quote and a backslash. The first word names the command;helpis the console’s own, which lists the commands or says what one does. - Usages write each argument after the name: a word to type as it
is (
profile dump [path]), a value (<x>, or[path]if it may be left out), or a choice (<error|warn|info>). The console checks a line against its command’s usage before the command runs: each word typed, each choice one of its own (ignoring case, then spelt as the usage spells it), each required argument there, no more arguments than the usage has.Argsgives them, and reads a value as a number. - Completion: Tab completes the line’s last word, or the next after a space: the first from the commands’ names, the others from their slots in the usage, a word or a choice’s choices. One match is put in whole, a space after it; several as far as they agree, or, if they agree no further, listed in the log, a match a line. A value has no completions.
- History: each line run is kept, the last
CONSOLE_HISTORYof them, one the same as the line before it once; Up goes back through them, and Down forward, to the line being typed. - The log keeps the last
CONSOLE_LINESlines: each line run, after>, what the commands printed (ConsoleOut), their errors in red, and what the caller adds (Console::log_record): the engine’s log records, warnings in amber and errors in red. A line too long for the window is cut at its edge.
Implementations§
Source§impl<C> Console<C>
impl<C> Console<C>
Sourcepub fn register(
&mut self,
usage: &str,
help: &str,
command: C,
) -> Result<(), ConsoleError>
pub fn register( &mut self, usage: &str, help: &str, command: C, ) -> Result<(), ConsoleError>
Registers command under usage, whose first word is its name, with
help, a line of what it does; it replaces a command of that name.
help lists the commands in the order they were first registered.
§Errors
ConsoleError::BadUsage if the usage’s first word is not a name:
there is none, it is a placeholder or a choice, it has a quote, or
it is help.
Sourcepub fn set_line(&mut self, line: &str)
pub fn set_line(&mut self, line: &str)
Sets the line being typed, as if typed: its caret goes to its end.
Sourcepub fn lines(&self) -> impl ExactSizeIterator<Item = &str> + '_
pub fn lines(&self) -> impl ExactSizeIterator<Item = &str> + '_
The log’s lines, the oldest first.
Sourcepub fn print(&mut self, text: impl Display)
pub fn print(&mut self, text: impl Display)
Prints text in the log, a line for each of its lines.
Sourcepub fn print_error(&mut self, text: impl Display)
pub fn print_error(&mut self, text: impl Display)
Prints text in red, a line for each of its lines.
Sourcepub fn log_record(&mut self, record: &str)
pub fn log_record(&mut self, record: &str)
Adds a record of the engine’s log, as nadir_core::log::since gives
it (<time> <LEVEL> <target>: <message>), a line for each of its
lines: in amber for WARN, in red for ERROR.
Sourcepub fn older(&mut self)
pub fn older(&mut self)
Up: shows the line run before the one shown, keeping the line being
typed for Console::newer to come back to.
Source§impl<C: Copy> Console<C>
impl<C: Copy> Console<C>
Sourcepub fn run_line<F>(&mut self, line: &str, run: F)
pub fn run_line<F>(&mut self, line: &str, run: F)
Runs line as if it were typed and Enter pressed, but leaves the
line being typed as it is: brings the log to its newest lines, so
that what follows is in view, echoes the line in the log, after >,
keeps it in the history, and, if it names a command and its
arguments fit the command’s usage, calls run with the command, its
arguments and where to print. An error, the line’s or the one run
returns, is printed in red. A line of only whitespace does nothing.
Sourcepub fn show<F>(&mut self, ui: &mut Ui<'_>, run: F)
pub fn show<F>(&mut self, ui: &mut Ui<'_>, run: F)
Fills a window with the console: the log, as many of its lines as
fit, the newest at the bottom; and the command line under it, across
the window. The command line takes the keyboard when the console is
shown after a frame it was not, none of that frame’s typing or keys
reaching it: a key that showed the console, typing its character as
it is pressed, is not typed. With it, Enter runs the line, as
Console::run_line does, and empties it; Tab completes; Up and
Down go through the history; PageUp and PageDown scroll the log a
page, as the wheel over it does by three lines. Each is handled after
the typing before it and before what follows, as the input ends a
frame at each (DebugInput). A log scrolled up
stays on its lines as new ones come, until a line is run, and one at
its bottom shows the newest. The rows fit the window, which does not
scroll once its size is settled.