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

rusty_prompt

Rusty 库,用于创建交互式运行时命令行提示

1 个不稳定版本

0.3.0 2023年10月2日

#17 in #suggestions

BSD-1-Clause

105KB
2.5K SLoC

Rusty Prompt

Rusty Prompt 是基于 go-prompt 库的命令行界面构建工具。

基本示例

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();
}

将输出一个提示

带有补全功能

当前快捷键

动作
结束 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

依赖项

~0.4–1MB
~20K SLoC