Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made popup title customizable. The Script's preview title is now its command name #633

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions tui/src/floating_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@ use tree_sitter_bash as hl_bash;
use tree_sitter_highlight::{self as hl, HighlightEvent};
use zips::zip_result;

pub enum FloatingTextMode {
Preview,
Description,
ActionsGuide,
}

pub struct FloatingText {
pub src: Vec<String>,
max_line_width: usize,
v_scroll: usize,
h_scroll: usize,
mode_title: &'static str,
mode_title: String,
}

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

impl FloatingText {
pub fn new(text: String, mode: FloatingTextMode) -> Self {
pub fn new(text: String, title: &str) -> Self {
let src = get_lines(&text)
.into_iter()
.map(|s| s.to_string())
Expand All @@ -139,14 +133,14 @@ impl FloatingText {
let max_line_width = max_width!(src);
Self {
src,
mode_title: Self::get_mode_title(mode),
mode_title: title.to_string(),
max_line_width,
v_scroll: 0,
h_scroll: 0,
}
}

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

Some(Self {
src,
mode_title: Self::get_mode_title(mode),
mode_title: title,
max_line_width,
h_scroll: 0,
v_scroll: 0,
})
}

fn get_mode_title(mode: FloatingTextMode) -> &'static str {
match mode {
FloatingTextMode::Preview => "Command Preview",
FloatingTextMode::Description => "Command Description",
FloatingTextMode::ActionsGuide => "Important Actions Guide",
}
}

fn scroll_down(&mut self) {
if self.v_scroll + 1 < self.src.len() {
self.v_scroll += 1;
Expand Down Expand Up @@ -214,7 +200,7 @@ impl FloatContent for FloatingText {
// Define the Block with a border and background color
let block = Block::default()
.borders(Borders::ALL)
.title(self.mode_title)
.title(self.mode_title.clone())
.title_alignment(ratatui::layout::Alignment::Center)
.title_style(Style::default().reversed())
.style(Style::default());
Expand Down Expand Up @@ -292,7 +278,7 @@ impl FloatContent for FloatingText {

fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
(
self.mode_title,
&self.mode_title,
Box::new([
Shortcut::new("Scroll down", ["j", "Down"]),
Shortcut::new("Scroll up", ["k", "Up"]),
Expand Down
17 changes: 8 additions & 9 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::rc::Rc;

use crate::{
confirmation::{ConfirmPrompt, ConfirmStatus},
filter::{Filter, SearchAction},
float::{Float, FloatContent},
floating_text::{FloatingText, FloatingTextMode},
floating_text::FloatingText,
hint::{create_shortcut_list, Shortcut},
running_command::RunningCommand,
theme::Theme,
Expand All @@ -21,6 +19,7 @@ use ratatui::{
widgets::{Block, Borders, List, ListState, Paragraph},
Frame,
};
use std::rc::Rc;
use temp_dir::TempDir;

const MIN_WIDTH: u16 = 77;
Expand Down Expand Up @@ -655,18 +654,18 @@ impl AppState {
}

fn enable_preview(&mut self) {
if let Some(node) = self.get_selected_node() {
if let Some(preview) =
FloatingText::from_command(&node.command, FloatingTextMode::Preview)
{
if let Some(list_node) = self.get_selected_node() {
let mut preview_title = "[Preview] - ".to_string();
preview_title.push_str(list_node.name.as_str());
if let Some(preview) = FloatingText::from_command(&list_node.command, preview_title) {
self.spawn_float(preview, 80, 80);
}
}
}

fn enable_description(&mut self) {
if let Some(command_description) = self.get_selected_description() {
let description = FloatingText::new(command_description, FloatingTextMode::Description);
let description = FloatingText::new(command_description, "Command Description");
self.spawn_float(description, 80, 80);
}
}
Expand Down Expand Up @@ -732,7 +731,7 @@ impl AppState {

fn toggle_task_list_guide(&mut self) {
self.spawn_float(
FloatingText::new(ACTIONS_GUIDE.to_string(), FloatingTextMode::ActionsGuide),
FloatingText::new(ACTIONS_GUIDE.to_string(), "Important Actions Guide"),
80,
80,
);
Expand Down