Cli

Struct Cli 

Source
pub struct Cli { /* private fields */ }
Expand description

Provide APIs to interact with the Command Line Interface

Implementations§

Source§

impl Cli

Source

pub fn new() -> Result<Self>

Create a new Command Line Interface.

Note that it configures the terminal in character mode.

Examples found in repository?
examples/hello.rs (line 5)
4async fn main() -> eyre::Result<()> {
5    let mut cli = Cli::new()?;
6    let mut exit = false;
7
8    while !exit {
9        let action = cli.getaction().await?;
10        match action {
11            Action::Command(cmd) => runcmd(cmd, &mut exit),
12            Action::AutoComplete(cmd) => autocomplete(&mut cli, cmd)?,
13            Action::NoAction => exit = true,
14        };
15    }
16
17    Ok(())
18}
Source

pub async fn getaction(&mut self) -> Result<Action>

Return an Action demanded by the user in CLI.

Examples found in repository?
examples/hello.rs (line 9)
4async fn main() -> eyre::Result<()> {
5    let mut cli = Cli::new()?;
6    let mut exit = false;
7
8    while !exit {
9        let action = cli.getaction().await?;
10        match action {
11            Action::Command(cmd) => runcmd(cmd, &mut exit),
12            Action::AutoComplete(cmd) => autocomplete(&mut cli, cmd)?,
13            Action::NoAction => exit = true,
14        };
15    }
16
17    Ok(())
18}
Source

pub fn autocomplete(&mut self, words: &Vec<String>) -> Result<()>

Auto-complete the current command with the provided list of possible words

The word list should only contains possible words for the current input. This function does not filter out the word list, and expect all words in the list to start with current input.
Examples found in repository?
examples/hello.rs (line 52)
41fn autocomplete(cli: &mut Cli, cmd: Vec<String>) -> eyre::Result<()> {
42    let mut cmdlist = vec![
43        String::from("hello"),
44        String::from("upper"),
45        String::from("exit"),
46        String::from("help"),
47    ];
48
49    if cmd.len() == 1 {
50        cmdlist.retain(|x| x.starts_with(&cmd[0]));
51        // autocomplete command
52        cli.autocomplete(&cmdlist)?;
53    } else if cmd.len() > 1 {
54        // autocomplete arguments
55    }
56    Ok(())
57}
Source

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

Set the name of the prompt

Trait Implementations§

Source§

impl Drop for Cli

Source§

fn drop(&mut self)

Release Cli ressources and configure back the terminal in its orignal state.

Auto Trait Implementations§

§

impl Freeze for Cli

§

impl RefUnwindSafe for Cli

§

impl Send for Cli

§

impl Sync for Cli

§

impl Unpin for Cli

§

impl UnwindSafe for Cli

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.