Skip to content

Commit 77962e2

Browse files
committed
Made popup title generic. Preview title is now the command name
1 parent aaa61cb commit 77962e2

File tree

4 files changed

+29
-45
lines changed

4 files changed

+29
-45
lines changed

tui/src/floating_text.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,12 @@ use tree_sitter_bash as hl_bash;
2727
use tree_sitter_highlight::{self as hl, HighlightEvent};
2828
use zips::zip_result;
2929

30-
pub enum FloatingTextMode {
31-
Preview,
32-
Description,
33-
ActionsGuide,
34-
}
35-
3630
pub struct FloatingText {
3731
pub src: Vec<String>,
3832
max_line_width: usize,
3933
v_scroll: usize,
4034
h_scroll: usize,
41-
mode_title: &'static str,
35+
mode_title: String,
4236
}
4337

4438
macro_rules! style {
@@ -133,7 +127,7 @@ fn get_lines_owned(s: &str) -> Vec<String> {
133127
}
134128

135129
impl FloatingText {
136-
pub fn new(text: String, mode: FloatingTextMode) -> Self {
130+
pub fn new(text: String, title: &str) -> Self {
137131
let src = get_lines(&text)
138132
.into_iter()
139133
.map(|s| s.to_string())
@@ -142,14 +136,14 @@ impl FloatingText {
142136
let max_line_width = max_width!(src);
143137
Self {
144138
src,
145-
mode_title: Self::get_mode_title(mode),
139+
mode_title: title.to_string(),
146140
max_line_width,
147141
v_scroll: 0,
148142
h_scroll: 0,
149143
}
150144
}
151145

152-
pub fn from_command(command: &Command, mode: FloatingTextMode) -> Option<Self> {
146+
pub fn from_command(command: &Command, title: String) -> Option<Self> {
153147
let (max_line_width, src) = match command {
154148
Command::Raw(cmd) => {
155149
// just apply highlights directly
@@ -172,21 +166,13 @@ impl FloatingText {
172166

173167
Some(Self {
174168
src,
175-
mode_title: Self::get_mode_title(mode),
169+
mode_title: title,
176170
max_line_width,
177171
h_scroll: 0,
178172
v_scroll: 0,
179173
})
180174
}
181175

182-
fn get_mode_title(mode: FloatingTextMode) -> &'static str {
183-
match mode {
184-
FloatingTextMode::Preview => "Command Preview",
185-
FloatingTextMode::Description => "Command Description",
186-
FloatingTextMode::ActionsGuide => "Important Actions Guide",
187-
}
188-
}
189-
190176
fn scroll_down(&mut self) {
191177
if self.v_scroll + 1 < self.src.len() {
192178
self.v_scroll += 1;
@@ -217,7 +203,7 @@ impl FloatContent for FloatingText {
217203
// Define the Block with a border and background color
218204
let block = Block::default()
219205
.borders(Borders::ALL)
220-
.title(self.mode_title)
206+
.title(self.mode_title.clone())
221207
.title_alignment(ratatui::layout::Alignment::Center)
222208
.title_style(Style::default().reversed())
223209
.style(Style::default());
@@ -295,7 +281,7 @@ impl FloatContent for FloatingText {
295281

296282
fn get_shortcut_list(&self) -> ShortcutList {
297283
ShortcutList {
298-
scope_name: self.mode_title,
284+
scope_name: self.mode_title.clone(),
299285
hints: vec![
300286
Shortcut::new(vec!["j", "Down"], "Scroll down"),
301287
Shortcut::new(vec!["k", "Up"], "Scroll up"),

tui/src/hint.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::state::{AppState, Focus};
1111
pub const SHORTCUT_LINES: usize = 2;
1212

1313
pub struct ShortcutList {
14-
pub scope_name: &'static str,
14+
pub scope_name: String,
1515
pub hints: Vec<Shortcut>,
1616
}
1717

@@ -114,7 +114,7 @@ fn get_list_item_shortcut(state: &AppState) -> Vec<Shortcut> {
114114
pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) {
115115
match state.focus {
116116
Focus::Search => ShortcutList {
117-
scope_name: "Search bar",
117+
scope_name: "Search bar".to_string(),
118118
hints: vec![Shortcut::new(vec!["Enter"], "Finish search")],
119119
},
120120

@@ -146,13 +146,13 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) {
146146
hints.push(Shortcut::new(vec!["Shift-Tab"], "Previous tab"));
147147
hints.push(Shortcut::new(vec!["g"], "Important actions guide"));
148148
ShortcutList {
149-
scope_name: "Command list",
149+
scope_name: "Command list".to_string(),
150150
hints,
151151
}
152152
}
153153

154154
Focus::TabList => ShortcutList {
155-
scope_name: "Tab list",
155+
scope_name: "Tab list".to_string(),
156156
hints: vec![
157157
Shortcut::new(vec!["q", "CTRL-c"], "Exit linutil"),
158158
Shortcut::new(vec!["l", "Right", "Enter"], "Focus action list"),

tui/src/running_command.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ impl FloatContent for RunningCommand {
123123
fn get_shortcut_list(&self) -> ShortcutList {
124124
if self.is_finished() {
125125
ShortcutList {
126-
scope_name: "Finished command",
126+
scope_name: "Finished command".to_string(),
127127
hints: vec![Shortcut::new(vec!["Enter", "q"], "Close window")],
128128
}
129129
} else {
130130
ShortcutList {
131-
scope_name: "Running command",
131+
scope_name: "Running command".to_string(),
132132
hints: vec![Shortcut::new(vec!["CTRL-c"], "Kill the command")],
133133
}
134134
}

tui/src/state.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
filter::{Filter, SearchAction},
33
float::{Float, FloatContent},
4-
floating_text::{FloatingText, FloatingTextMode},
4+
floating_text::FloatingText,
55
hint::{draw_shortcuts, SHORTCUT_LINES},
66
running_command::RunningCommand,
77
theme::Theme,
@@ -415,11 +415,11 @@ impl AppState {
415415
}
416416
}
417417
fn toggle_selection(&mut self) {
418-
if let Some(command) = self.get_selected_command() {
419-
if self.selected_commands.contains(&command) {
420-
self.selected_commands.retain(|c| c != &command);
418+
if let Some(list_node) = self.get_selected_list_node() {
419+
if self.selected_commands.contains(&list_node.command) {
420+
self.selected_commands.retain(|c| c != &list_node.command);
421421
} else {
422-
self.selected_commands.push(command);
422+
self.selected_commands.push(list_node.command);
423423
}
424424
}
425425
}
@@ -470,12 +470,8 @@ impl AppState {
470470
}
471471
None
472472
}
473-
pub fn get_selected_command(&self) -> Option<Command> {
474-
self.get_selected_node().map(|node| node.command.clone())
475-
}
476-
fn get_selected_description(&self) -> Option<String> {
477-
self.get_selected_node()
478-
.map(|node| node.description.clone())
473+
pub fn get_selected_list_node(&self) -> Option<ListNode> {
474+
self.get_selected_node().cloned()
479475
}
480476
pub fn go_to_selected_dir(&mut self) {
481477
let mut selected_index = self.selection.selected().unwrap_or(0);
@@ -524,24 +520,26 @@ impl AppState {
524520
!self.at_root() && selected_index == 0
525521
}
526522
fn enable_preview(&mut self) {
527-
if let Some(command) = self.get_selected_command() {
528-
if let Some(preview) = FloatingText::from_command(&command, FloatingTextMode::Preview) {
523+
if let Some(list_node) = self.get_selected_list_node() {
524+
let mut preview_title = "[Preview] - ".to_string();
525+
preview_title.push_str(list_node.name.as_str());
526+
if let Some(preview) = FloatingText::from_command(&list_node.command, preview_title) {
529527
self.spawn_float(preview, 80, 80);
530528
}
531529
}
532530
}
533531
fn enable_description(&mut self) {
534-
if let Some(command_description) = self.get_selected_description() {
535-
let description = FloatingText::new(command_description, FloatingTextMode::Description);
532+
if let Some(list_node) = self.get_selected_list_node() {
533+
let description = FloatingText::new(list_node.description, "Command Description");
536534
self.spawn_float(description, 80, 80);
537535
}
538536
}
539537

540538
fn handle_enter(&mut self) {
541539
if self.selected_item_is_cmd() {
542540
if self.selected_commands.is_empty() {
543-
if let Some(cmd) = self.get_selected_command() {
544-
self.selected_commands.push(cmd);
541+
if let Some(list_node) = self.get_selected_list_node() {
542+
self.selected_commands.push(list_node.command);
545543
}
546544
}
547545
let command = RunningCommand::new(self.selected_commands.clone());
@@ -576,7 +574,7 @@ impl AppState {
576574

577575
fn toggle_task_list_guide(&mut self) {
578576
self.spawn_float(
579-
FloatingText::new(ACTIONS_GUIDE.to_string(), FloatingTextMode::ActionsGuide),
577+
FloatingText::new(ACTIONS_GUIDE.to_string(), "Important Actions Guide"),
580578
80,
581579
80,
582580
);

0 commit comments

Comments
 (0)