pub struct Terminal {
pub running: Arc<AtomicBool>,
pub prompt: Arc<Mutex<String>>,
pub term: Arc<Crossterm>,
pub handler: Arc<dyn Cli>,
pub terminate: Arc<AtomicBool>,
pub pipe_raw: Channel<String>,
pub pipe_crlf: Channel<String>,
pub pipe_ctl: DuplexChannel<()>,
pub para_width: Arc<AtomicUsize>,
/* private fields */
}Expand description
Terminal interface
Fields§
§running: Arc<AtomicBool>Set while a submitted command is being processed by the Cli handler.
prompt: Arc<Mutex<String>>The default prompt string rendered before each input line.
term: Arc<Crossterm>The underlying platform-specific terminal interface.
handler: Arc<dyn Cli>The command-line processor that handles submitted commands.
terminate: Arc<AtomicBool>When set, the terminal exits its run loop after the current command.
pipe_raw: Channel<String>Pipe for writing raw text (without newline conversion) to the terminal.
pipe_crlf: Channel<String>Pipe for writing lines (terminated with CRLF) to the terminal.
pipe_ctl: DuplexChannel<()>Duplex control channel used to start and stop the pipe processing task.
para_width: Arc<AtomicUsize>Fallback paragraph wrap width (in columns) when the terminal width is unknown.
Implementations§
Source§impl Terminal
impl Terminal
Sourcepub fn try_new(handler: Arc<dyn Cli>, prompt: &str) -> Result<Self>
pub fn try_new(handler: Arc<dyn Cli>, prompt: &str) -> Result<Self>
Create a new default terminal instance bound to the supplied command-line processor Cli.
Sourcepub fn try_new_with_options(
handler: Arc<dyn Cli>,
options: Options,
) -> Result<Self>
pub fn try_new_with_options( handler: Arc<dyn Cli>, options: Options, ) -> Result<Self>
Create a new terminal instance bound to the supplied command-line processor Cli.
Receives options::Options that allow terminal customization.
Sourcepub fn inner(&self) -> LockResult<MutexGuard<'_, Inner>>
pub fn inner(&self) -> LockResult<MutexGuard<'_, Inner>>
Access to the underlying terminal instance
Sourcepub fn history(&self) -> Vec<UnicodeString>
pub fn history(&self) -> Vec<UnicodeString>
Get terminal command line history list as Vec<String>
Sourcepub fn reset_line_buffer(&self)
pub fn reset_line_buffer(&self)
Clear the current input line buffer and reset the cursor to the start.
Sourcepub fn get_prompt(&self) -> String
pub fn get_prompt(&self) -> String
Get the current terminal prompt string
Sourcepub fn refresh_prompt(&self)
pub fn refresh_prompt(&self)
Refreshes the prompt and the user input buffer. This function is useful when the prompt is handled externally and contains data that should be updated.
Sourcepub fn para<S>(&self, text: S)
pub fn para<S>(&self, text: S)
Write text to the terminal as a paragraph, word-wrapped to the
current terminal width (falling back to Terminal::para_width).
Sourcepub fn para_with_options<'a, S, Opt>(&self, width_or_options: Opt, text: S)
pub fn para_with_options<'a, S, Opt>(&self, width_or_options: Opt, text: S)
Write text to the terminal as a paragraph, word-wrapped using the
supplied width or textwrap::Options.
Sourcepub fn help<S: ToString, H: ToString>(
&self,
list: &[(S, H)],
separator: Option<&str>,
) -> Result<()>
pub fn help<S: ToString, H: ToString>( &self, list: &[(S, H)], separator: Option<&str>, ) -> Result<()>
Render a formatted, alphabetically-sorted help listing of
(command, description) pairs, wrapping descriptions to fit the
terminal width. separator (default a single space) is placed between
each command and its description.
Sourcepub async fn run(self: &Arc<Self>) -> Result<()>
pub async fn run(self: &Arc<Self>) -> Result<()>
Execute the async terminal processing loop.
Once started, it should be stopped using
Terminal::exit
Sourcepub async fn ask(
self: &Arc<Terminal>,
secret: bool,
prompt: &str,
) -> Result<String>
pub async fn ask( self: &Arc<Terminal>, secret: bool, prompt: &str, ) -> Result<String>
Ask a question (input a string until CRLF).
secret argument suppresses echoing of the
user input (useful for password entry)
Sourcepub async fn kbhit(self: &Arc<Terminal>, prompt: Option<&str>) -> Result<String>
pub async fn kbhit(self: &Arc<Terminal>, prompt: Option<&str>) -> Result<String>
Wait for a single keystroke, optionally displaying prompt first, and
return the captured key without echoing it.
Sourcepub fn inject_unicode_string(&self, text: UnicodeString) -> Result<()>
pub fn inject_unicode_string(&self, text: UnicodeString) -> Result<()>
Inject a string into the current cursor position
Sourcepub fn inject<S: ToString>(&self, text: S) -> Result<()>
pub fn inject<S: ToString>(&self, text: S) -> Result<()>
Insert text into the current input line at the cursor position.
Sourcepub fn inject_char(&self, ch: char) -> Result<()>
pub fn inject_char(&self, ch: char) -> Result<()>
Insert a single character into the current input line at the cursor position.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Sourcepub async fn exec<S: ToString>(self: &Arc<Terminal>, cmd: S) -> Result<()>
pub async fn exec<S: ToString>(self: &Arc<Terminal>, cmd: S) -> Result<()>
Submit cmd to the Cli handler for processing, printing any error
it returns and then either exiting (if termination was requested) or
re-rendering the prompt.
Sourcepub fn set_theme(&self, _theme: Theme) -> Result<()>
pub fn set_theme(&self, _theme: Theme) -> Result<()>
Apply the supplied color Theme (effective on the WASM/xterm.js target).
Sourcepub fn update_theme(&self) -> Result<()>
pub fn update_theme(&self) -> Result<()>
Re-apply the current theme to the terminal (effective on the WASM/xterm.js target).
Sourcepub fn clipboard_copy(&self) -> Result<()>
pub fn clipboard_copy(&self) -> Result<()>
Copy the current terminal selection to the system clipboard (effective on the WASM/xterm.js target).
Sourcepub fn clipboard_paste(&self) -> Result<()>
pub fn clipboard_paste(&self) -> Result<()>
Paste the system clipboard contents into the terminal (effective on the WASM/xterm.js target).
Sourcepub fn increase_font_size(&self) -> Result<Option<f64>>
pub fn increase_font_size(&self) -> Result<Option<f64>>
Increase the terminal font size, returning the new size if applicable.
Sourcepub fn decrease_font_size(&self) -> Result<Option<f64>>
pub fn decrease_font_size(&self) -> Result<Option<f64>>
Decrease the terminal font size, returning the new size if applicable.
Sourcepub fn set_font_size(&self, font_size: f64) -> Result<()>
pub fn set_font_size(&self, font_size: f64) -> Result<()>
Set the terminal font size to font_size.
Sourcepub fn get_font_size(&self) -> Result<Option<f64>>
pub fn get_font_size(&self) -> Result<Option<f64>>
Return the current terminal font size, if known.
Sourcepub async fn select<T>(
self: &Arc<Terminal>,
prompt: &str,
list: &[T],
) -> Result<Option<T>>
pub async fn select<T>( self: &Arc<Terminal>, prompt: &str, list: &[T], ) -> Result<Option<T>>
Prompt the user to choose one item from list by its index. Returns
None for an empty list, the sole item for a single-element list, and
otherwise repeatedly displays the choices until a valid index is entered;
pressing enter on an empty line aborts with Error::UserAbort.
Sourcepub fn register_event_handler(
self: &Arc<Self>,
_handler: EventHandlerFn,
) -> Result<()>
pub fn register_event_handler( self: &Arc<Self>, _handler: EventHandlerFn, ) -> Result<()>
Register a handler invoked on terminal Events such as copy and paste
(effective on the WASM/xterm.js target).
Sourcepub fn register_link_matcher(
&self,
_regexp: &RegExp,
_handler: LinkMatcherHandlerFn,
) -> Result<()>
pub fn register_link_matcher( &self, _regexp: &RegExp, _handler: LinkMatcherHandlerFn, ) -> Result<()>
Register a handler invoked when terminal text matching _regexp is
clicked (effective on the WASM/xterm.js target).