From 0c54d96ee3f76ba82ab9c3e8c06ad723b3a75d4d Mon Sep 17 00:00:00 2001 From: fdai7381 Date: Wed, 25 Jan 2023 13:34:00 +0100 Subject: [PATCH] feat: add filename completion --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + src/main.rs | 21 +++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 766acb9..dc95538 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -375,6 +375,7 @@ dependencies = [ "once_cell", "regex", "rustyline", + "rustyline-derive", "shlex", "sysinfo", "uname", @@ -505,6 +506,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustyline-derive" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "107c3d5d7f370ac09efa62a78375f94d94b8a33c61d8c278b96683fb4dbf2d8d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "scopeguard" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1333d8e..1aa8263 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ dirs = "4.0" once_cell = "1.17" regex = "1.7" rustyline = "10.1" +rustyline-derive = "0.7" shlex = "1.1" sysinfo = "0.27" uname = "0.1" diff --git a/src/main.rs b/src/main.rs index f7ad6e4..e79b195 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,11 @@ use crate::builtins::BuiltinConfig; use crate::prompt::Prompt; use colored::Colorize; use execute::interpret; +use rustyline::completion::FilenameCompleter; use rustyline::config::Configurer; use rustyline::error::ReadlineError; -use rustyline::{Editor, Result}; +use rustyline::{CompletionType, Config, Editor, Result}; +use rustyline_derive::{Completer, Helper, Highlighter, Hinter, Validator}; use whoami::username; mod builtins; @@ -14,13 +16,28 @@ mod parse; mod preprocess; mod prompt; +#[derive(Completer, Helper, Highlighter, Hinter, Validator)] +struct EditorHelper { + #[rustyline(Completer)] + completer: FilenameCompleter, +} + fn main() -> Result<()> { let (ctrlc_send, ctrlc_recv) = crossbeam_channel::unbounded::<()>(); let _ = ctrlc::set_handler(move || { let _ = ctrlc_send.send(()); }); - let mut rl = Editor::<()>::new()?; + let config = Config::builder() + .completion_type(CompletionType::List) + .build(); + + let helper = EditorHelper { + completer: FilenameCompleter::new(), + }; + + let mut rl = Editor::with_config(config)?; + rl.set_helper(Some(helper)); rl.set_auto_add_history(true); let mut prompt = Prompt::new();