Made popup title generic. Preview title is now the command name

This commit is contained in:
afonsofrancof 2024-09-23 03:31:52 +01:00
parent aaa61cbce8
commit 77962e2a5c
No known key found for this signature in database
4 changed files with 29 additions and 45 deletions

View File

@ -27,18 +27,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 {
@ -133,7 +127,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())
@ -142,14 +136,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
@ -172,21 +166,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;
@ -217,7 +203,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());
@ -295,7 +281,7 @@ impl FloatContent for FloatingText {
fn get_shortcut_list(&self) -> ShortcutList {
ShortcutList {
scope_name: self.mode_title,
scope_name: self.mode_title.clone(),
hints: vec![
Shortcut::new(vec!["j", "Down"], "Scroll down"),
Shortcut::new(vec!["k", "Up"], "Scroll up"),

View File

@ -11,7 +11,7 @@ use crate::state::{AppState, Focus};
pub const SHORTCUT_LINES: usize = 2;
pub struct ShortcutList {
pub scope_name: &'static str,
pub scope_name: String,
pub hints: Vec<Shortcut>,
}
@ -114,7 +114,7 @@ fn get_list_item_shortcut(state: &AppState) -> Vec<Shortcut> {
pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) {
match state.focus {
Focus::Search => ShortcutList {
scope_name: "Search bar",
scope_name: "Search bar".to_string(),
hints: vec![Shortcut::new(vec!["Enter"], "Finish search")],
},
@ -146,13 +146,13 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) {
hints.push(Shortcut::new(vec!["Shift-Tab"], "Previous tab"));
hints.push(Shortcut::new(vec!["g"], "Important actions guide"));
ShortcutList {
scope_name: "Command list",
scope_name: "Command list".to_string(),
hints,
}
}
Focus::TabList => ShortcutList {
scope_name: "Tab list",
scope_name: "Tab list".to_string(),
hints: vec![
Shortcut::new(vec!["q", "CTRL-c"], "Exit linutil"),
Shortcut::new(vec!["l", "Right", "Enter"], "Focus action list"),

View File

@ -123,12 +123,12 @@ impl FloatContent for RunningCommand {
fn get_shortcut_list(&self) -> ShortcutList {
if self.is_finished() {
ShortcutList {
scope_name: "Finished command",
scope_name: "Finished command".to_string(),
hints: vec![Shortcut::new(vec!["Enter", "q"], "Close window")],
}
} else {
ShortcutList {
scope_name: "Running command",
scope_name: "Running command".to_string(),
hints: vec![Shortcut::new(vec!["CTRL-c"], "Kill the command")],
}
}

View File

@ -1,7 +1,7 @@
use crate::{
filter::{Filter, SearchAction},
float::{Float, FloatContent},
floating_text::{FloatingText, FloatingTextMode},
floating_text::FloatingText,
hint::{draw_shortcuts, SHORTCUT_LINES},
running_command::RunningCommand,
theme::Theme,
@ -415,11 +415,11 @@ impl AppState {
}
}
fn toggle_selection(&mut self) {
if let Some(command) = self.get_selected_command() {
if self.selected_commands.contains(&command) {
self.selected_commands.retain(|c| c != &command);
if let Some(list_node) = self.get_selected_list_node() {
if self.selected_commands.contains(&list_node.command) {
self.selected_commands.retain(|c| c != &list_node.command);
} else {
self.selected_commands.push(command);
self.selected_commands.push(list_node.command);
}
}
}
@ -470,12 +470,8 @@ impl AppState {
}
None
}
pub fn get_selected_command(&self) -> Option<Command> {
self.get_selected_node().map(|node| node.command.clone())
}
fn get_selected_description(&self) -> Option<String> {
self.get_selected_node()
.map(|node| node.description.clone())
pub fn get_selected_list_node(&self) -> Option<ListNode> {
self.get_selected_node().cloned()
}
pub fn go_to_selected_dir(&mut self) {
let mut selected_index = self.selection.selected().unwrap_or(0);
@ -524,15 +520,17 @@ impl AppState {
!self.at_root() && selected_index == 0
}
fn enable_preview(&mut self) {
if let Some(command) = self.get_selected_command() {
if let Some(preview) = FloatingText::from_command(&command, FloatingTextMode::Preview) {
if let Some(list_node) = self.get_selected_list_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);
if let Some(list_node) = self.get_selected_list_node() {
let description = FloatingText::new(list_node.description, "Command Description");
self.spawn_float(description, 80, 80);
}
}
@ -540,8 +538,8 @@ impl AppState {
fn handle_enter(&mut self) {
if self.selected_item_is_cmd() {
if self.selected_commands.is_empty() {
if let Some(cmd) = self.get_selected_command() {
self.selected_commands.push(cmd);
if let Some(list_node) = self.get_selected_list_node() {
self.selected_commands.push(list_node.command);
}
}
let command = RunningCommand::new(self.selected_commands.clone());
@ -576,7 +574,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,
);