WIP: edit word

This commit is contained in:
2026-08-06 23:19:00 +02:00
parent 7a8a1cb340
commit 311f2fe7b4
2 changed files with 115 additions and 7 deletions

View File

@@ -143,18 +143,26 @@ impl App {
}; };
// PERF: do not clone // PERF: do not clone
let selected_word = self.word_list.clone().into_iter().nth(scroll_position); let selected_word = self.word_list.clone().into_iter().nth(scroll_position);
if self.state == UiState::ADD { if self.state == UiState::ADD || self.state == UiState::EDIT {
let add_words_heading_layout = let add_words_heading_layout =
Layout::horizontal(vec![Constraint::Fill(1), Constraint::Length(8)]) Layout::horizontal(vec![Constraint::Fill(1), Constraint::Length(8)])
.spacing(1) .spacing(1)
.split(main_layout[0]); .split(main_layout[0]);
// PERF: maybe not clone // PERF: maybe not clone
let word_to_add = self.word_to_add.clone().unwrap_or(Word { let word_to_add = {
key: "".to_string(), if self.state == UiState::EDIT
description: "".to_string(), && let Some(w) = self.word_to_edit.clone()
lang: "".to_string(), {
}); w
} else {
self.word_to_add.clone().unwrap_or(Word {
key: "".to_string(),
description: "".to_string(),
lang: "".to_string(),
})
}
};
frame.render_widget( frame.render_widget(
Paragraph::new(word_to_add.key).block( Paragraph::new(word_to_add.key).block(
Block::new() Block::new()
@@ -309,6 +317,17 @@ impl App {
} }
} }
pub fn get_selected_word(&self) -> Option<Word> {
let scroll_position: usize = {
if let Some(i) = self.list_state.selected() {
i
} else {
0
}
};
self.word_list.clone().into_iter().nth(scroll_position)
}
fn handle_events(&mut self) -> io::Result<()> { fn handle_events(&mut self) -> io::Result<()> {
return handle_user_input(self); return handle_user_input(self);
} }

View File

@@ -6,7 +6,6 @@ use crate::modules::{
source::{Word, WordSource}, source::{Word, WordSource},
ui::app::{AddState, App, UiState}, ui::app::{AddState, App, UiState},
}; };
pub fn handle_user_input(app: &mut App) -> io::Result<()> { pub fn handle_user_input(app: &mut App) -> io::Result<()> {
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {
match key.code { match key.code {
@@ -93,6 +92,44 @@ pub fn handle_user_input(app: &mut App) -> io::Result<()> {
} }
} }
}, },
UiState::EDIT => match app.add_state {
AddState::KEY => {
if let Some(mut prev_word) = app.word_to_edit.clone() {
prev_word.key.push(c);
app.word_to_edit = Some(prev_word);
} else {
app.word_to_edit = Some(Word {
key: c.to_string(),
description: "".to_string(),
lang: "".to_string(),
});
}
}
AddState::DESCRIPTION => {
if let Some(mut prev_word) = app.word_to_edit.clone() {
prev_word.description.push(c);
app.word_to_edit = Some(prev_word);
} else {
app.word_to_add = Some(Word {
key: c.to_string(),
description: "".to_string(),
lang: "".to_string(),
});
}
}
AddState::LANG => {
if let Some(mut prev_word) = app.word_to_edit.clone() {
prev_word.lang.push(c);
app.word_to_edit = Some(prev_word);
} else {
app.word_to_edit = Some(Word {
key: c.to_string(),
description: "".to_string(),
lang: "".to_string(),
});
}
}
},
_ => { _ => {
// Quit // Quit
if c == 'q' || c == 'x' { if c == 'q' || c == 'x' {
@@ -110,6 +147,8 @@ pub fn handle_user_input(app: &mut App) -> io::Result<()> {
} }
if c == 'e' { if c == 'e' {
app.state = UiState::EDIT; app.state = UiState::EDIT;
let selected_word = app.get_selected_word();
app.word_to_edit = selected_word;
} }
if c == 'a' { if c == 'a' {
app.state = UiState::ADD; app.state = UiState::ADD;
@@ -128,6 +167,18 @@ pub fn handle_user_input(app: &mut App) -> io::Result<()> {
app.description_scroll = 0; app.description_scroll = 0;
} }
} }
if c == 'g' {
if let Some(g) = app.last_key {
match g.code {
event::KeyCode::Char(g_c) => {
if g_c == 'g' {
app.list_state.select_first();
}
}
_ => {}
}
}
}
if c == 'G' { if c == 'G' {
app.list_state.select_last(); app.list_state.select_last();
} }
@@ -162,12 +213,30 @@ pub fn handle_user_input(app: &mut App) -> io::Result<()> {
} }
} }
} }
UiState::EDIT => {
if let Some(w) = app.word_to_edit.clone()
&& w.key.len() > 0
{
let res = app.file_database.edit_word(w.clone());
match res {
Ok(_) => {
app.update_word_list();
app.word_to_edit = None;
app.state = get_next_ui_state(app.state);
}
Err(_) => app.state = get_next_ui_state(app.state),
}
}
}
_ => {} _ => {}
}, },
event::KeyCode::Tab => match app.state { event::KeyCode::Tab => match app.state {
UiState::ADD => { UiState::ADD => {
app.add_state = get_next_add_state(app.add_state); app.add_state = get_next_add_state(app.add_state);
} }
UiState::EDIT => {
app.add_state = get_next_add_state(app.add_state);
}
_ => { _ => {
app.state = get_next_ui_state(app.state); app.state = get_next_ui_state(app.state);
} }
@@ -201,6 +270,26 @@ pub fn handle_user_input(app: &mut App) -> io::Result<()> {
} }
} }
}, },
UiState::EDIT => match app.add_state {
AddState::KEY => {
if let Some(mut prev_word) = app.word_to_edit.clone() {
prev_word.key.pop();
app.word_to_edit = Some(prev_word);
}
}
AddState::DESCRIPTION => {
if let Some(mut prev_word) = app.word_to_edit.clone() {
prev_word.description.pop();
app.word_to_edit = Some(prev_word);
}
}
AddState::LANG => {
if let Some(mut prev_word) = app.word_to_edit.clone() {
prev_word.lang.pop();
app.word_to_edit = Some(prev_word);
}
}
},
_ => {} _ => {}
}, },
event::KeyCode::Esc => { event::KeyCode::Esc => {