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 {
Event::Key(key) => {
match key.code {
KeyCode::Char(c) => {
// TODO: refactor back to `if let` arm guards when those are stabilized
KeyCode::Char(c)
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg
self.bottom_msg =>
{
term.push(c);
return Some(InputAction::Redraw);
InputAction::Redraw.into()
}
KeyCode::Char(c)
if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) =
self.bottom_msg
{
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| {
self.bottom_msg && matches!(c, 'g' if self.is_kitty) =>
c.to_digit(10).map(|input_num| {
*page = (*page * 10) + input_num as usize;
InputAction::Redraw
});
}),
KeyCode::Char(_)
if let BottomMessage::Input(InputCommand::GoToPage(_)) =
self.bottom_msg =>
{
self.set_msg(MessageSetting::Pop);
self.update_zoom(Zoom::pan_bottom)
}
match c {
KeyCode::Char(c) => match c {
'l' => self.change_page(PageChange::Next, ChangeAmount::Single),
'j' => self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
'h' => self.change_page(PageChange::Prev, ChangeAmount::Single),
@@ -749,10 +746,10 @@ impl Tui {
Some(InputAction::Redraw)
}
'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
// 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()
.enumerate()
.find_map(|(idx, p)| {
@@ -761,13 +758,8 @@ impl Tui {
.then_some(self.page + 1 + idx)
})
.map(|next_page| {
jump_to_page(
&mut self.page,
&mut self.last_render.rect,
next_page
)
})
}
jump_to_page(&mut self.page, &mut self.last_render.rect, next_page)
}),
'N' if self.page > 0 => self.rendered[..(self.page)]
.iter()
.rev()
@@ -778,11 +770,7 @@ impl Tui {
.then_some(self.page - (idx + 1))
})
.map(|prev_page| {
jump_to_page(
&mut self.page,
&mut self.last_render.rect,
prev_page
)
jump_to_page(&mut self.page, &mut self.last_render.rect, prev_page)
}),
'z' if key.modifiers.contains(KeyModifiers::CONTROL) => {
// [todo] better error handling here?
@@ -841,16 +829,13 @@ impl Tui {
'$' if can_zoom => self.update_zoom(Zoom::pan_right),
'r' => Some(InputAction::Rotate),
_ => None
}
}
KeyCode::Backspace => {
},
KeyCode::Backspace
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
self.bottom_msg
self.bottom_msg =>
{
term.pop();
return Some(InputAction::Redraw);
}
None
InputAction::Redraw.into()
}
KeyCode::Right => self.change_page(PageChange::Next, ChangeAmount::Single),
KeyCode::Down | KeyCode::PageDown =>