#suggestions #prompt #command-line #interactive #command-line-interface #run-time #cursor

rusty_prompt

Rusty library for creating interactive runtime command line prompts

1 unstable release

0.3.0 Oct 2, 2023

#14 in #suggestions

BSD-1-Clause

105KB
2.5K SLoC

Rusty Prompt

Rusty Prompt is a command line interface builder based off of the go-prompt library.

Basic example:

use rusty_prompt::{App, Color, CommandState, Completer, Document, Executor, Prompt, PromptBuilder, Suggestion, Writer};
use rusty_prompt::colored_string::{ColoredChar, ColoredString};

pub struct Cli {}

impl Completer for Cli {
    fn get_suggestions(&self, doc: Document) -> Vec<Suggestion> {
        vec![
            Suggestion::new("find", "Find something"),
            Suggestion::new("go", "Go somewhere"),
            Suggestion::new("die", "kills something"),
            Suggestion::new("flame", "flames something"),
            Suggestion::new("Blank_Desc", ""),
        ]
    }
}

impl Executor for Cli {
    fn execute(&mut self, command: &str, writer: &mut Writer) -> CommandState {
        writer.write_str(&format!("Command: {}", command));
        writer.write_str("\n");
        CommandState::Ok
    }
}

impl App for Cli {}

fn main() {
    let chr = ColoredChar::builder().ch('>' as u8).foreground_color(Color::Yellow).build();
    let prefix = ColoredString::new_from_inner(&[
        chr, chr, chr,
        ColoredChar::builder().ch(' ' as u8).build()
    ]);
    let mut prompt: Prompt = PromptBuilder::builder().prefix(prefix).app(Box::new(Cli {})).build().into();
    prompt.run();
}

Which will output a prompt:

With Completions:

Current keybinds:

Key Action
End Moves cursor to end
Home Moves cursor to start
Delete Deletes the next char
Backspace Deletes the prev char
Right Moves cursor to the right
Left Moves cursor to the left
Control+E Moves cursor to end
Control+A Moves cursor to start
Control+X Deletes the char after the cursor
Control+U Deletes all chars before cursor
Control+H Deletes the char before the cursor
Control+F Moves cursor to the right
Control+B Moves cursor to the left
Control+W Removes the chars before the cursor
Control+L Clears the current line
Control+D Quit
Control+C Quit
Up Gets the previous history and puts it in the line
Down Gets the next history and puts it in the line
Tab Completes the line

Dependencies

~0.3–0.8MB
~19K SLoC