Skip to main content

Shell

Struct Shell 

Source
pub struct Shell<'a> { /* private fields */ }
Expand description

The shell.

The type parameter 'a is the lifetime of state captured by command handlers registered with Shell::add_command.

§Example

use embassy_shell::Shell;

let mut shell = Shell::new();
shell.add_command("echo", "print arguments", |args, mut io| {
    Box::pin(async move { io.println(&args.rest()).await })
});

let mut input: &[u8] = b"echo hello world\r\n";
let mut output: Vec<u8> = Vec::new();
futures::executor::block_on(shell.run(&mut input, &mut output)).unwrap();

assert!(String::from_utf8_lossy(&output).contains("hello world"));

Implementations§

Source§

impl<'a> Shell<'a>

Source

pub fn new() -> Self

Create an empty shell with default settings.

Source

pub fn prompt(&mut self, prompt: &'static str) -> &mut Self

Set the prompt string.

Source

pub fn max_history(&mut self, n: usize) -> &mut Self

Set the maximum number of remembered history entries.

Source

pub fn add_command<F>(&mut self, name: &str, help: &'static str, handler: F)
where F: for<'x> Fn(Args<'x>, Io<'x>) -> BoxFuture<'x, Result<()>> + 'a,

Register a command.

name is what the user types at the prompt, help is the one-line description shown by help. handler receives the arguments and an output handle, and must return a boxed future (wrap an async move block in Box::pin) so that all commands share one concrete future type:

let mut shell = Shell::new();
shell.add_command("hello", "say hello", |args, mut io| {
    Box::pin(async move {
        let name = args.get(0).unwrap_or("world");
        io.println(&format!("Hello, {name}!")).await
    })
});
Source

pub fn add_command_with_options<F>( &mut self, name: &str, help: &'static str, options: &'static [&'static str], handler: F, )
where F: for<'x> Fn(Args<'x>, Io<'x>) -> BoxFuture<'x, Result<()>> + 'a,

Register a command whose first argument is completed from a fixed list of options (e.g. led on|off|blink).

Source

pub fn add_command_with_completer<F, C>( &mut self, name: &str, help: &'static str, handler: F, completer: C, )
where F: for<'x> Fn(Args<'x>, Io<'x>) -> BoxFuture<'x, Result<()>> + 'a, C: Fn(usize, &str) -> Vec<String> + 'a,

Register a command with a custom completer. The completer is called with the index of the argument being completed (>= 1) and the partial token, and returns candidate replacements for that token.

Source

pub fn command_names(&self) -> impl Iterator<Item = &str>

Names of all registered user commands.

Source

pub async fn run<R, W>(&mut self, reader: &mut R, writer: &mut W) -> Result<()>
where R: Read, W: Write,

Run the interactive shell until end of input.

reader and writer are the transport (UART, USB CDC, …). They may be any types implementing embedded_io_async::Read / embedded_io_async::Write.

Ctrl-C interrupts a running command (the handler future is dropped, so handlers should be cancel-safe). Bytes typed while a command runs are not echoed and only the most recent one is kept for the next line.

Trait Implementations§

Source§

impl Default for Shell<'_>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Shell<'a>

§

impl<'a> !Send for Shell<'a>

§

impl<'a> !Sync for Shell<'a>

§

impl<'a> !UnwindSafe for Shell<'a>

§

impl<'a> Freeze for Shell<'a>

§

impl<'a> Unpin for Shell<'a>

§

impl<'a> UnsafeUnpin for Shell<'a>

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.