WIP: basic wordlist rendering
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::modules::source::FileDatabase;
|
||||
use crate::modules::source::{FileDatabase, WordSource};
|
||||
use crossterm::event::{
|
||||
self,
|
||||
Event::{self, Key},
|
||||
@@ -10,7 +10,7 @@ use ratatui::{
|
||||
text::Text,
|
||||
widgets::{Block, Borders, Padding, Paragraph},
|
||||
};
|
||||
use std::io;
|
||||
use std::{io, ops::Index};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum UiState {
|
||||
@@ -25,7 +25,7 @@ pub enum UiState {
|
||||
pub struct App {
|
||||
state: UiState,
|
||||
file_database: FileDatabase,
|
||||
scroll_position: u32,
|
||||
scroll_position: usize,
|
||||
word_filter: Option<String>,
|
||||
lang_filter: Option<String>,
|
||||
exit: bool,
|
||||
@@ -88,7 +88,8 @@ impl App {
|
||||
}
|
||||
};
|
||||
frame.render_widget(
|
||||
Paragraph::new(word_filter).block(Block::new().title("Word").borders(Borders::ALL)),
|
||||
Paragraph::new(word_filter.clone())
|
||||
.block(Block::new().title("Word").borders(Borders::ALL)),
|
||||
filters_layout[0],
|
||||
);
|
||||
|
||||
@@ -104,15 +105,31 @@ impl App {
|
||||
filters_layout[1],
|
||||
);
|
||||
|
||||
// FIXME: this is wrong, the list must reside in memory and only be updated when required,
|
||||
// this makes a new call every frame, insane
|
||||
let word_list = {
|
||||
if let Some(w_filter) = &self.word_filter
|
||||
&& let Some(l_filter) = &self.lang_filter
|
||||
{
|
||||
self.file_database
|
||||
.find_by_word_and_lang(&word_filter, &l_filter)
|
||||
} else if let Some(l_filter) = &self.lang_filter {
|
||||
self.file_database.find_by_lang(&l_filter)
|
||||
} else {
|
||||
self.file_database.get_all_words()
|
||||
}
|
||||
};
|
||||
frame.render_widget(
|
||||
Paragraph::new("Word list").block(Block::new().borders(Borders::ALL).title("Entries")),
|
||||
words_layout[0],
|
||||
);
|
||||
|
||||
frame.render_widget(
|
||||
Paragraph::new("Selected word description").block(Block::new().borders(Borders::ALL)),
|
||||
words_layout[1],
|
||||
);
|
||||
let selected_word = word_list.into_iter().nth(self.scroll_position);
|
||||
if let Some(w) = selected_word {
|
||||
frame.render_widget(
|
||||
Paragraph::new(w.description).block(Block::new().borders(Borders::ALL)),
|
||||
words_layout[1],
|
||||
);
|
||||
}
|
||||
|
||||
let help_text = Text::raw("(f)ilter | lan(g) | (d)elete | (e)dit | (a)dd | (q)uit ")
|
||||
.style(Style::new().fg(Color::DarkGray));
|
||||
@@ -137,10 +154,20 @@ impl App {
|
||||
self.word_filter = Some(c.to_string());
|
||||
}
|
||||
}
|
||||
UiState::FILTERLANG => {
|
||||
if let Some(mut prev_lang) = self.lang_filter.clone() {
|
||||
prev_lang.push(c);
|
||||
self.lang_filter = Some(prev_lang);
|
||||
} else {
|
||||
self.lang_filter = Some(c.to_string());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Quit
|
||||
if c == 'q' || c == 'x' {
|
||||
return Err(io::Error::last_os_error()); // FIXME: this is not right
|
||||
}
|
||||
// State change
|
||||
if c == 'f' || c == '/' {
|
||||
self.state = UiState::FILTERWORD;
|
||||
}
|
||||
@@ -156,8 +183,13 @@ impl App {
|
||||
if c == 'a' {
|
||||
self.state = UiState::ADD;
|
||||
}
|
||||
// TODO: remove, just for testing
|
||||
self.lang_filter = Some(format!("{0} >> {1}", c, key.code));
|
||||
// Movement
|
||||
if c == 'j' {
|
||||
self.scroll_position -= 1;
|
||||
}
|
||||
if c == 'k' {
|
||||
self.scroll_position += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user