Skip to content

Commit

Permalink
fix(repl): update keystroke feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
icanvardar committed Nov 3, 2024
1 parent 37b3de5 commit f46f4aa
Showing 1 changed file with 78 additions and 10 deletions.
88 changes: 78 additions & 10 deletions src/util/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::common::app_builder::{AppBuilder, Arbor};
pub struct Repl {
arbor: Arbor,
input: String,
input_section: usize,
selected_suggestion: usize,
}

Expand All @@ -24,6 +25,7 @@ impl Repl {
Ok(Self {
arbor: Arbor::build().await?,
input: "".to_string(),
input_section: 0,
selected_suggestion: 0,
})
}
Expand All @@ -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::<Vec<&str>>();

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 {
Expand All @@ -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::<Vec<&str>>()
.get(self.input_section)
.unwrap(),
)
.await?
.get(self.selected_suggestion)
{
self.input = suggestion.to_string()
let mut words = self.input.split(' ').collect::<Vec<&str>>();

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!"),
Expand All @@ -90,8 +150,16 @@ impl Repl {
let suggestions = self
.arbor
.autocomplete
.suggest_word(self.input.as_str())
.suggest_word(
self.input
.split(' ')
.collect::<Vec<&str>>()
.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;
Expand Down

0 comments on commit f46f4aa

Please sign in to comment.