Skip to content

Commit abebe9b

Browse files
Merge pull request #633 from afonsofrancof:generic-popup-title
Made popup title customizable. The Script's preview title is now its command name
2 parents 49379a7 + 34ffbc5 commit abebe9b

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

tui/src/floating_text.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,12 @@ use tree_sitter_bash as hl_bash;
2424
use tree_sitter_highlight::{self as hl, HighlightEvent};
2525
use zips::zip_result;
2626

27-
pub enum FloatingTextMode {
28-
Preview,
29-
Description,
30-
ActionsGuide,
31-
}
32-
3327
pub struct FloatingText {
3428
pub src: Vec<String>,
3529
max_line_width: usize,
3630
v_scroll: usize,
3731
h_scroll: usize,
38-
mode_title: &'static str,
32+
mode_title: String,
3933
}
4034

4135
macro_rules! style {
@@ -130,7 +124,7 @@ fn get_lines_owned(s: &str) -> Vec<String> {
130124
}
131125

132126
impl FloatingText {
133-
pub fn new(text: String, mode: FloatingTextMode) -> Self {
127+
pub fn new(text: String, title: &str) -> Self {
134128
let src = get_lines(&text)
135129
.into_iter()
136130
.map(|s| s.to_string())
@@ -139,14 +133,14 @@ impl FloatingText {
139133
let max_line_width = max_width!(src);
140134
Self {
141135
src,
142-
mode_title: Self::get_mode_title(mode),
136+
mode_title: title.to_string(),
143137
max_line_width,
144138
v_scroll: 0,
145139
h_scroll: 0,
146140
}
147141
}
148142

149-
pub fn from_command(command: &Command, mode: FloatingTextMode) -> Option<Self> {
143+
pub fn from_command(command: &Command, title: String) -> Option<Self> {
150144
let (max_line_width, src) = match command {
151145
Command::Raw(cmd) => {
152146
// just apply highlights directly
@@ -169,21 +163,13 @@ impl FloatingText {
169163

170164
Some(Self {
171165
src,
172-
mode_title: Self::get_mode_title(mode),
166+
mode_title: title,
173167
max_line_width,
174168
h_scroll: 0,
175169
v_scroll: 0,
176170
})
177171
}
178172

179-
fn get_mode_title(mode: FloatingTextMode) -> &'static str {
180-
match mode {
181-
FloatingTextMode::Preview => "Command Preview",
182-
FloatingTextMode::Description => "Command Description",
183-
FloatingTextMode::ActionsGuide => "Important Actions Guide",
184-
}
185-
}
186-
187173
fn scroll_down(&mut self) {
188174
if self.v_scroll + 1 < self.src.len() {
189175
self.v_scroll += 1;
@@ -214,7 +200,7 @@ impl FloatContent for FloatingText {
214200
// Define the Block with a border and background color
215201
let block = Block::default()
216202
.borders(Borders::ALL)
217-
.title(self.mode_title)
203+
.title(self.mode_title.clone())
218204
.title_alignment(ratatui::layout::Alignment::Center)
219205
.title_style(Style::default().reversed())
220206
.style(Style::default());
@@ -292,7 +278,7 @@ impl FloatContent for FloatingText {
292278

293279
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
294280
(
295-
self.mode_title,
281+
&self.mode_title,
296282
Box::new([
297283
Shortcut::new("Scroll down", ["j", "Down"]),
298284
Shortcut::new("Scroll up", ["k", "Up"]),

tui/src/state.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use std::rc::Rc;
2-
31
use crate::{
42
confirmation::{ConfirmPrompt, ConfirmStatus},
53
filter::{Filter, SearchAction},
64
float::{Float, FloatContent},
7-
floating_text::{FloatingText, FloatingTextMode},
5+
floating_text::FloatingText,
86
hint::{create_shortcut_list, Shortcut},
97
running_command::RunningCommand,
108
theme::Theme,
@@ -21,6 +19,7 @@ use ratatui::{
2119
widgets::{Block, Borders, List, ListState, Paragraph},
2220
Frame,
2321
};
22+
use std::rc::Rc;
2423
use temp_dir::TempDir;
2524

2625
const MIN_WIDTH: u16 = 77;
@@ -655,18 +654,18 @@ impl AppState {
655654
}
656655

657656
fn enable_preview(&mut self) {
658-
if let Some(node) = self.get_selected_node() {
659-
if let Some(preview) =
660-
FloatingText::from_command(&node.command, FloatingTextMode::Preview)
661-
{
657+
if let Some(list_node) = self.get_selected_node() {
658+
let mut preview_title = "[Preview] - ".to_string();
659+
preview_title.push_str(list_node.name.as_str());
660+
if let Some(preview) = FloatingText::from_command(&list_node.command, preview_title) {
662661
self.spawn_float(preview, 80, 80);
663662
}
664663
}
665664
}
666665

667666
fn enable_description(&mut self) {
668667
if let Some(command_description) = self.get_selected_description() {
669-
let description = FloatingText::new(command_description, FloatingTextMode::Description);
668+
let description = FloatingText::new(command_description, "Command Description");
670669
self.spawn_float(description, 80, 80);
671670
}
672671
}
@@ -732,7 +731,7 @@ impl AppState {
732731

733732
fn toggle_task_list_guide(&mut self) {
734733
self.spawn_float(
735-
FloatingText::new(ACTIONS_GUIDE.to_string(), FloatingTextMode::ActionsGuide),
734+
FloatingText::new(ACTIONS_GUIDE.to_string(), "Important Actions Guide"),
736735
80,
737736
80,
738737
);

0 commit comments

Comments
 (0)