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::{
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"]),
),
)
}
}

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 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"])
),
)
}
}

View File

@ -78,3 +78,14 @@ impl Shortcut {
.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 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"]),
),
)
}
}

View File

@ -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(),