Skip to content

Commit 7448fc7

Browse files
authored
Minor refactor with if-let guards (#145)
`if let` guards were stabilized in 1.95. The code concerning the TODO has been refactored.
1 parent 1ed03e9 commit 7448fc7

1 file changed

Lines changed: 126 additions & 141 deletions

File tree

src/tui.rs

Lines changed: 126 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -701,156 +701,141 @@ impl Tui {
701701
match ev {
702702
Event::Key(key) => {
703703
match key.code {
704-
KeyCode::Char(c) => {
705-
// TODO: refactor back to `if let` arm guards when those are stabilized
704+
KeyCode::Char(c)
706705
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
707-
self.bottom_msg
708-
{
709-
term.push(c);
710-
return Some(InputAction::Redraw);
711-
}
712-
706+
self.bottom_msg =>
707+
{
708+
term.push(c);
709+
InputAction::Redraw.into()
710+
}
711+
KeyCode::Char(c)
713712
if let BottomMessage::Input(InputCommand::GoToPage(ref mut page)) =
714-
self.bottom_msg
715-
{
716-
if c == 'g' && self.is_kitty {
717-
self.update_zoom(Zoom::pan_bottom);
718-
self.set_msg(MessageSetting::Pop);
719-
return Some(InputAction::Redraw);
720-
}
721-
722-
return c.to_digit(10).map(|input_num| {
723-
*page = (*page * 10) + input_num as usize;
724-
InputAction::Redraw
725-
});
713+
self.bottom_msg && matches!(c, 'g' if self.is_kitty) =>
714+
c.to_digit(10).map(|input_num| {
715+
*page = (*page * 10) + input_num as usize;
716+
InputAction::Redraw
717+
}),
718+
KeyCode::Char(_)
719+
if let BottomMessage::Input(InputCommand::GoToPage(_)) =
720+
self.bottom_msg =>
721+
{
722+
self.set_msg(MessageSetting::Pop);
723+
self.update_zoom(Zoom::pan_bottom)
724+
}
725+
KeyCode::Char(c) => match c {
726+
'l' => self.change_page(PageChange::Next, ChangeAmount::Single),
727+
'j' => self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
728+
'h' => self.change_page(PageChange::Prev, ChangeAmount::Single),
729+
'k' => self.change_page(PageChange::Prev, ChangeAmount::WholeScreen),
730+
'q' => Some(InputAction::QuitApp),
731+
'g' => {
732+
self.set_msg(MessageSetting::Some(BottomMessage::Input(
733+
InputCommand::GoToPage(0)
734+
)));
735+
Some(InputAction::Redraw)
726736
}
727-
728-
match c {
729-
'l' => self.change_page(PageChange::Next, ChangeAmount::Single),
730-
'j' => self.change_page(PageChange::Next, ChangeAmount::WholeScreen),
731-
'h' => self.change_page(PageChange::Prev, ChangeAmount::Single),
732-
'k' => self.change_page(PageChange::Prev, ChangeAmount::WholeScreen),
733-
'q' => Some(InputAction::QuitApp),
734-
'g' => {
735-
self.set_msg(MessageSetting::Some(BottomMessage::Input(
736-
InputCommand::GoToPage(0)
737-
)));
738-
Some(InputAction::Redraw)
739-
}
740-
'/' => {
741-
self.set_msg(MessageSetting::Some(BottomMessage::Input(
742-
InputCommand::Search(String::new())
743-
)));
744-
Some(InputAction::Redraw)
745-
}
746-
'i' => Some(InputAction::Invert),
747-
'?' => {
748-
self.showing_help_msg = true;
749-
Some(InputAction::Redraw)
750-
}
751-
'f' => Some(InputAction::Fullscreen),
752-
'n' if self.page < self.rendered.len() - 1 => {
753-
// TODO: If we can't find one, then maybe like block until we've verified
754-
// all the pages have been checked?
755-
self.rendered[(self.page + 1)..]
756-
.iter()
757-
.enumerate()
758-
.find_map(|(idx, p)| {
759-
p.num_results
760-
.is_some_and(|num| num > 0)
761-
.then_some(self.page + 1 + idx)
762-
})
763-
.map(|next_page| {
764-
jump_to_page(
765-
&mut self.page,
766-
&mut self.last_render.rect,
767-
next_page
768-
)
769-
})
770-
}
771-
'N' if self.page > 0 => self.rendered[..(self.page)]
772-
.iter()
773-
.rev()
774-
.enumerate()
775-
.find_map(|(idx, p)| {
776-
p.num_results
777-
.is_some_and(|num| num > 0)
778-
.then_some(self.page - (idx + 1))
779-
})
780-
.map(|prev_page| {
781-
jump_to_page(
782-
&mut self.page,
783-
&mut self.last_render.rect,
784-
prev_page
785-
)
786-
}),
787-
'z' if key.modifiers.contains(KeyModifiers::CONTROL) => {
788-
// [todo] better error handling here?
789-
790-
let mut backend = stdout();
791-
execute!(
792-
&mut backend,
793-
LeaveAlternateScreen,
794-
crossterm::cursor::Show,
795-
crossterm::event::DisableMouseCapture
737+
'/' => {
738+
self.set_msg(MessageSetting::Some(BottomMessage::Input(
739+
InputCommand::Search(String::new())
740+
)));
741+
Some(InputAction::Redraw)
742+
}
743+
'i' => Some(InputAction::Invert),
744+
'?' => {
745+
self.showing_help_msg = true;
746+
Some(InputAction::Redraw)
747+
}
748+
'f' => Some(InputAction::Fullscreen),
749+
// TODO: If we can't find one, then maybe like block until we've verified
750+
// all the pages have been checked?
751+
'n' if self.page < self.rendered.len() - 1 => self.rendered
752+
[(self.page + 1)..]
753+
.iter()
754+
.enumerate()
755+
.find_map(|(idx, p)| {
756+
p.num_results
757+
.is_some_and(|num| num > 0)
758+
.then_some(self.page + 1 + idx)
759+
})
760+
.map(|next_page| {
761+
jump_to_page(&mut self.page, &mut self.last_render.rect, next_page)
762+
}),
763+
'N' if self.page > 0 => self.rendered[..(self.page)]
764+
.iter()
765+
.rev()
766+
.enumerate()
767+
.find_map(|(idx, p)| {
768+
p.num_results
769+
.is_some_and(|num| num > 0)
770+
.then_some(self.page - (idx + 1))
771+
})
772+
.map(|prev_page| {
773+
jump_to_page(&mut self.page, &mut self.last_render.rect, prev_page)
774+
}),
775+
'z' if key.modifiers.contains(KeyModifiers::CONTROL) => {
776+
// [todo] better error handling here?
777+
778+
let mut backend = stdout();
779+
execute!(
780+
&mut backend,
781+
LeaveAlternateScreen,
782+
crossterm::cursor::Show,
783+
crossterm::event::DisableMouseCapture
784+
)
785+
.unwrap();
786+
disable_raw_mode().unwrap();
787+
788+
#[cfg(unix)]
789+
{
790+
// This process will hang after the SIGSTOP call until we get
791+
// foregrounded again by something else, at which point we need to
792+
// re-setup everything so that it all gets drawn again.
793+
nix::sys::signal::kill(
794+
nix::unistd::Pid::this(),
795+
nix::sys::signal::Signal::SIGSTOP
796796
)
797797
.unwrap();
798-
disable_raw_mode().unwrap();
799-
800-
#[cfg(unix)]
801-
{
802-
// This process will hang after the SIGSTOP call until we get
803-
// foregrounded again by something else, at which point we need to
804-
// re-setup everything so that it all gets drawn again.
805-
nix::sys::signal::kill(
806-
nix::unistd::Pid::this(),
807-
nix::sys::signal::Signal::SIGSTOP
808-
)
809-
.unwrap();
810-
}
798+
}
811799

812-
enable_raw_mode().unwrap();
813-
execute!(
814-
&mut backend,
815-
EnterAlternateScreen,
816-
crossterm::cursor::Hide,
817-
crossterm::event::EnableMouseCapture
818-
)
819-
.unwrap();
800+
enable_raw_mode().unwrap();
801+
execute!(
802+
&mut backend,
803+
EnterAlternateScreen,
804+
crossterm::cursor::Hide,
805+
crossterm::event::EnableMouseCapture
806+
)
807+
.unwrap();
820808

821-
self.last_render.rect = Rect::default();
822-
Some(InputAction::Redraw)
823-
}
824-
'z' if self.is_kitty => {
825-
let (zoom, f_or_f) = match self.zoom {
826-
None => (Some(Zoom::default()), FitOrFill::Fill),
827-
Some(_) => (None, FitOrFill::Fit)
828-
};
829-
self.zoom = zoom;
830-
self.last_render.rect = Rect::default();
831-
Some(InputAction::SwitchRenderZoom(f_or_f))
832-
}
833-
'o' if can_zoom => self.update_zoom(Zoom::step_in),
834-
'O' if can_zoom => self.update_zoom(Zoom::step_out),
835-
'L' if can_zoom => self.update_zoom(|z| z.pan(Direction::Right)),
836-
'H' if can_zoom => self.update_zoom(|z| z.pan(Direction::Left)),
837-
'J' if can_zoom => self.update_zoom(|z| z.pan(Direction::Down)),
838-
'K' if can_zoom => self.update_zoom(|z| z.pan(Direction::Up)),
839-
'G' if can_zoom => self.update_zoom(Zoom::pan_top),
840-
'0' if can_zoom => self.update_zoom(Zoom::pan_left),
841-
'$' if can_zoom => self.update_zoom(Zoom::pan_right),
842-
'r' => Some(InputAction::Rotate),
843-
_ => None
809+
self.last_render.rect = Rect::default();
810+
Some(InputAction::Redraw)
844811
}
845-
}
846-
KeyCode::Backspace => {
847-
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
848-
self.bottom_msg
849-
{
850-
term.pop();
851-
return Some(InputAction::Redraw);
812+
'z' if self.is_kitty => {
813+
let (zoom, f_or_f) = match self.zoom {
814+
None => (Some(Zoom::default()), FitOrFill::Fill),
815+
Some(_) => (None, FitOrFill::Fit)
816+
};
817+
self.zoom = zoom;
818+
self.last_render.rect = Rect::default();
819+
Some(InputAction::SwitchRenderZoom(f_or_f))
852820
}
853-
None
821+
'o' if can_zoom => self.update_zoom(Zoom::step_in),
822+
'O' if can_zoom => self.update_zoom(Zoom::step_out),
823+
'L' if can_zoom => self.update_zoom(|z| z.pan(Direction::Right)),
824+
'H' if can_zoom => self.update_zoom(|z| z.pan(Direction::Left)),
825+
'J' if can_zoom => self.update_zoom(|z| z.pan(Direction::Down)),
826+
'K' if can_zoom => self.update_zoom(|z| z.pan(Direction::Up)),
827+
'G' if can_zoom => self.update_zoom(Zoom::pan_top),
828+
'0' if can_zoom => self.update_zoom(Zoom::pan_left),
829+
'$' if can_zoom => self.update_zoom(Zoom::pan_right),
830+
'r' => Some(InputAction::Rotate),
831+
_ => None
832+
},
833+
KeyCode::Backspace
834+
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
835+
self.bottom_msg =>
836+
{
837+
term.pop();
838+
InputAction::Redraw.into()
854839
}
855840
KeyCode::Right => self.change_page(PageChange::Next, ChangeAmount::Single),
856841
KeyCode::Down | KeyCode::PageDown =>

0 commit comments

Comments
 (0)