From f46f4aa12d491dffd29337e3a09872ddac233af3 Mon Sep 17 00:00:00 2001 From: Can Vardar Date: Sun, 3 Nov 2024 20:03:47 +0300 Subject: [PATCH] fix(repl): update keystroke feedback --- src/util/repl.rs | 88 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 10 deletions(-) diff --git a/src/util/repl.rs b/src/util/repl.rs index 05de46f..5abed65 100644 --- a/src/util/repl.rs +++ b/src/util/repl.rs @@ -16,6 +16,7 @@ use crate::common::app_builder::{AppBuilder, Arbor}; pub struct Repl { arbor: Arbor, input: String, + input_section: usize, selected_suggestion: usize, } @@ -24,6 +25,7 @@ impl Repl { Ok(Self { arbor: Arbor::build().await?, input: "".to_string(), + input_section: 0, selected_suggestion: 0, }) } @@ -38,27 +40,68 @@ impl Repl { if event::poll(Duration::from_millis(100))? { if let Event::Key(event) = event::read()? { match event.code { + KeyCode::Char(' ') => { + if self.input.len() == 0 { + continue; + } + + self.input.push(' '); + self.input_section += 1; + + if self + .input + .chars() + .nth(self.input.len().saturating_sub(2)) + .unwrap() + == ' ' + { + self.input.pop(); + self.input_section -= 1; + } + } KeyCode::Char('c') if event.modifiers.contains(KeyModifiers::CONTROL) => { break } - KeyCode::Char(' ') => { - println!("nonono"); - } KeyCode::Char(c) => { self.input.push(c); self.selected_suggestion = 0; } KeyCode::Backspace => { + if self.input.len() == 0 { + continue; + } + + let curr = self + .input + .chars() + .nth(self.input.len().saturating_sub(1)) + .unwrap(); + + if curr == ' ' { + self.input_section -= 1; + } + self.input.pop(); self.selected_suggestion = 0; } KeyCode::Enter => { - self.arbor - .autocomplete - .insert_word(self.input.clone()) - .await?; + let words = self.input.split(' ').collect::>(); + + for word in words { + // word length must be bigger than 1 character + if word.len() < 2 { + continue; + } + + self.arbor + .autocomplete + .insert_word(word.to_string()) + .await?; + } + self.input = "".to_string(); self.selected_suggestion = 0; + self.input_section = 0; } KeyCode::Up => { if self.selected_suggestion > 0 { @@ -69,17 +112,34 @@ impl Repl { self.selected_suggestion += 1; } KeyCode::Tab => { + if self.input.len() == 0 { + continue; + } + if let Some(suggestion) = self .arbor .autocomplete - .suggest_word(self.input.as_str()) + .suggest_word( + self.input + .split(' ') + .collect::>() + .get(self.input_section) + .unwrap(), + ) .await? .get(self.selected_suggestion) { - self.input = suggestion.to_string() + let mut words = self.input.split(' ').collect::>(); + + words[self.input_section] = suggestion.as_str(); + + self.input = words.join(" "); } self.selected_suggestion = 0; + + self.input.push(' '); + self.input_section += 1; } KeyCode::Esc => break, _ => panic!("Unknown keystroke!"), @@ -90,8 +150,16 @@ impl Repl { let suggestions = self .arbor .autocomplete - .suggest_word(self.input.as_str()) + .suggest_word( + self.input + .split(' ') + .collect::>() + .get(self.input_section) + .unwrap(), + ) .await?; + + // NOTE: this is to prevent selection overflow let max_index = suggestions.len().saturating_sub(1); if self.selected_suggestion > max_index { self.selected_suggestion = max_index;