Skip to content

Commit

Permalink
use command names in FloatContent titles
Browse files Browse the repository at this point in the history
Co-authored-by: Afonso Franco F. <[email protected]>
  • Loading branch information
cartercanedy and afonsofrancof committed Oct 3, 2024
1 parent 9e8dc0e commit 486b39b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 13 additions & 3 deletions tui/src/floating_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum FloatingTextMode {

pub struct FloatingText {
pub src: Vec<String>,
name: Option<String>,
max_line_width: usize,
v_scroll: usize,
h_scroll: usize,
Expand Down Expand Up @@ -130,7 +131,7 @@ fn get_lines_owned(s: &str) -> Vec<String> {
}

impl FloatingText {
pub fn new(text: String, mode: FloatingTextMode) -> Self {
pub fn new(text: String, name: Option<String>, mode: FloatingTextMode) -> Self {
let src = get_lines(&text)
.into_iter()
.map(|s| s.to_string())
Expand All @@ -139,19 +140,21 @@ impl FloatingText {
let max_line_width = max_width!(src);
Self {
src,
name,
mode_title: Self::get_mode_title(mode),
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, name: Option<String>, mode: FloatingTextMode) -> Option<Self> {
let (max_line_width, src) = match command {
Command::Raw(cmd) => {
// just apply highlights directly
(max_width!(get_lines(cmd)), Some(cmd.clone()))
}

Command::LocalFile { file, .. } => {
// have to read from tmp dir to get cmd src
let raw = std::fs::read_to_string(file)
Expand All @@ -169,6 +172,7 @@ impl FloatingText {

Some(Self {
src,
name,
mode_title: Self::get_mode_title(mode),
max_line_width,
h_scroll: 0,
Expand Down Expand Up @@ -211,10 +215,16 @@ impl FloatingText {

impl FloatContent for FloatingText {
fn top_title(&self) -> Option<Line<'_>> {
let title_text = format!(" {} ", self.mode_title);
let mut title_text = format!(" {} ", self.mode_title);

if let Some(ref name) = self.name {
title_text = format!("{}- {} ", title_text, name);
}

let title_line = Line::from(title_text)
.centered()
.style(Style::default().reversed());

Some(title_line)
}

Expand Down
8 changes: 5 additions & 3 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ 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)
FloatingText::from_command(&node.command, Some(node.name.clone()), FloatingTextMode::Preview)
{
self.spawn_float(preview, 80, 80);
}
Expand All @@ -662,7 +662,9 @@ impl AppState {

fn enable_description(&mut self) {
if let Some(command_description) = self.get_selected_description() {
let description = FloatingText::new(command_description, FloatingTextMode::Description);
let name = self.get_selected_node().map(|node| node.name.clone());

let description = FloatingText::new(command_description, name, FloatingTextMode::Description);
self.spawn_float(description, 80, 80);
}
}
Expand Down Expand Up @@ -728,7 +730,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(), None, FloatingTextMode::ActionsGuide),
80,
80,
);
Expand Down

0 comments on commit 486b39b

Please sign in to comment.