feat: delete + styling
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::modules::source::{FileDatabase, Word, WordSource};
|
||||
use crate::modules::ui::app::UiState::FILTERLANG;
|
||||
use crate::modules::ui::handler::handle_user_input;
|
||||
use crossterm::event::KeyEvent;
|
||||
use ratatui::{
|
||||
DefaultTerminal, Frame,
|
||||
layout::{Constraint, Direction, Flex, Layout},
|
||||
@@ -42,6 +43,7 @@ pub struct App {
|
||||
pub word_to_edit: Option<Word>,
|
||||
pub word_filter: Option<String>,
|
||||
pub lang_filter: Option<String>,
|
||||
pub last_key: Option<KeyEvent>,
|
||||
pub exit: bool,
|
||||
}
|
||||
|
||||
@@ -60,6 +62,7 @@ impl App {
|
||||
word_to_edit: None,
|
||||
word_filter: None,
|
||||
lang_filter: None,
|
||||
last_key: None,
|
||||
exit: false,
|
||||
}
|
||||
}
|
||||
@@ -131,6 +134,15 @@ impl App {
|
||||
/*
|
||||
* Word list / add / edit block
|
||||
* */
|
||||
let scroll_position: usize = {
|
||||
if let Some(i) = self.list_state.selected() {
|
||||
i
|
||||
} else {
|
||||
0
|
||||
}
|
||||
};
|
||||
// PERF: do not clone
|
||||
let selected_word = self.word_list.clone().into_iter().nth(scroll_position);
|
||||
if self.state == UiState::ADD {
|
||||
let add_words_heading_layout =
|
||||
Layout::horizontal(vec![Constraint::Fill(1), Constraint::Length(8)])
|
||||
@@ -171,11 +183,23 @@ impl App {
|
||||
let highligh_symbol: &str = {
|
||||
if self.state == UiState::LIST {
|
||||
"> "
|
||||
} else if self.state == UiState::DELETE {
|
||||
"x "
|
||||
} else {
|
||||
" "
|
||||
}
|
||||
};
|
||||
|
||||
let highligh_color: Color = {
|
||||
if self.state == UiState::LIST {
|
||||
Color::Yellow
|
||||
} else if self.state == UiState::DELETE {
|
||||
Color::LightRed
|
||||
} else {
|
||||
Color::default()
|
||||
}
|
||||
};
|
||||
|
||||
let words_layout =
|
||||
Layout::horizontal(vec![Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(main_layout[1]);
|
||||
@@ -194,21 +218,12 @@ impl App {
|
||||
.borders(Borders::ALL)
|
||||
.padding(Padding::horizontal(1)),
|
||||
)
|
||||
.highlight_style(Style::default().fg(Color::default()))
|
||||
.highlight_style(Style::default().fg(highligh_color))
|
||||
.highlight_symbol(highligh_symbol)
|
||||
.style(Style::default().fg(Color::DarkGray));
|
||||
frame.render_stateful_widget(word_key_list, words_layout[0], &mut self.list_state);
|
||||
|
||||
let scroll_position: usize = {
|
||||
if let Some(i) = self.list_state.selected() {
|
||||
i
|
||||
} else {
|
||||
0
|
||||
}
|
||||
};
|
||||
// PERF: do not clone
|
||||
let selected_word = self.word_list.clone().into_iter().nth(scroll_position);
|
||||
if let Some(w) = selected_word {
|
||||
if let Some(w) = selected_word.clone() {
|
||||
frame.render_widget(
|
||||
Paragraph::new(w.description)
|
||||
.wrap(Wrap { trim: true })
|
||||
@@ -226,8 +241,18 @@ impl App {
|
||||
.flex(Flex::Center)
|
||||
.split(main_layout[2]);
|
||||
|
||||
let help_text = Text::raw("(f)ilter | (l)ang | (d)elete | (e)dit | (a)dd | (q)uit ")
|
||||
.style(Style::new().fg(Color::DarkGray));
|
||||
let help_text = {
|
||||
if self.state == UiState::DELETE
|
||||
&& let Some(s_w) = selected_word.clone()
|
||||
{
|
||||
Text::raw(format!("Sure you want to delete '{0}'? (y)es", s_w.key))
|
||||
.style(Style::new().fg(Color::LightRed))
|
||||
} else {
|
||||
Text::raw("(f)ilter | (l)ang | (d)elete | (e)dit | (a)dd | (q)uit ")
|
||||
.style(Style::new().fg(Color::DarkGray))
|
||||
}
|
||||
};
|
||||
|
||||
let help_area = help_layout[0].centered(
|
||||
Constraint::Length(help_text.width() as u16),
|
||||
Constraint::Length(1),
|
||||
|
||||
Reference in New Issue
Block a user