Crate terminal_cli [−] [src]

A helper library for implementing low-level terminal command line interfaces, like those on embedded bare-bones environments with UART ports.

The library doesn't use Rust's std library, but requires the alloc and collections libraries for heap memory allocation and vector manipulation.

Custom commands can be easily added by implementing the CliCommand trait.

# Example

 #![feature(convert)]
 let help = CliCommandKeyword {
    keyword: "help".to_string(),
    action: |line, cli| {
        cli.output_line("Help here!");
    }
 };

 // Adds "set time <HH:mm>" and "get time" commands. No parsing here.
 let time = CliPropertyVar {
    var_name: "time".to_string(),
    var_value: "11:15".to_string(),
    val_hint: "HH:mm".to_string(),
 
    var_output: |v| { v.to_string() },
    var_input: |v| {
        if v.len() > 0 { Some(v.to_string()) }
        else { None }
    }
 };
 
 // For this test, a ```stdout``` terminal. Isn't included in the 
 // library as it's meant for bare-bones usage - no std library.
 struct StdoutTerminal;
 impl CliTerminal for StdoutTerminal {
    fn output_line(&mut self, line: &str) {
        println!("{}", line);
    }
 }

 let mut commands = vec![
    Box::new(help) as Box<CliCommand>,
    Box::new(time) as Box<CliCommand>
 ];
 
 let mut term = StdoutTerminal;
 
 // Execute a line buffer
 cli_execute("help", commands.as_mut_slice(), &mut term);
 
 // Try to autocomplete the active buffer. Will return a summary of all commands.
 let autocomplete = cli_try_autocomplete("", commands.as_mut_slice());


 // Serial terminal input handling
 struct StdoutPromptOutput;
 impl CliPromptTerminal for StdoutPromptOutput {
        fn print_bytes(&self, bytes: &[u8]) {
            io::stdout().write(bytes);
        }
 }

 let mut prompt = CliPromptAutocompleteBuffer::new("#".to_string());
 // help + newline
 let keyboard_input = vec!['h' as u8, 'e' as u8, 'l' as u8, 
    0x7f /* backspace */, 'l' as u8, 'p' as u8, 0x0d /* \r */ ];
 
 for byte in keyboard_input {
        prompt.handle_received_byte(byte, &StdoutPromptOutput,
                                    commands.as_mut_slice(), &mut term);
 }

Structs

AutocompleteLine

One autocomplete suggestion

CliCommandKeyword

Simple keyword command, like help with no arguments.

CliPromptAutocompleteBuffer

Holds the current line buffer for a terminal and its possible autocomplete state.

CliPropertyFn

Retrieved property that can be changed with set var_name <value> and retrieved with get var_name. Useful for values that are changed by other parts of the system, like RTC clock or some other counter.

CliPropertyVar

Owned property that can be changed with set var_name <value> and retrieved with get var_name.

Enums

AutocompleteOption

A command's hints to the autocompletition

AutocompleteResult

Result of the autocomplete request on a given set of commands

Traits

CliCommand

A command that can be executed by the execution function.

CliPromptTerminal
CliTerminal

Terminal trait.

Functions

cli_execute

Execute the given line buffer with the set of commands.

cli_try_autocomplete

Collect autocomplete suggestions for this line buffer

format_in_columns

Formats the strings in autocomplete-style column notation. Adds spaces in between. Preserves the ordering. Last line will contain the newline sequence.

longest_common_prefix

A naive implementation. Can be implemented with a trie, but it's overkill here. http://en.wikipedia.org/wiki/LCP_array