feat: more selection keybinds

also deduplicates code into State::populate_view
This commit is contained in:
electria 2026-08-27 10:28:49 -07:00
commit 7aafbe2207
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -29,6 +29,9 @@ enum Message {
SelectionUp, SelectionUp,
SelectionRight, SelectionRight,
SelectionTop,
SelectionBottom,
ChangeQuery(String), ChangeQuery(String),
Copy, Copy,
@ -108,11 +111,8 @@ impl State {
match result { match result {
Ok(db) => { Ok(db) => {
self.error = "".into(); self.error = "".into();
self.db_view = db
.iter_all_entries()
.map(|entry_ref| entry_ref.clone())
.collect();
self.db = Some(db); self.db = Some(db);
self.populate_view();
self.db_loaded = true; self.db_loaded = true;
} }
Err(e) => self.error = e.into(), Err(e) => self.error = e.into(),
@ -140,19 +140,12 @@ impl State {
} }
Message::SelectionRight => self.selected_field.next(), Message::SelectionRight => self.selected_field.next(),
Message::SelectionTop => self.selected_entry = 0,
Message::SelectionBottom => self.selected_entry = self.db_view.len() - 1,
Message::ChangeQuery(str) => { Message::ChangeQuery(str) => {
self.query = str; self.query = str;
self.db_view = self self.populate_view();
.db
.as_ref()
.unwrap()
.iter_all_entries()
.filter_map(|entry_ref| {
entry_ref
.contains(&self.query)
.then(|| entry_ref.to_owned())
})
.collect();
} }
Message::Copy => { Message::Copy => {
@ -358,6 +351,14 @@ impl State {
Some(Message::SelectionRight) Some(Message::SelectionRight)
} }
Key::Character("g") => Some(if modifiers.shift() {
Message::SelectionBottom
} else {
Message::SelectionTop
}),
Key::Named(key::Named::Home) => Some(Message::SelectionTop),
Key::Named(key::Named::End) => Some(Message::SelectionBottom),
Key::Character("y" | "c") if db_loaded => Some(Message::Copy), Key::Character("y" | "c") if db_loaded => Some(Message::Copy),
Key::Character("q") => Some(Message::Quit), Key::Character("q") => Some(Message::Quit),
@ -393,6 +394,23 @@ impl State {
Message::DbOpened, Message::DbOpened,
) )
} }
fn populate_view(&mut self) {
self.db_view = self
.db
.as_ref()
.unwrap()
.iter_all_entries()
.filter_map(|entry_ref| {
// this matches everything when the query is an empty string,
// which is the desired behavior so that it displays everything
// (when search isn't being used)
entry_ref
.contains(&self.query)
.then(|| entry_ref.to_owned())
})
.collect();
}
} }
pub fn run() -> Result<(), iced::Error> { pub fn run() -> Result<(), iced::Error> {