Skip to content

Commit

Permalink
use boolean rather than enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Nov 5, 2024
1 parent 2bc6568 commit fb16afa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
63 changes: 23 additions & 40 deletions tui/src/floating_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@ use tree_sitter_bash as hl_bash;
use tree_sitter_highlight::{self as hl, HighlightEvent};
use zips::zip_result;

#[derive(Clone)]
pub enum FloatingTextMode {
Preview,
Description,
ActionsGuide,
}

pub struct FloatingText {
pub src: String,
wrapped_lines: Vec<String>,
max_line_width: usize,
v_scroll: usize,
h_scroll: usize,
mode_title: String,
mode: FloatingTextMode,
wrap_words: bool,
frame_height: usize,
}

Expand Down Expand Up @@ -129,25 +122,30 @@ 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, wrap_words: bool) -> Self {
let max_line_width = 80;
let wrapped_lines = wrap(&text, max_line_width)
.into_iter()
.map(|cow| cow.into_owned())
.collect();
let wrapped_lines = if wrap_words {
wrap(&text, max_line_width)
.into_iter()
.map(|cow| cow.into_owned())
.collect()
} else {
get_lines_owned(&text)
};

Self {
src: text,
wrapped_lines,
mode_title: Self::get_mode_title(&mode).to_string(),
mode_title: title.to_string(),
max_line_width,
v_scroll: 0,
h_scroll: 0,
mode,
wrap_words,
frame_height: 0,
}
}

pub fn from_command(command: &Command, mode: FloatingTextMode) -> Option<Self> {
pub fn from_command(command: &Command, title: String) -> Option<Self> {
let src = match command {
Command::Raw(cmd) => Some(cmd.clone()),
Command::LocalFile { file, .. } => std::fs::read_to_string(file)
Expand All @@ -157,34 +155,20 @@ impl FloatingText {
}?;

let max_line_width = 80;
let wrapped_lines = match mode {
FloatingTextMode::Description => wrap(&src, max_line_width)
.into_iter()
.map(|cow| cow.into_owned())
.collect(),
_ => get_lines_owned(&get_highlighted_string(&src)?),
};
let wrapped_lines = get_lines_owned(&get_highlighted_string(&src)?);

Some(Self {
src,
wrapped_lines,
mode_title: Self::get_mode_title(&mode).to_string(),
mode_title: title,
max_line_width,
h_scroll: 0,
v_scroll: 0,
mode,
wrap_words: false,
frame_height: 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) {
let visible_lines = self.frame_height.saturating_sub(2);
if self.v_scroll + visible_lines < self.src.len() {
Expand Down Expand Up @@ -213,14 +197,13 @@ impl FloatingText {
fn update_wrapping(&mut self, width: usize) {
if self.max_line_width != width {
self.max_line_width = width;
self.wrapped_lines = match self.mode {
FloatingTextMode::Description => wrap(&self.src, width)
self.wrapped_lines = if self.wrap_words {
wrap(&self.src, width)
.into_iter()
.map(|cow| cow.into_owned())
.collect(),
_ => {
get_lines_owned(&get_highlighted_string(&self.src).unwrap_or(self.src.clone()))
}
.collect()
} else {
get_lines_owned(&get_highlighted_string(&self.src).unwrap_or(self.src.clone()))
};
}
}
Expand Down Expand Up @@ -254,7 +237,7 @@ impl FloatContent for FloatingText {
.skip(self.v_scroll)
.take(height as usize)
.flat_map(|l| {
if let FloatingTextMode::Description = self.mode {
if self.wrap_words {
vec![Line::raw(l.clone())]
} else {
l.into_text().unwrap().lines
Expand Down
10 changes: 4 additions & 6 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 Down Expand Up @@ -709,9 +709,7 @@ impl AppState {
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, FloatingTextMode::Preview)
{
if let Some(preview) = FloatingText::from_command(&list_node.command, preview_title) {
self.spawn_float(preview, 80, 80);
}
}
Expand All @@ -721,7 +719,7 @@ impl AppState {
if let Some(command_description) = self.get_selected_description() {
if !command_description.is_empty() {
let description =
FloatingText::new(command_description, FloatingTextMode::Description);
FloatingText::new(command_description, "Command Description", true);
self.spawn_float(description, 80, 80);
}
}
Expand Down Expand Up @@ -807,7 +805,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", true),
80,
80,
);
Expand Down

0 comments on commit fb16afa

Please sign in to comment.