Minor refactor with if-let guards (#145)

`if let` guards were stabilized in 1.95. The code concerning the TODO has been refactored.
This commit is contained in:
Adam Martinez
2026-04-19 18:19:58 +02:00
committed by GitHub
parent 1ed03e94fb
commit 7448fc73c5
+23 -38
View File
@@ -701,31 +701,28 @@ impl Tui {
match ev { match ev {
Event::Key(key) => { Event::Key(key) => {
match key.code { match key.code {
KeyCode::Char(c) => { KeyCode::Char(c)
// TODO: refactor back to `if let` arm guards when those are stabilized
if let BottomMessage::Input(InputCommand::Search(ref mut term)) = if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg self.bottom_msg =>
{ {
term.push(c); term.push(c);
return Some(InputAction::Redraw); InputAction::Redraw.into()
} }
KeyCode::Char(c)
if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) = if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) =
self.bottom_msg self.bottom_msg && matches!(c, 'g' if self.is_kitty) =>
{ c.to_digit(10).map(|input_num| {
if c == 'g' && self.is_kitty {
self.update_zoom(Zoom::pan_bottom);
self.set_msg(MessageSetting::Pop);
return Some(InputAction::Redraw);
}
return c.to_digit(10).map(|input_num| {
*page = (*page * 10) + input_num as usize; *page = (*page * 10) + input_num as usize;
InputAction::Redraw InputAction::Redraw
}); }),
KeyCode::Char(_)
if let BottomMessage::Input(InputCommand::GoToPage(_)) =
self.bottom_msg =>
{
self.set_msg(MessageSetting::Pop);
self.update_zoom(Zoom::pan_bottom)
} }
KeyCode::Char(c) => match c {
match c {
'l' => self.change_page(PageChange::Next, ChangeAmount::Single), 'l' => self.change_page(PageChange::Next, ChangeAmount::Single),
'j' => self.change_page(PageChange::Next, ChangeAmount::WholeScreen), 'j' => self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
'h' => self.change_page(PageChange::Prev, ChangeAmount::Single), 'h' => self.change_page(PageChange::Prev, ChangeAmount::Single),
@@ -749,10 +746,10 @@ impl Tui {
Some(InputAction::Redraw) Some(InputAction::Redraw)
} }
'f' => Some(InputAction::Fullscreen), 'f' => Some(InputAction::Fullscreen),
'n' if self.page < self.rendered.len() - 1 => {
// TODO: If we can't find one, then maybe like block until we've verified // TODO: If we can't find one, then maybe like block until we've verified
// all the pages have been checked? // all the pages have been checked?
self.rendered[(self.page + 1)..] 'n' if self.page < self.rendered.len() - 1 => self.rendered
[(self.page + 1)..]
.iter() .iter()
.enumerate() .enumerate()
.find_map(|(idx, p)| { .find_map(|(idx, p)| {
@@ -761,13 +758,8 @@ impl Tui {
.then_some(self.page + 1 + idx) .then_some(self.page + 1 + idx)
}) })
.map(|next_page| { .map(|next_page| {
jump_to_page( jump_to_page(&mut self.page, &mut self.last_render.rect, next_page)
&mut self.page, }),
&mut self.last_render.rect,
next_page
)
})
}
'N' if self.page > 0 => self.rendered[..(self.page)] 'N' if self.page > 0 => self.rendered[..(self.page)]
.iter() .iter()
.rev() .rev()
@@ -778,11 +770,7 @@ impl Tui {
.then_some(self.page - (idx + 1)) .then_some(self.page - (idx + 1))
}) })
.map(|prev_page| { .map(|prev_page| {
jump_to_page( jump_to_page(&mut self.page, &mut self.last_render.rect, prev_page)
&mut self.page,
&mut self.last_render.rect,
prev_page
)
}), }),
'z' if key.modifiers.contains(KeyModifiers::CONTROL) => { 'z' if key.modifiers.contains(KeyModifiers::CONTROL) => {
// [todo] better error handling here? // [todo] better error handling here?
@@ -841,16 +829,13 @@ impl Tui {
'$' if can_zoom => self.update_zoom(Zoom::pan_right), '$' if can_zoom => self.update_zoom(Zoom::pan_right),
'r' => Some(InputAction::Rotate), 'r' => Some(InputAction::Rotate),
_ => None _ => None
} },
} KeyCode::Backspace
KeyCode::Backspace => {
if let BottomMessage::Input(InputCommand::Search(ref mut term)) = if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg self.bottom_msg =>
{ {
term.pop(); term.pop();
return Some(InputAction::Redraw); InputAction::Redraw.into()
}
None
} }
KeyCode::Right => self.change_page(PageChange::Next, ChangeAmount::Single), KeyCode::Right => self.change_page(PageChange::Next, ChangeAmount::Single),
KeyCode::Down | KeyCode::PageDown => KeyCode::Down | KeyCode::PageDown =>