Skip to content

Commit ce3f5ca

Browse files
committed
Minor refactor with if-let guards
`if let` guards were stabilized in 1.95. The code concerning the TODO has been refactored.
1 parent cf9d295 commit ce3f5ca

1 file changed

Lines changed: 124 additions & 141 deletions

File tree

src/tui.rs

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

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

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
810+
self.last_render.rect = Rect::default();
811+
Some(InputAction::Redraw)
844812
}
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);
813+
'z' if self.is_kitty => {
814+
let (zoom, f_or_f) = match self.zoom {
815+
None => (Some(Zoom::default()), FitOrFill::Fill),
816+
Some(_) => (None, FitOrFill::Fit)
817+
};
818+
self.zoom = zoom;
819+
self.last_render.rect = Rect::default();
820+
Some(InputAction::SwitchRenderZoom(f_or_f))
852821
}
853-
None
854-
}
822+
'o' if can_zoom => self.update_zoom(Zoom::step_in),
823+
'O' if can_zoom => self.update_zoom(Zoom::step_out),
824+
'L' if can_zoom => self.update_zoom(|z| z.pan(Direction::Right)),
825+
'H' if can_zoom => self.update_zoom(|z| z.pan(Direction::Left)),
826+
'J' if can_zoom => self.update_zoom(|z| z.pan(Direction::Down)),
827+
'K' if can_zoom => self.update_zoom(|z| z.pan(Direction::Up)),
828+
'G' if can_zoom => self.update_zoom(Zoom::pan_top),
829+
'0' if can_zoom => self.update_zoom(Zoom::pan_left),
830+
'$' if can_zoom => self.update_zoom(Zoom::pan_right),
831+
'r' => Some(InputAction::Rotate),
832+
_ => None
833+
},
834+
KeyCode::Backspace
835+
if let BottomMessage::Input(InputCommand::Search(ref mut term)) =
836+
self.bottom_msg =>
837+
(term.pop(), InputAction::Redraw).1.into(),
855838
KeyCode::Right => self.change_page(PageChange::Next, ChangeAmount::Single),
856839
KeyCode::Down | KeyCode::PageDown =>
857840
self.change_page(PageChange::Next, ChangeAmount::WholeScreen),

0 commit comments

Comments
 (0)