From 7be9300f149974ddbce9df22c06bffd3a393b0b3 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 21 May 2024 16:52:29 -0400 Subject: [PATCH] Update CLI --- dust-shell/src/cli.rs | 87 +++--------------------------------------- dust-shell/src/main.rs | 2 +- 2 files changed, 6 insertions(+), 83 deletions(-) diff --git a/dust-shell/src/cli.rs b/dust-shell/src/cli.rs index 5b9a647..0bf2ab0 100644 --- a/dust-shell/src/cli.rs +++ b/dust-shell/src/cli.rs @@ -7,18 +7,15 @@ use std::{ }; use ariadne::sources; -use dust_lang::{ - context::{Context, ValueData}, - *, -}; +use dust_lang::{context::Context, *}; use nu_ansi_term::{Color, Style}; use reedline::{ - default_emacs_keybindings, ColumnarMenu, Completer, DefaultHinter, EditCommand, Emacs, KeyCode, - KeyModifiers, MenuBuilder, Prompt, Reedline, ReedlineEvent, ReedlineMenu, Signal, Span, - SqliteBackedHistory, Suggestion, + default_emacs_keybindings, ColumnarMenu, DefaultHinter, EditCommand, Emacs, KeyCode, + KeyModifiers, MenuBuilder, Prompt, Reedline, ReedlineEvent, ReedlineMenu, Signal, + SqliteBackedHistory, }; -pub fn run_shell(context: Context) -> Result<(), io::Error> { +pub fn run_shell<'a>(context: Context<'a>) -> Result<(), io::Error> { let mut interpreter = Interpreter::new(context.clone()); let mut keybindings = default_emacs_keybindings(); @@ -52,14 +49,12 @@ pub fn run_shell(context: Context) -> Result<(), io::Error> { .expect("Error loading history."), ); let hinter = Box::new(DefaultHinter::default().with_style(Style::new().dimmed())); - let completer = DustCompleter::new(context.clone()); let mut line_editor = Reedline::create() .with_edit_mode(edit_mode) .with_history(history) .with_hinter(hinter) .use_kitty_keyboard_enhancement(true) - .with_completer(Box::new(completer)) .with_menu(ReedlineMenu::EngineCompleter(Box::new( ColumnarMenu::default() .with_name("context menu") @@ -171,75 +166,3 @@ impl Prompt for StarshipPrompt { Cow::Borrowed("") } } - -pub struct DustCompleter { - context: Context, -} - -impl DustCompleter { - fn new(context: Context) -> Self { - DustCompleter { context } - } -} - -impl Completer for DustCompleter { - fn complete(&mut self, line: &str, pos: usize) -> Vec { - let mut suggestions = Vec::new(); - let last_word = if let Some(word) = line.rsplit([' ', ':']).next() { - word - } else { - line - }; - - if let Ok(path) = PathBuf::try_from(last_word) { - if let Ok(read_dir) = path.read_dir() { - for entry in read_dir { - if let Ok(entry) = entry { - let description = if let Ok(file_type) = entry.file_type() { - if file_type.is_dir() { - "directory" - } else if file_type.is_file() { - "file" - } else if file_type.is_symlink() { - "symlink" - } else { - "unknown" - } - } else { - "unknown" - }; - - suggestions.push(Suggestion { - value: entry.path().to_string_lossy().to_string(), - description: Some(description.to_string()), - extra: None, - span: Span::new(pos - last_word.len(), pos), - append_whitespace: false, - style: None, - }); - } - } - } - } - - for (key, (value_data, _)) in self.context.inner().unwrap().iter() { - let description = match value_data { - ValueData::Value(value) => value.to_string(), - ValueData::Type(r#type) => r#type.to_string(), - }; - - if key.as_str().contains(last_word) { - suggestions.push(Suggestion { - value: key.to_string(), - description: Some(description), - extra: None, - span: Span::new(pos - last_word.len(), pos), - append_whitespace: false, - style: None, - }); - } - } - - suggestions - } -} diff --git a/dust-shell/src/main.rs b/dust-shell/src/main.rs index 376e1e0..09fdfad 100644 --- a/dust-shell/src/main.rs +++ b/dust-shell/src/main.rs @@ -48,7 +48,7 @@ fn main() { .init(); let args = Args::parse(); - let context = Context::new(); + let context = Context::new(None); let mut interpreter = Interpreter::new(context.clone()); interpreter.load_std().unwrap();