From 5566c0e25e45b0b5b6ad07acef7a8867d802de63 Mon Sep 17 00:00:00 2001 From: Liam <33645555+lj3954@users.noreply.github.com> Date: Sat, 30 Nov 2024 12:55:36 -0800 Subject: [PATCH] refactor: Use a macro to simplify shortcut creation --- tui/src/confirmation.rs | 16 +++++----- tui/src/floating_text.rs | 16 +++++----- tui/src/hint.rs | 11 +++++++ tui/src/running_command.rs | 24 +++++++-------- tui/src/state.rs | 62 ++++++++++++++++++++------------------ 5 files changed, 71 insertions(+), 58 deletions(-) diff --git a/tui/src/confirmation.rs b/tui/src/confirmation.rs index 3a4e9578..ea465fcc 100644 --- a/tui/src/confirmation.rs +++ b/tui/src/confirmation.rs @@ -1,4 +1,4 @@ -use crate::{float::FloatContent, hint::Shortcut, theme}; +use crate::{float::FloatContent, hint::Shortcut, shortcuts, theme}; use ratatui::{ crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind}, layout::Alignment, @@ -135,13 +135,13 @@ impl FloatContent for ConfirmPrompt { fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) { ( "Confirmation prompt", - Box::new([ - Shortcut::new("Continue", ["Y", "y"]), - Shortcut::new("Abort", ["N", "n", "q", "Esc"]), - Shortcut::new("Scroll up", ["k", "Up"]), - Shortcut::new("Scroll down", ["j", "Down"]), - Shortcut::new("Close linutil", ["CTRL-c"]), - ]), + shortcuts!( + ("Continue", ["Y", "y"]), + ("Abort", ["N", "n", "q", "Esc"]), + ("Scroll up", ["k", "Up"]), + ("Scroll down", ["j", "Down"]), + ("Close linutil", ["CTRL-c"]), + ), ) } } diff --git a/tui/src/floating_text.rs b/tui/src/floating_text.rs index c2077de5..b449a6ee 100644 --- a/tui/src/floating_text.rs +++ b/tui/src/floating_text.rs @@ -1,4 +1,4 @@ -use crate::{float::FloatContent, hint::Shortcut, theme::Theme}; +use crate::{float::FloatContent, hint::Shortcut, shortcuts, theme::Theme}; use linutil_core::Command; use ratatui::{ crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind}, @@ -228,13 +228,13 @@ impl<'a> FloatContent for FloatingText<'a> { fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) { ( &self.mode_title, - Box::new([ - Shortcut::new("Scroll down", ["j", "Down"]), - Shortcut::new("Scroll up", ["k", "Up"]), - Shortcut::new("Scroll left", ["h", "Left"]), - Shortcut::new("Scroll right", ["l", "Right"]), - Shortcut::new("Close window", ["Enter", "p", "q", "d", "g"]), - ]), + shortcuts!( + ("Scroll down", ["j", "Down"]), + ("Scroll up", ["k", "Up"]), + ("Scroll left", ["h", "Left"]), + ("Scroll right", ["l", "Right"]), + ("Close window", ["Enter", "p", "q", "d", "g"]) + ), ) } } diff --git a/tui/src/hint.rs b/tui/src/hint.rs index 0f77f7a5..19cfb585 100644 --- a/tui/src/hint.rs +++ b/tui/src/hint.rs @@ -78,3 +78,14 @@ impl Shortcut { .collect() } } + +#[macro_export] +macro_rules! shortcuts { + ($(($name:literal,[$($key:literal),+ $(,)?])),* $(,)?) => { + vec![ + $( + Shortcut::new($name, [$($key),*]) + ),* + ].into_boxed_slice() + }; +} diff --git a/tui/src/running_command.rs b/tui/src/running_command.rs index d2342826..378eec4d 100644 --- a/tui/src/running_command.rs +++ b/tui/src/running_command.rs @@ -1,4 +1,4 @@ -use crate::{float::FloatContent, hint::Shortcut, theme::Theme}; +use crate::{float::FloatContent, hint::Shortcut, shortcuts, theme::Theme}; use linutil_core::Command; use oneshot::{channel, Receiver}; use portable_pty::{ @@ -139,21 +139,21 @@ impl FloatContent for RunningCommand { if self.is_finished() { ( "Finished command", - Box::new([ - Shortcut::new("Close window", ["Enter", "q"]), - Shortcut::new("Scroll up", ["Page up"]), - Shortcut::new("Scroll down", ["Page down"]), - Shortcut::new("Save log", ["l"]), - ]), + shortcuts!( + ("Close window", ["Enter", "q"]), + ("Scroll up", ["Page up"]), + ("Scroll down", ["Page down"]), + ("Save log", ["l"]), + ), ) } else { ( "Running command", - Box::new([ - Shortcut::new("Kill the command", ["CTRL-c"]), - Shortcut::new("Scroll up", ["Page up"]), - Shortcut::new("Scroll down", ["Page down"]), - ]), + shortcuts!( + ("Kill the command", ["CTRL-c"]), + ("Scroll up", ["Page up"]), + ("Scroll down", ["Page down"]), + ), ) } } diff --git a/tui/src/state.rs b/tui/src/state.rs index cec9cc3c..3225feb7 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -6,6 +6,7 @@ use crate::{ hint::{create_shortcut_list, Shortcut}, root::check_root_status, running_command::RunningCommand, + shortcuts, theme::Theme, Args, }; @@ -171,13 +172,13 @@ impl AppState { fn get_list_item_shortcut(&self) -> Box<[Shortcut]> { if self.selected_item_is_dir() { - Box::new([Shortcut::new("Go to selected dir", ["l", "Right", "Enter"])]) + shortcuts!(("Go to selected dir", ["l", "Right", "Enter"])) } else { - Box::new([ - Shortcut::new("Run selected command", ["l", "Right", "Enter"]), - Shortcut::new("Enable preview", ["p"]), - Shortcut::new("Command Description", ["d"]), - ]) + shortcuts!( + ("Run selected command", ["l", "Right", "Enter"]), + ("Enable preview", ["p"]), + ("Command Description", ["d"]) + ) } } @@ -185,10 +186,7 @@ impl AppState { match self.focus { Focus::Search => ( "Search bar", - Box::new([ - Shortcut::new("Abort search", ["Esc", "CTRL-c"]), - Shortcut::new("Search", ["Enter"]), - ]), + shortcuts!(("Abort search", ["Esc", "CTRL-c"]), ("Search", ["Enter"])), ), Focus::List => { @@ -208,35 +206,39 @@ impl AppState { hints.extend(self.get_list_item_shortcut()); } - hints.push(Shortcut::new("Select item above", ["k", "Up"])); - hints.push(Shortcut::new("Select item below", ["j", "Down"])); - hints.push(Shortcut::new("Next theme", ["t"])); - hints.push(Shortcut::new("Previous theme", ["T"])); - hints.push(Shortcut::new("Multi-selection mode", ["v"])); + hints.extend(shortcuts!( + ("Select item above", ["k", "Up"]), + ("Select item below", ["j", "Down"]), + ("Next theme", ["t"]), + ("Previous theme", ["T"]), + ("Multi-selection mode", ["v"]), + )); if self.multi_select { hints.push(Shortcut::new("Select multiple commands", ["Space"])); } - hints.push(Shortcut::new("Next tab", ["Tab"])); - hints.push(Shortcut::new("Previous tab", ["Shift-Tab"])); - hints.push(Shortcut::new("Important actions guide", ["g"])); + hints.extend(shortcuts!( + ("Next tab", ["Tab"]), + ("Previous tab", ["Shift-Tab"]), + ("Important actions guide", ["g"]) + )); ("Command list", hints.into_boxed_slice()) } Focus::TabList => ( "Tab list", - Box::new([ - Shortcut::new("Exit linutil", ["q", "CTRL-c"]), - Shortcut::new("Focus action list", ["l", "Right", "Enter"]), - Shortcut::new("Select item above", ["k", "Up"]), - Shortcut::new("Select item below", ["j", "Down"]), - Shortcut::new("Next theme", ["t"]), - Shortcut::new("Previous theme", ["T"]), - Shortcut::new("Next tab", ["Tab"]), - Shortcut::new("Previous tab", ["Shift-Tab"]), - Shortcut::new("Important actions guide", ["g"]), - Shortcut::new("Multi-selection mode", ["v"]), - ]), + shortcuts!( + ("Exit linutil", ["q", "CTRL-c"]), + ("Focus action list", ["l", "Right", "Enter"]), + ("Select item above", ["k", "Up"]), + ("Select item below", ["j", "Down"]), + ("Next theme", ["t"]), + ("Previous theme", ["T"]), + ("Next tab", ["Tab"]), + ("Previous tab", ["Shift-Tab"]), + ("Important actions guide", ["g"]), + ("Multi-selection mode", ["v"]), + ), ), Focus::FloatingWindow(ref float) => float.get_shortcut_list(),