refactor: Use a macro to simplify shortcut creation

This commit is contained in:
Liam 2024-11-30 12:55:36 -08:00
parent 15c40d1297
commit 5566c0e25e
No known key found for this signature in database
5 changed files with 71 additions and 58 deletions

View File

@ -1,4 +1,4 @@
use crate::{float::FloatContent, hint::Shortcut, theme}; use crate::{float::FloatContent, hint::Shortcut, shortcuts, theme};
use ratatui::{ use ratatui::{
crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind}, crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind},
layout::Alignment, layout::Alignment,
@ -135,13 +135,13 @@ impl FloatContent for ConfirmPrompt {
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) { fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
( (
"Confirmation prompt", "Confirmation prompt",
Box::new([ shortcuts!(
Shortcut::new("Continue", ["Y", "y"]), ("Continue", ["Y", "y"]),
Shortcut::new("Abort", ["N", "n", "q", "Esc"]), ("Abort", ["N", "n", "q", "Esc"]),
Shortcut::new("Scroll up", ["k", "Up"]), ("Scroll up", ["k", "Up"]),
Shortcut::new("Scroll down", ["j", "Down"]), ("Scroll down", ["j", "Down"]),
Shortcut::new("Close linutil", ["CTRL-c"]), ("Close linutil", ["CTRL-c"]),
]), ),
) )
} }
} }

View File

@ -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 linutil_core::Command;
use ratatui::{ use ratatui::{
crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind}, crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind},
@ -228,13 +228,13 @@ impl<'a> FloatContent for FloatingText<'a> {
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) { fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
( (
&self.mode_title, &self.mode_title,
Box::new([ shortcuts!(
Shortcut::new("Scroll down", ["j", "Down"]), ("Scroll down", ["j", "Down"]),
Shortcut::new("Scroll up", ["k", "Up"]), ("Scroll up", ["k", "Up"]),
Shortcut::new("Scroll left", ["h", "Left"]), ("Scroll left", ["h", "Left"]),
Shortcut::new("Scroll right", ["l", "Right"]), ("Scroll right", ["l", "Right"]),
Shortcut::new("Close window", ["Enter", "p", "q", "d", "g"]), ("Close window", ["Enter", "p", "q", "d", "g"])
]), ),
) )
} }
} }

View File

@ -78,3 +78,14 @@ impl Shortcut {
.collect() .collect()
} }
} }
#[macro_export]
macro_rules! shortcuts {
($(($name:literal,[$($key:literal),+ $(,)?])),* $(,)?) => {
vec![
$(
Shortcut::new($name, [$($key),*])
),*
].into_boxed_slice()
};
}

View File

@ -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 linutil_core::Command;
use oneshot::{channel, Receiver}; use oneshot::{channel, Receiver};
use portable_pty::{ use portable_pty::{
@ -139,21 +139,21 @@ impl FloatContent for RunningCommand {
if self.is_finished() { if self.is_finished() {
( (
"Finished command", "Finished command",
Box::new([ shortcuts!(
Shortcut::new("Close window", ["Enter", "q"]), ("Close window", ["Enter", "q"]),
Shortcut::new("Scroll up", ["Page up"]), ("Scroll up", ["Page up"]),
Shortcut::new("Scroll down", ["Page down"]), ("Scroll down", ["Page down"]),
Shortcut::new("Save log", ["l"]), ("Save log", ["l"]),
]), ),
) )
} else { } else {
( (
"Running command", "Running command",
Box::new([ shortcuts!(
Shortcut::new("Kill the command", ["CTRL-c"]), ("Kill the command", ["CTRL-c"]),
Shortcut::new("Scroll up", ["Page up"]), ("Scroll up", ["Page up"]),
Shortcut::new("Scroll down", ["Page down"]), ("Scroll down", ["Page down"]),
]), ),
) )
} }
} }

View File

@ -6,6 +6,7 @@ use crate::{
hint::{create_shortcut_list, Shortcut}, hint::{create_shortcut_list, Shortcut},
root::check_root_status, root::check_root_status,
running_command::RunningCommand, running_command::RunningCommand,
shortcuts,
theme::Theme, theme::Theme,
Args, Args,
}; };
@ -171,13 +172,13 @@ impl AppState {
fn get_list_item_shortcut(&self) -> Box<[Shortcut]> { fn get_list_item_shortcut(&self) -> Box<[Shortcut]> {
if self.selected_item_is_dir() { 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 { } else {
Box::new([ shortcuts!(
Shortcut::new("Run selected command", ["l", "Right", "Enter"]), ("Run selected command", ["l", "Right", "Enter"]),
Shortcut::new("Enable preview", ["p"]), ("Enable preview", ["p"]),
Shortcut::new("Command Description", ["d"]), ("Command Description", ["d"])
]) )
} }
} }
@ -185,10 +186,7 @@ impl AppState {
match self.focus { match self.focus {
Focus::Search => ( Focus::Search => (
"Search bar", "Search bar",
Box::new([ shortcuts!(("Abort search", ["Esc", "CTRL-c"]), ("Search", ["Enter"])),
Shortcut::new("Abort search", ["Esc", "CTRL-c"]),
Shortcut::new("Search", ["Enter"]),
]),
), ),
Focus::List => { Focus::List => {
@ -208,35 +206,39 @@ impl AppState {
hints.extend(self.get_list_item_shortcut()); hints.extend(self.get_list_item_shortcut());
} }
hints.push(Shortcut::new("Select item above", ["k", "Up"])); hints.extend(shortcuts!(
hints.push(Shortcut::new("Select item below", ["j", "Down"])); ("Select item above", ["k", "Up"]),
hints.push(Shortcut::new("Next theme", ["t"])); ("Select item below", ["j", "Down"]),
hints.push(Shortcut::new("Previous theme", ["T"])); ("Next theme", ["t"]),
hints.push(Shortcut::new("Multi-selection mode", ["v"])); ("Previous theme", ["T"]),
("Multi-selection mode", ["v"]),
));
if self.multi_select { if self.multi_select {
hints.push(Shortcut::new("Select multiple commands", ["Space"])); hints.push(Shortcut::new("Select multiple commands", ["Space"]));
} }
hints.push(Shortcut::new("Next tab", ["Tab"])); hints.extend(shortcuts!(
hints.push(Shortcut::new("Previous tab", ["Shift-Tab"])); ("Next tab", ["Tab"]),
hints.push(Shortcut::new("Important actions guide", ["g"])); ("Previous tab", ["Shift-Tab"]),
("Important actions guide", ["g"])
));
("Command list", hints.into_boxed_slice()) ("Command list", hints.into_boxed_slice())
} }
Focus::TabList => ( Focus::TabList => (
"Tab list", "Tab list",
Box::new([ shortcuts!(
Shortcut::new("Exit linutil", ["q", "CTRL-c"]), ("Exit linutil", ["q", "CTRL-c"]),
Shortcut::new("Focus action list", ["l", "Right", "Enter"]), ("Focus action list", ["l", "Right", "Enter"]),
Shortcut::new("Select item above", ["k", "Up"]), ("Select item above", ["k", "Up"]),
Shortcut::new("Select item below", ["j", "Down"]), ("Select item below", ["j", "Down"]),
Shortcut::new("Next theme", ["t"]), ("Next theme", ["t"]),
Shortcut::new("Previous theme", ["T"]), ("Previous theme", ["T"]),
Shortcut::new("Next tab", ["Tab"]), ("Next tab", ["Tab"]),
Shortcut::new("Previous tab", ["Shift-Tab"]), ("Previous tab", ["Shift-Tab"]),
Shortcut::new("Important actions guide", ["g"]), ("Important actions guide", ["g"]),
Shortcut::new("Multi-selection mode", ["v"]), ("Multi-selection mode", ["v"]),
]), ),
), ),
Focus::FloatingWindow(ref float) => float.get_shortcut_list(), Focus::FloatingWindow(ref float) => float.get_shortcut_list(),