Skip to content

Commit 552c3d2

Browse files
committed
refactor: extract help rendering into HelpWidget
1 parent 1371006 commit 552c3d2

3 files changed

Lines changed: 74 additions & 50 deletions

File tree

src/app.rs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::cmd_runner::{CmdRunner, Output};
66
use crate::completable_input::CompletableInput;
77
use crate::config::{KeyBindingsConfig, ThemeConfig};
88
use crate::debouncer::debouncer_task;
9+
use crate::help_widget::HelpWidget;
910
use crate::history::History;
1011
use crate::output_widget::{ErrorDisplayMode, ErrorPanePlacement, OutputWidget};
1112
use crate::rura::ExecuteType;
@@ -41,19 +42,19 @@ pub struct App {
4142
rura_widget: RuraWidget,
4243
output_widget: OutputWidget,
4344
search_widget: SearchWidget,
45+
help_widget: HelpWidget,
46+
save_output_widget: SaveToFileWidget,
47+
save_command_widget: SaveToFileWidget,
4448
stdin: String,
45-
exit: bool,
4649
action_rx: Receiver<Action>,
4750
command_tx: Sender<(String, String)>,
4851
key_bindings: KeyBindings,
4952
command_line_placement: CommandLinePlacement,
50-
kb_config: KeyBindingsConfig,
5153
input_mode: InputMode,
5254
debouncer_tx: Sender<()>,
5355
active_mode: ActiveMode,
5456
active_modal: ActiveModal,
55-
save_output_widget: SaveToFileWidget,
56-
save_command_widget: SaveToFileWidget,
57+
exit: bool,
5758
}
5859

5960
impl App {
@@ -127,13 +128,13 @@ impl App {
127128
action_rx,
128129
command_tx,
129130
debouncer_tx,
130-
exit: false,
131131
key_bindings: KeyBindings::from_config(&kb_config),
132132
command_line_placement,
133-
kb_config,
133+
help_widget: HelpWidget::new(kb_config),
134134
input_mode: InputMode::Normal,
135135
active_mode: ActiveMode::default(),
136136
active_modal: ActiveModal::default(),
137+
exit: false,
137138
}
138139
}
139140

@@ -677,7 +678,7 @@ impl App {
677678
self.render_live_confirm(frame);
678679
}
679680
ActiveModal::Help => {
680-
self.render_help(frame);
681+
frame.render_widget(&self.help_widget, frame.area());
681682
}
682683
ActiveModal::SaveOutput => {
683684
self.save_output_widget
@@ -711,48 +712,6 @@ impl App {
711712
frame.render_widget(popup, frame.area());
712713
}
713714

714-
fn render_help(&self, frame: &mut Frame) {
715-
#[rustfmt::skip]
716-
let lines = Text::from(vec![
717-
Line::from(format!("{:012} - Execute full command", self.kb_config.execute_full.first().unwrap().to_string())),
718-
Line::from(format!("{:012} - Execute until cursor", self.kb_config.execute_until_current.first().unwrap().to_string())),
719-
Line::from(format!("{:012} - Execute before cursor", self.kb_config.execute_until_prev.first().unwrap().to_string())),
720-
Line::from(format!("{:012} - Reset input", self.kb_config.reset_input.first().unwrap().to_string())),
721-
Line::from(""),
722-
Line::from(format!("{:012} - Save output to file", self.kb_config.save_output.first().unwrap().to_string())),
723-
Line::from(format!("{:012} - Save command to file", self.kb_config.save_command.first().unwrap().to_string())),
724-
Line::from(""),
725-
Line::from(format!("{:012} - Search next", self.kb_config.search_next.first().unwrap().to_string())),
726-
Line::from(format!("{:012} - Search previous", self.kb_config.search_prev.first().unwrap().to_string())),
727-
Line::from(format!("{:012} - Toggle regex mode", "alt+x")),
728-
Line::from(format!("{:012} - Toggle case sensitivity", "alt+c")),
729-
Line::from(""),
730-
Line::from(format!("{:012} - Complete forward", self.kb_config.complete.first().unwrap().to_string())),
731-
Line::from(format!("{:012} - Complete backward", self.kb_config.complete_prev.first().unwrap().to_string())),
732-
Line::from(""),
733-
Line::from(format!("{:012} - Go to previous subcommand", self.kb_config.subcommand_prev.first().unwrap().to_string())),
734-
Line::from(format!("{:012} - Go to next subcommand", self.kb_config.subcommand_next.first().unwrap().to_string())),
735-
Line::from(""),
736-
Line::from(format!("{:012} - History previous item", self.kb_config.history_prev.first().unwrap().to_string())),
737-
Line::from(format!("{:012} - History next item", self.kb_config.history_next.first().unwrap().to_string())),
738-
Line::from(""),
739-
Line::from(format!("{:012} - Scroll up", self.kb_config.scroll_up.first().unwrap().to_string())),
740-
Line::from(format!("{:012} - Scroll down", self.kb_config.scroll_down.first().unwrap().to_string())),
741-
Line::from(format!("{:012} - Scroll page up", self.kb_config.scroll_up_page.first().unwrap().to_string())),
742-
Line::from(format!("{:012} - Scroll page down", self.kb_config.scroll_down_page.first().unwrap().to_string())),
743-
Line::from(""),
744-
Line::from(format!("{:012} - Scroll right", self.kb_config.scroll_right.first().unwrap().to_string())),
745-
Line::from(format!("{:012} - Scroll left", self.kb_config.scroll_left.first().unwrap().to_string())),
746-
Line::from(""),
747-
Line::from(format!("{:012} - Wrap output lines", self.kb_config.toggle_wrap.first().unwrap().to_string())),
748-
]);
749-
750-
let popup = Popup::new(lines)
751-
.title(" Keys ")
752-
.style(Style::new().white().on_blue());
753-
frame.render_widget(popup, frame.area());
754-
}
755-
756715
fn hints_widget(&self) -> Line<'_> {
757716
let mut spans: Vec<Span> = vec![];
758717

@@ -945,7 +904,7 @@ mod tests {
945904
exit: false,
946905
key_bindings: KeyBindings::from_config(&kb_config),
947906
command_line_placement: CommandLinePlacement::Bottom,
948-
kb_config,
907+
help_widget: HelpWidget::new(kb_config),
949908
input_mode: InputMode::Normal,
950909
active_mode: ActiveMode::default(),
951910
active_modal: ActiveModal::default(),

src/help_widget.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate::config::KeyBindingsConfig;
2+
use ratatui::buffer::Buffer;
3+
use ratatui::layout::Rect;
4+
use ratatui::prelude::Widget;
5+
use ratatui::style::Style;
6+
use ratatui::text::{Line, Text};
7+
use tui_popup::Popup;
8+
9+
pub struct HelpWidget {
10+
kb_config: KeyBindingsConfig,
11+
}
12+
13+
impl HelpWidget {
14+
pub fn new(kb_config: KeyBindingsConfig) -> Self {
15+
Self { kb_config }
16+
}
17+
}
18+
19+
impl Widget for &HelpWidget {
20+
fn render(self, area: Rect, buf: &mut Buffer)
21+
where
22+
Self: Sized,
23+
{
24+
#[rustfmt::skip]
25+
let lines = Text::from(vec![
26+
Line::from(format!("{:012} - Execute full command", self.kb_config.execute_full.first().unwrap().to_string())),
27+
Line::from(format!("{:012} - Execute until cursor", self.kb_config.execute_until_current.first().unwrap().to_string())),
28+
Line::from(format!("{:012} - Execute before cursor", self.kb_config.execute_until_prev.first().unwrap().to_string())),
29+
Line::from(format!("{:012} - Reset input", self.kb_config.reset_input.first().unwrap().to_string())),
30+
Line::from(""),
31+
Line::from(format!("{:012} - Save output to file", self.kb_config.save_output.first().unwrap().to_string())),
32+
Line::from(format!("{:012} - Save command to file", self.kb_config.save_command.first().unwrap().to_string())),
33+
Line::from(""),
34+
Line::from(format!("{:012} - Search next", self.kb_config.search_next.first().unwrap().to_string())),
35+
Line::from(format!("{:012} - Search previous", self.kb_config.search_prev.first().unwrap().to_string())),
36+
Line::from(format!("{:012} - Toggle regex mode", "alt+x")),
37+
Line::from(format!("{:012} - Toggle case sensitivity", "alt+c")),
38+
Line::from(""),
39+
Line::from(format!("{:012} - Complete forward", self.kb_config.complete.first().unwrap().to_string())),
40+
Line::from(format!("{:012} - Complete backward", self.kb_config.complete_prev.first().unwrap().to_string())),
41+
Line::from(""),
42+
Line::from(format!("{:012} - Go to previous subcommand", self.kb_config.subcommand_prev.first().unwrap().to_string())),
43+
Line::from(format!("{:012} - Go to next subcommand", self.kb_config.subcommand_next.first().unwrap().to_string())),
44+
Line::from(""),
45+
Line::from(format!("{:012} - History previous item", self.kb_config.history_prev.first().unwrap().to_string())),
46+
Line::from(format!("{:012} - History next item", self.kb_config.history_next.first().unwrap().to_string())),
47+
Line::from(""),
48+
Line::from(format!("{:012} - Scroll up", self.kb_config.scroll_up.first().unwrap().to_string())),
49+
Line::from(format!("{:012} - Scroll down", self.kb_config.scroll_down.first().unwrap().to_string())),
50+
Line::from(format!("{:012} - Scroll page up", self.kb_config.scroll_up_page.first().unwrap().to_string())),
51+
Line::from(format!("{:012} - Scroll page down", self.kb_config.scroll_down_page.first().unwrap().to_string())),
52+
Line::from(""),
53+
Line::from(format!("{:012} - Scroll right", self.kb_config.scroll_right.first().unwrap().to_string())),
54+
Line::from(format!("{:012} - Scroll left", self.kb_config.scroll_left.first().unwrap().to_string())),
55+
Line::from(""),
56+
Line::from(format!("{:012} - Wrap output lines", self.kb_config.toggle_wrap.first().unwrap().to_string())),
57+
]);
58+
59+
Popup::new(lines)
60+
.title(" Keys ")
61+
.style(Style::new().white().on_blue())
62+
.render(area, buf);
63+
}
64+
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod completable_input;
44
mod completion;
55
mod config;
66
mod debouncer;
7+
mod help_widget;
78
mod history;
89
mod output_widget;
910
mod props;

0 commit comments

Comments
 (0)