Skip to main content

Console

Struct Console 

Source
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; help is 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. Args gives 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_HISTORY of 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_LINES lines: 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>

Source

pub fn new() -> Self

A console with no commands but help, an empty line and log.

Source

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.

Source

pub fn line(&self) -> &str

The line being typed.

Source

pub fn set_line(&mut self, line: &str)

Sets the line being typed, as if typed: its caret goes to its end.

Source

pub fn lines(&self) -> impl ExactSizeIterator<Item = &str> + '_

The log’s lines, the oldest first.

Source

pub fn print(&mut self, text: impl Display)

Prints text in the log, a line for each of its lines.

Source

pub fn print_error(&mut self, text: impl Display)

Prints text in red, a line for each of its lines.

Source

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.

Source

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

pub fn newer(&mut self)

Down: shows the line run after the one shown, or past the last, the line that was being typed.

Source

pub fn complete(&mut self)

Tab: completes the line’s last word, or lists what it could be, as Console says.

Source§

impl<C: Copy> Console<C>

Source

pub fn run_line<F>(&mut self, line: &str, run: F)
where F: FnMut(C, &Args<'_>, &mut ConsoleOut<'_>) -> Result<(), ConsoleError>,

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.

Source

pub fn show<F>(&mut self, ui: &mut Ui<'_>, run: F)
where F: FnMut(C, &Args<'_>, &mut ConsoleOut<'_>) -> Result<(), ConsoleError>,

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.

Trait Implementations§

Source§

impl<C> Default for Console<C>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C> Forget for Console<C>

§

impl<C> Freeze for Console<C>
where Vec<Command<C>>: Freeze,

§

impl<C> Move for Console<C>

§

impl<C> RefUnwindSafe for Console<C>
where Vec<Command<C>>: RefUnwindSafe,

§

impl<C> Send for Console<C>
where Vec<Command<C>>: Send,

§

impl<C> Sync for Console<C>
where Vec<Command<C>>: Sync,

§

impl<C> Unpin for Console<C>
where Vec<Command<C>>: Unpin,

§

impl<C> UnsafeUnpin for Console<C>
where Vec<Command<C>>: UnsafeUnpin,

§

impl<C> UnwindSafe for Console<C>
where Vec<Command<C>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.