Skip to content

Commit

Permalink
refactor: Use a macro to simplify shortcut creation
Browse files Browse the repository at this point in the history
  • Loading branch information
lj3954 committed Nov 30, 2024
1 parent 15c40d1 commit 5566c0e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 58 deletions.
16 changes: 8 additions & 8 deletions tui/src/confirmation.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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"]),
),
)
}
}
16 changes: 8 additions & 8 deletions tui/src/floating_text.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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"])
),
)
}
}
11 changes: 11 additions & 0 deletions tui/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ impl Shortcut {
.collect()
}
}

#[macro_export]
macro_rules! shortcuts {
($(($name:literal,[$($key:literal),+ $(,)?])),* $(,)?) => {
vec![
$(
Shortcut::new($name, [$($key),*])
),*
].into_boxed_slice()
};
}
24 changes: 12 additions & 12 deletions tui/src/running_command.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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"]),
),
)
}
}
Expand Down
62 changes: 32 additions & 30 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
hint::{create_shortcut_list, Shortcut},
root::check_root_status,
running_command::RunningCommand,
shortcuts,
theme::Theme,
Args,
};
Expand Down Expand Up @@ -171,24 +172,21 @@ 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"])
)
}
}

pub fn get_keybinds(&self) -> (&str, Box<[Shortcut]>) {
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 => {
Expand All @@ -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(),
Expand Down

0 comments on commit 5566c0e

Please sign in to comment.