2024-08-09 10:22:19 +01:00
|
|
|
use crate::{
|
2024-09-30 22:48:22 +01:00
|
|
|
confirmation::{ConfirmPrompt, ConfirmStatus},
|
2024-08-21 17:48:14 +01:00
|
|
|
filter::{Filter, SearchAction},
|
2024-08-09 10:22:19 +01:00
|
|
|
float::{Float, FloatContent},
|
2024-09-23 03:31:52 +01:00
|
|
|
floating_text::FloatingText,
|
2024-09-28 20:05:19 +01:00
|
|
|
hint::{create_shortcut_list, Shortcut},
|
2024-11-11 16:27:30 +00:00
|
|
|
root::check_root_status,
|
2024-09-06 00:39:10 +01:00
|
|
|
running_command::RunningCommand,
|
2024-08-09 10:22:19 +01:00
|
|
|
theme::Theme,
|
|
|
|
};
|
2024-11-07 19:00:34 +00:00
|
|
|
|
2024-11-07 19:21:37 +00:00
|
|
|
use linutil_core::{ego_tree::NodeId, Config, ListNode, TabList};
|
2024-09-20 16:48:09 +01:00
|
|
|
#[cfg(feature = "tips")]
|
2024-09-20 16:09:08 +01:00
|
|
|
use rand::Rng;
|
2024-08-09 10:22:19 +01:00
|
|
|
use ratatui::{
|
2024-11-11 16:22:21 +00:00
|
|
|
crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEvent, MouseEventKind},
|
|
|
|
layout::{Alignment, Constraint, Direction, Flex, Layout, Position, Rect},
|
2024-08-18 20:51:47 +01:00
|
|
|
style::{Style, Stylize},
|
2024-09-28 20:05:19 +01:00
|
|
|
text::{Line, Span, Text},
|
2024-09-07 15:25:56 +01:00
|
|
|
widgets::{Block, Borders, List, ListState, Paragraph},
|
2024-08-09 10:22:19 +01:00
|
|
|
Frame,
|
|
|
|
};
|
2024-11-07 19:21:37 +00:00
|
|
|
use std::path::PathBuf;
|
2024-10-03 21:07:12 +01:00
|
|
|
use std::rc::Rc;
|
2024-07-28 16:17:06 +01:00
|
|
|
|
2024-10-31 18:25:16 +00:00
|
|
|
const MIN_WIDTH: u16 = 100;
|
|
|
|
const MIN_HEIGHT: u16 = 25;
|
2024-10-31 19:14:17 +00:00
|
|
|
const TITLE: &str = concat!(" Linux Toolbox - ", env!("CARGO_PKG_VERSION"), " ");
|
2024-09-22 20:08:27 +01:00
|
|
|
const ACTIONS_GUIDE: &str = "List of important tasks performed by commands' names:
|
|
|
|
|
|
|
|
D - disk modifications (ex. partitioning) (privileged)
|
|
|
|
FI - flatpak installation
|
|
|
|
FM - file modification
|
|
|
|
I - installation (privileged)
|
2024-11-06 22:20:16 +00:00
|
|
|
K - kernel modifications (privileged)
|
2024-09-22 20:08:27 +01:00
|
|
|
MP - package manager actions
|
|
|
|
SI - full system installation
|
2024-10-03 20:20:50 +01:00
|
|
|
SS - systemd actions (privileged)
|
2024-09-22 20:08:27 +01:00
|
|
|
RP - package removal
|
|
|
|
|
|
|
|
P* - privileged *
|
|
|
|
";
|
2024-09-19 19:18:55 +01:00
|
|
|
|
2024-07-28 16:17:06 +01:00
|
|
|
pub struct AppState {
|
2024-11-11 16:22:21 +00:00
|
|
|
/// Areas of tabs
|
|
|
|
areas: Option<Areas>,
|
2024-07-28 16:17:06 +01:00
|
|
|
/// Selected theme
|
2024-08-09 10:22:19 +01:00
|
|
|
theme: Theme,
|
|
|
|
/// Currently focused area
|
2024-09-06 22:36:12 +01:00
|
|
|
pub focus: Focus,
|
2024-08-11 03:52:49 +01:00
|
|
|
/// List of tabs
|
2024-11-06 21:28:17 +00:00
|
|
|
tabs: TabList,
|
2024-08-09 10:22:19 +01:00
|
|
|
/// Current tab
|
|
|
|
current_tab: ListState,
|
2024-09-14 16:30:03 +01:00
|
|
|
/// This stack keeps track of our "current directory". You can think of it as `pwd`. but not
|
2024-08-09 10:22:19 +01:00
|
|
|
/// just the current directory, all paths that took us here, so we can "cd .."
|
2024-10-31 18:30:22 +00:00
|
|
|
visit_stack: Vec<(NodeId, usize)>,
|
2024-10-08 02:42:03 +01:00
|
|
|
/// This is the state associated with the list widget, used to display the selection in the
|
2024-08-09 10:22:19 +01:00
|
|
|
/// widget
|
|
|
|
selection: ListState,
|
2024-08-21 17:48:14 +01:00
|
|
|
filter: Filter,
|
2024-09-19 19:15:30 +01:00
|
|
|
multi_select: bool,
|
2024-09-30 22:48:22 +01:00
|
|
|
selected_commands: Vec<Rc<ListNode>>,
|
2024-09-18 19:49:33 +01:00
|
|
|
drawable: bool,
|
2024-09-20 16:48:09 +01:00
|
|
|
#[cfg(feature = "tips")]
|
2024-10-31 19:14:17 +00:00
|
|
|
tip: String,
|
2024-11-06 15:49:26 +00:00
|
|
|
size_bypass: bool,
|
2024-11-07 19:09:18 +00:00
|
|
|
skip_confirmation: bool,
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Focus {
|
|
|
|
Search,
|
|
|
|
TabList,
|
|
|
|
List,
|
2024-09-30 22:48:22 +01:00
|
|
|
FloatingWindow(Float<dyn FloatContent>),
|
|
|
|
ConfirmationPrompt(Float<ConfirmPrompt>),
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
|
2024-08-18 20:51:47 +01:00
|
|
|
pub struct ListEntry {
|
2024-09-30 22:48:22 +01:00
|
|
|
pub node: Rc<ListNode>,
|
2024-08-18 20:51:47 +01:00
|
|
|
pub id: NodeId,
|
|
|
|
pub has_children: bool,
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 16:22:21 +00:00
|
|
|
pub struct Areas {
|
|
|
|
tab_list: Rect,
|
|
|
|
list: Rect,
|
|
|
|
}
|
|
|
|
|
2024-10-31 18:30:22 +00:00
|
|
|
enum SelectedItem {
|
|
|
|
UpDir,
|
|
|
|
Directory,
|
|
|
|
Command,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
impl AppState {
|
2024-11-07 19:09:18 +00:00
|
|
|
pub fn new(
|
2024-11-07 19:21:37 +00:00
|
|
|
config_path: Option<PathBuf>,
|
2024-11-07 19:09:18 +00:00
|
|
|
theme: Theme,
|
|
|
|
override_validation: bool,
|
|
|
|
size_bypass: bool,
|
|
|
|
skip_confirmation: bool,
|
|
|
|
) -> Self {
|
2024-11-06 21:28:17 +00:00
|
|
|
let tabs = linutil_core::get_tabs(!override_validation);
|
2024-08-11 03:52:49 +01:00
|
|
|
let root_id = tabs[0].tree.root().id();
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-11-07 19:21:37 +00:00
|
|
|
let auto_execute_commands = config_path.map(|path| Config::from_file(&path).auto_execute);
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
let mut state = Self {
|
2024-11-11 16:22:21 +00:00
|
|
|
areas: None,
|
2024-08-09 10:22:19 +01:00
|
|
|
theme,
|
|
|
|
focus: Focus::List,
|
2024-08-11 03:52:49 +01:00
|
|
|
tabs,
|
2024-08-09 10:22:19 +01:00
|
|
|
current_tab: ListState::default().with_selected(Some(0)),
|
2024-10-31 18:30:22 +00:00
|
|
|
visit_stack: vec![(root_id, 0usize)],
|
2024-08-09 10:22:19 +01:00
|
|
|
selection: ListState::default().with_selected(Some(0)),
|
2024-08-21 17:48:14 +01:00
|
|
|
filter: Filter::new(),
|
2024-09-19 19:15:30 +01:00
|
|
|
multi_select: false,
|
|
|
|
selected_commands: Vec::new(),
|
2024-09-18 19:49:33 +01:00
|
|
|
drawable: false,
|
2024-09-20 16:48:09 +01:00
|
|
|
#[cfg(feature = "tips")]
|
2024-09-21 01:12:26 +01:00
|
|
|
tip: get_random_tip(),
|
2024-11-06 15:49:26 +00:00
|
|
|
size_bypass,
|
2024-11-07 19:09:18 +00:00
|
|
|
skip_confirmation,
|
2024-08-09 10:22:19 +01:00
|
|
|
};
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-11-11 16:27:30 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
if let Some(root_warning) = check_root_status() {
|
|
|
|
state.spawn_float(root_warning, 60, 40);
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
state.update_items();
|
2024-11-07 19:21:37 +00:00
|
|
|
if let Some(auto_execute_commands) = auto_execute_commands {
|
|
|
|
state.handle_initial_auto_execute(&auto_execute_commands);
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
state
|
|
|
|
}
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-11-07 19:21:37 +00:00
|
|
|
fn handle_initial_auto_execute(&mut self, auto_execute_commands: &[String]) {
|
|
|
|
self.selected_commands = auto_execute_commands
|
|
|
|
.iter()
|
|
|
|
.filter_map(|name| self.tabs.iter().find_map(|tab| tab.find_command(name)))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if !self.selected_commands.is_empty() {
|
|
|
|
let cmd_names: Vec<_> = self
|
|
|
|
.selected_commands
|
|
|
|
.iter()
|
|
|
|
.map(|node| node.name.as_str())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let prompt = ConfirmPrompt::new(&cmd_names);
|
|
|
|
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
fn get_list_item_shortcut(&self) -> Box<[Shortcut]> {
|
|
|
|
if self.selected_item_is_dir() {
|
|
|
|
Box::new([Shortcut::new("Go to selected dir", ["l", "Right", "Enter"])])
|
|
|
|
} else {
|
|
|
|
Box::new([
|
|
|
|
Shortcut::new("Run selected command", ["l", "Right", "Enter"]),
|
|
|
|
Shortcut::new("Enable preview", ["p"]),
|
|
|
|
Shortcut::new("Command Description", ["d"]),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_keybinds(&self) -> (&str, Box<[Shortcut]>) {
|
|
|
|
match self.focus {
|
|
|
|
Focus::Search => (
|
|
|
|
"Search bar",
|
2024-10-24 20:43:16 +01:00
|
|
|
Box::new([
|
|
|
|
Shortcut::new("Abort search", ["Esc", "CTRL-c"]),
|
|
|
|
Shortcut::new("Search", ["Enter"]),
|
|
|
|
]),
|
2024-09-28 20:05:19 +01:00
|
|
|
),
|
|
|
|
|
|
|
|
Focus::List => {
|
|
|
|
let mut hints = Vec::new();
|
|
|
|
hints.push(Shortcut::new("Exit linutil", ["q", "CTRL-c"]));
|
|
|
|
|
|
|
|
if self.at_root() {
|
|
|
|
hints.push(Shortcut::new("Focus tab list", ["h", "Left"]));
|
|
|
|
hints.extend(self.get_list_item_shortcut());
|
|
|
|
} else if self.selected_item_is_up_dir() {
|
|
|
|
hints.push(Shortcut::new(
|
|
|
|
"Go to parent directory",
|
|
|
|
["l", "Right", "Enter", "h", "Left"],
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
hints.push(Shortcut::new("Go to parent directory", ["h", "Left"]));
|
|
|
|
hints.extend(self.get_list_item_shortcut());
|
|
|
|
}
|
|
|
|
|
|
|
|
hints.push(Shortcut::new("Select item above", ["k", "Up"]));
|
|
|
|
hints.push(Shortcut::new("Select item below", ["j", "Down"]));
|
|
|
|
hints.push(Shortcut::new("Next theme", ["t"]));
|
|
|
|
hints.push(Shortcut::new("Previous theme", ["T"]));
|
2024-11-05 19:59:57 +00:00
|
|
|
hints.push(Shortcut::new("Multi-selection mode", ["v"]));
|
|
|
|
if self.multi_select {
|
2024-09-28 20:05:19 +01:00
|
|
|
hints.push(Shortcut::new("Select multiple commands", ["Space"]));
|
|
|
|
}
|
|
|
|
hints.push(Shortcut::new("Next tab", ["Tab"]));
|
|
|
|
hints.push(Shortcut::new("Previous tab", ["Shift-Tab"]));
|
|
|
|
hints.push(Shortcut::new("Important actions guide", ["g"]));
|
|
|
|
|
|
|
|
("Command list", hints.into_boxed_slice())
|
|
|
|
}
|
|
|
|
|
|
|
|
Focus::TabList => (
|
|
|
|
"Tab list",
|
|
|
|
Box::new([
|
|
|
|
Shortcut::new("Exit linutil", ["q", "CTRL-c"]),
|
|
|
|
Shortcut::new("Focus action list", ["l", "Right", "Enter"]),
|
|
|
|
Shortcut::new("Select item above", ["k", "Up"]),
|
|
|
|
Shortcut::new("Select item below", ["j", "Down"]),
|
|
|
|
Shortcut::new("Next theme", ["t"]),
|
|
|
|
Shortcut::new("Previous theme", ["T"]),
|
|
|
|
Shortcut::new("Next tab", ["Tab"]),
|
|
|
|
Shortcut::new("Previous tab", ["Shift-Tab"]),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
|
|
|
|
Focus::FloatingWindow(ref float) => float.get_shortcut_list(),
|
2024-09-30 22:48:22 +01:00
|
|
|
Focus::ConfirmationPrompt(ref prompt) => prompt.get_shortcut_list(),
|
2024-09-28 20:05:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
pub fn draw(&mut self, frame: &mut Frame) {
|
2024-09-18 19:49:33 +01:00
|
|
|
let terminal_size = frame.area();
|
|
|
|
|
2024-11-06 15:49:26 +00:00
|
|
|
if !self.size_bypass
|
|
|
|
&& (terminal_size.height < MIN_HEIGHT || terminal_size.width < MIN_WIDTH)
|
|
|
|
{
|
2024-09-22 17:51:25 +01:00
|
|
|
let warning = Paragraph::new(format!(
|
2024-09-18 19:49:33 +01:00
|
|
|
"Terminal size too small:\nWidth = {} Height = {}\n\nMinimum size:\nWidth = {} Height = {}",
|
|
|
|
terminal_size.width,
|
|
|
|
terminal_size.height,
|
2024-09-19 19:18:55 +01:00
|
|
|
MIN_WIDTH,
|
|
|
|
MIN_HEIGHT,
|
2024-09-22 17:51:25 +01:00
|
|
|
))
|
2024-09-18 19:49:33 +01:00
|
|
|
.alignment(Alignment::Center)
|
|
|
|
.style(Style::default().fg(ratatui::style::Color::Red).bold())
|
|
|
|
.wrap(ratatui::widgets::Wrap { trim: true });
|
|
|
|
|
2024-09-22 17:51:25 +01:00
|
|
|
let centered_layout = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints([
|
|
|
|
Constraint::Fill(1),
|
|
|
|
Constraint::Length(5),
|
|
|
|
Constraint::Fill(1),
|
|
|
|
])
|
|
|
|
.split(terminal_size);
|
2024-09-18 19:49:33 +01:00
|
|
|
|
|
|
|
self.drawable = false;
|
2024-09-22 17:51:25 +01:00
|
|
|
return frame.render_widget(warning, centered_layout[1]);
|
2024-09-18 19:49:33 +01:00
|
|
|
} else {
|
|
|
|
self.drawable = true;
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:01:19 +00:00
|
|
|
let label_block = Block::default()
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_set(ratatui::symbols::border::ROUNDED)
|
|
|
|
.border_set(ratatui::symbols::border::Set {
|
|
|
|
top_left: " ",
|
|
|
|
top_right: " ",
|
|
|
|
bottom_left: " ",
|
|
|
|
bottom_right: " ",
|
|
|
|
vertical_left: " ",
|
|
|
|
vertical_right: " ",
|
|
|
|
horizontal_top: "*",
|
|
|
|
horizontal_bottom: "*",
|
|
|
|
});
|
2024-09-07 15:25:56 +01:00
|
|
|
let str1 = "Linutil ";
|
|
|
|
let str2 = "by Chris Titus";
|
|
|
|
let label = Paragraph::new(Line::from(vec![
|
|
|
|
Span::styled(str1, Style::default().bold()),
|
|
|
|
Span::styled(str2, Style::default().italic()),
|
|
|
|
]))
|
|
|
|
.block(label_block)
|
|
|
|
.alignment(Alignment::Center);
|
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
let longest_tab_display_len = self
|
|
|
|
.tabs
|
2024-08-09 10:22:19 +01:00
|
|
|
.iter()
|
2024-08-15 23:13:47 +01:00
|
|
|
.map(|tab| tab.name.len() + self.theme.tab_icon().len())
|
2024-08-09 10:22:19 +01:00
|
|
|
.max()
|
2024-09-07 15:25:56 +01:00
|
|
|
.unwrap_or(0)
|
|
|
|
.max(str1.len() + str2.len());
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
let (keybind_scope, shortcuts) = self.get_keybinds();
|
|
|
|
|
|
|
|
let keybind_render_width = terminal_size.width - 2;
|
|
|
|
|
|
|
|
let keybinds_block = Block::default()
|
|
|
|
.title(format!(" {} ", keybind_scope))
|
2024-11-06 20:01:19 +00:00
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_set(ratatui::symbols::border::ROUNDED);
|
2024-09-28 20:05:19 +01:00
|
|
|
|
|
|
|
let keybinds = create_shortcut_list(shortcuts, keybind_render_width);
|
|
|
|
let n_lines = keybinds.len() as u16;
|
|
|
|
|
|
|
|
let keybind_para = Paragraph::new(Text::from_iter(keybinds)).block(keybinds_block);
|
|
|
|
|
2024-09-06 22:36:12 +01:00
|
|
|
let vertical = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints([
|
2024-09-28 20:05:19 +01:00
|
|
|
Constraint::Percentage(0),
|
|
|
|
Constraint::Max(n_lines as u16 + 2),
|
2024-09-06 22:36:12 +01:00
|
|
|
])
|
2024-09-28 20:05:19 +01:00
|
|
|
.flex(Flex::Legacy)
|
2024-09-06 22:36:12 +01:00
|
|
|
.margin(0)
|
2024-09-10 22:00:03 +01:00
|
|
|
.split(frame.area());
|
2024-08-09 10:22:19 +01:00
|
|
|
|
|
|
|
let horizontal = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([
|
|
|
|
Constraint::Min(longest_tab_display_len as u16 + 5),
|
|
|
|
Constraint::Percentage(100),
|
|
|
|
])
|
2024-09-06 22:36:12 +01:00
|
|
|
.split(vertical[0]);
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
let left_chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints([Constraint::Length(3), Constraint::Min(1)])
|
|
|
|
.split(horizontal[0]);
|
2024-09-07 15:25:56 +01:00
|
|
|
frame.render_widget(label, left_chunks[0]);
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-11-11 16:22:21 +00:00
|
|
|
self.areas = Some(Areas {
|
|
|
|
tab_list: left_chunks[1],
|
|
|
|
list: horizontal[1],
|
|
|
|
});
|
|
|
|
|
2024-08-11 03:52:49 +01:00
|
|
|
let tabs = self
|
|
|
|
.tabs
|
|
|
|
.iter()
|
|
|
|
.map(|tab| tab.name.as_str())
|
|
|
|
.collect::<Vec<_>>();
|
2024-08-09 10:22:19 +01:00
|
|
|
|
|
|
|
let tab_hl_style = if let Focus::TabList = self.focus {
|
2024-08-15 23:13:47 +01:00
|
|
|
Style::default().reversed().fg(self.theme.tab_color())
|
2024-08-09 10:22:19 +01:00
|
|
|
} else {
|
2024-08-15 23:13:47 +01:00
|
|
|
Style::new().fg(self.theme.tab_color())
|
2024-08-09 10:22:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let list = List::new(tabs)
|
2024-11-06 20:01:19 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_set(ratatui::symbols::border::ROUNDED),
|
|
|
|
)
|
2024-08-09 10:22:19 +01:00
|
|
|
.highlight_style(tab_hl_style)
|
2024-08-15 23:13:47 +01:00
|
|
|
.highlight_symbol(self.theme.tab_icon());
|
2024-08-09 10:22:19 +01:00
|
|
|
frame.render_stateful_widget(list, left_chunks[1], &mut self.current_tab);
|
|
|
|
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints([Constraint::Length(3), Constraint::Min(1)].as_ref())
|
|
|
|
.split(horizontal[1]);
|
|
|
|
|
2024-09-22 17:10:05 +01:00
|
|
|
let list_chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
2024-09-22 20:08:27 +01:00
|
|
|
.constraints([Constraint::Percentage(70), Constraint::Percentage(30)].as_ref())
|
2024-09-22 17:10:05 +01:00
|
|
|
.split(chunks[1]);
|
|
|
|
|
2024-08-21 17:48:14 +01:00
|
|
|
self.filter.draw_searchbar(frame, chunks[0], &self.theme);
|
2024-08-09 10:22:19 +01:00
|
|
|
|
|
|
|
let mut items: Vec<Line> = Vec::new();
|
2024-09-22 17:10:05 +01:00
|
|
|
let mut task_items: Vec<Line> = Vec::new();
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
if !self.at_root() {
|
|
|
|
items.push(
|
2024-08-15 23:13:47 +01:00
|
|
|
Line::from(format!("{} ..", self.theme.dir_icon())).style(self.theme.dir_color()),
|
2024-08-09 10:22:19 +01:00
|
|
|
);
|
2024-09-22 17:10:05 +01:00
|
|
|
task_items.push(Line::from(" ").style(self.theme.dir_color()));
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
|
2024-08-18 20:51:47 +01:00
|
|
|
items.extend(self.filter.item_list().iter().map(
|
2024-08-09 10:22:19 +01:00
|
|
|
|ListEntry {
|
|
|
|
node, has_children, ..
|
|
|
|
}| {
|
2024-09-30 22:48:22 +01:00
|
|
|
let is_selected = self.selected_commands.contains(node);
|
2024-09-19 19:15:30 +01:00
|
|
|
let (indicator, style) = if is_selected {
|
|
|
|
(self.theme.multi_select_icon(), Style::default().bold())
|
|
|
|
} else {
|
2024-11-05 19:59:57 +00:00
|
|
|
let ms_style = if self.multi_select && !node.multi_select {
|
|
|
|
Style::default().fg(self.theme.multi_select_disabled_color())
|
|
|
|
} else {
|
|
|
|
Style::new()
|
|
|
|
};
|
|
|
|
("", ms_style)
|
2024-09-19 19:15:30 +01:00
|
|
|
};
|
2024-08-09 10:22:19 +01:00
|
|
|
if *has_children {
|
2024-09-19 19:15:30 +01:00
|
|
|
Line::from(format!(
|
|
|
|
"{} {} {}",
|
|
|
|
self.theme.dir_icon(),
|
|
|
|
node.name,
|
|
|
|
indicator
|
|
|
|
))
|
|
|
|
.style(self.theme.dir_color())
|
2024-11-05 19:59:57 +00:00
|
|
|
.patch_style(style)
|
2024-08-09 10:22:19 +01:00
|
|
|
} else {
|
2024-09-19 19:15:30 +01:00
|
|
|
Line::from(format!(
|
|
|
|
"{} {} {}",
|
|
|
|
self.theme.cmd_icon(),
|
|
|
|
node.name,
|
|
|
|
indicator
|
|
|
|
))
|
|
|
|
.style(self.theme.cmd_color())
|
|
|
|
.patch_style(style)
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
2024-09-22 17:10:05 +01:00
|
|
|
task_items.extend(self.filter.item_list().iter().map(
|
|
|
|
|ListEntry {
|
|
|
|
node, has_children, ..
|
|
|
|
}| {
|
2024-11-05 19:59:57 +00:00
|
|
|
let ms_style = if self.multi_select && !node.multi_select {
|
|
|
|
Style::default().fg(self.theme.multi_select_disabled_color())
|
|
|
|
} else {
|
|
|
|
Style::new()
|
|
|
|
};
|
2024-09-22 17:10:05 +01:00
|
|
|
if *has_children {
|
2024-11-05 19:59:57 +00:00
|
|
|
Line::from(" ")
|
|
|
|
.style(self.theme.dir_color())
|
|
|
|
.patch_style(ms_style)
|
2024-09-22 17:10:05 +01:00
|
|
|
} else {
|
|
|
|
Line::from(format!("{} ", node.task_list))
|
|
|
|
.alignment(Alignment::Right)
|
|
|
|
.style(self.theme.cmd_color())
|
|
|
|
.bold()
|
2024-11-05 19:59:57 +00:00
|
|
|
.patch_style(ms_style)
|
2024-09-22 17:10:05 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
2024-09-19 19:44:04 +01:00
|
|
|
let style = if let Focus::List = self.focus {
|
2024-09-19 19:18:55 +01:00
|
|
|
Style::default().reversed()
|
|
|
|
} else {
|
|
|
|
Style::new()
|
|
|
|
};
|
|
|
|
|
2024-09-21 15:08:24 +01:00
|
|
|
let title = if self.multi_select {
|
2024-10-31 19:14:17 +00:00
|
|
|
&format!("{}[Multi-Select] ", TITLE)
|
2024-09-21 15:08:24 +01:00
|
|
|
} else {
|
|
|
|
TITLE
|
|
|
|
};
|
2024-09-19 19:41:23 +01:00
|
|
|
|
2024-09-20 16:48:09 +01:00
|
|
|
#[cfg(feature = "tips")]
|
2024-10-31 19:14:17 +00:00
|
|
|
let bottom_title = Line::from(self.tip.as_str().bold().blue()).right_aligned();
|
2024-09-20 16:48:09 +01:00
|
|
|
#[cfg(not(feature = "tips"))]
|
|
|
|
let bottom_title = "";
|
|
|
|
|
2024-10-31 19:14:17 +00:00
|
|
|
let task_list_title = Line::from(" Important Actions ").right_aligned();
|
2024-09-22 17:10:05 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
// Create the list widget with items
|
|
|
|
let list = List::new(items)
|
2024-09-19 19:41:23 +01:00
|
|
|
.highlight_style(style)
|
2024-09-20 16:09:08 +01:00
|
|
|
.block(
|
|
|
|
Block::default()
|
2024-09-22 17:10:05 +01:00
|
|
|
.borders(Borders::ALL & !Borders::RIGHT)
|
2024-11-06 20:01:19 +00:00
|
|
|
.border_set(ratatui::symbols::border::ROUNDED)
|
2024-09-20 16:09:08 +01:00
|
|
|
.title(title)
|
2024-09-20 16:48:09 +01:00
|
|
|
.title_bottom(bottom_title),
|
2024-09-20 16:09:08 +01:00
|
|
|
)
|
2024-08-09 10:22:19 +01:00
|
|
|
.scroll_padding(1);
|
2024-09-22 17:10:05 +01:00
|
|
|
frame.render_stateful_widget(list, list_chunks[0], &mut self.selection);
|
|
|
|
|
|
|
|
let disclaimer_list = List::new(task_items).highlight_style(style).block(
|
|
|
|
Block::default()
|
|
|
|
.borders(Borders::ALL & !Borders::LEFT)
|
2024-11-06 20:01:19 +00:00
|
|
|
.border_set(ratatui::symbols::border::ROUNDED)
|
2024-09-22 17:10:05 +01:00
|
|
|
.title(task_list_title),
|
|
|
|
);
|
|
|
|
|
|
|
|
frame.render_stateful_widget(disclaimer_list, list_chunks[1], &mut self.selection);
|
2024-08-09 10:22:19 +01:00
|
|
|
|
2024-09-30 22:48:22 +01:00
|
|
|
match &mut self.focus {
|
|
|
|
Focus::FloatingWindow(float) => float.draw(frame, chunks[1]),
|
|
|
|
Focus::ConfirmationPrompt(prompt) => prompt.draw(frame, chunks[1]),
|
|
|
|
_ => {}
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-06 22:36:12 +01:00
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
frame.render_widget(keybind_para, vertical[1]);
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-11-11 16:22:21 +00:00
|
|
|
pub fn handle_mouse(&mut self, event: &MouseEvent) -> bool {
|
|
|
|
if !self.drawable {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches!(self.focus, Focus::TabList | Focus::List) {
|
|
|
|
let position = Position::new(event.column, event.row);
|
|
|
|
let mouse_in_tab_list = self.areas.as_ref().unwrap().tab_list.contains(position);
|
|
|
|
let mouse_in_list = self.areas.as_ref().unwrap().list.contains(position);
|
|
|
|
|
|
|
|
match event.kind {
|
|
|
|
MouseEventKind::Moved => {
|
|
|
|
if mouse_in_list {
|
|
|
|
self.focus = Focus::List
|
|
|
|
} else if mouse_in_tab_list {
|
|
|
|
self.focus = Focus::TabList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MouseEventKind::ScrollDown => {
|
|
|
|
if mouse_in_tab_list {
|
|
|
|
if self.current_tab.selected().unwrap() != self.tabs.len() - 1 {
|
|
|
|
self.current_tab.select_next();
|
|
|
|
}
|
|
|
|
self.refresh_tab();
|
|
|
|
} else if mouse_in_list {
|
|
|
|
self.selection.select_next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MouseEventKind::ScrollUp => {
|
|
|
|
if mouse_in_tab_list {
|
|
|
|
if self.current_tab.selected().unwrap() != 0 {
|
|
|
|
self.current_tab.select_previous();
|
|
|
|
}
|
|
|
|
self.refresh_tab();
|
|
|
|
} else if mouse_in_list {
|
|
|
|
self.selection.select_previous()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match &mut self.focus {
|
|
|
|
Focus::FloatingWindow(float) => {
|
|
|
|
float.content.handle_mouse_event(event);
|
|
|
|
}
|
|
|
|
Focus::ConfirmationPrompt(confirm) => {
|
|
|
|
confirm.content.handle_mouse_event(event);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
pub fn handle_key(&mut self, key: &KeyEvent) -> bool {
|
2024-09-18 19:49:33 +01:00
|
|
|
// This should be defined first to allow closing
|
|
|
|
// the application even when not drawable ( If terminal is small )
|
2024-09-19 19:18:55 +01:00
|
|
|
// Exit on 'q' or 'Ctrl-c' input
|
2024-10-31 18:49:08 +00:00
|
|
|
if matches!(self.focus, Focus::TabList | Focus::List)
|
|
|
|
&& (key.code == KeyCode::Char('q')
|
|
|
|
|| key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c'))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches!(self.focus, Focus::ConfirmationPrompt(_))
|
|
|
|
&& (key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c'))
|
2024-09-19 19:18:55 +01:00
|
|
|
{
|
|
|
|
return false;
|
2024-09-18 19:49:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If UI is not drawable returning true will mark as the key handled
|
|
|
|
if !self.drawable {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-09-19 03:22:43 +01:00
|
|
|
// Handle key only when Tablist or List is focused
|
|
|
|
// Prevents exiting the application even when a command is running
|
|
|
|
// Add keys here which should work on both TabList and List
|
|
|
|
if matches!(self.focus, Focus::TabList | Focus::List) {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Tab => {
|
|
|
|
if self.current_tab.selected().unwrap() == self.tabs.len() - 1 {
|
2024-09-19 19:15:30 +01:00
|
|
|
self.current_tab.select_first();
|
2024-09-19 03:22:43 +01:00
|
|
|
} else {
|
|
|
|
self.current_tab.select_next();
|
|
|
|
}
|
|
|
|
self.refresh_tab();
|
|
|
|
}
|
|
|
|
KeyCode::BackTab => {
|
|
|
|
if self.current_tab.selected().unwrap() == 0 {
|
2024-09-19 19:15:30 +01:00
|
|
|
self.current_tab.select(Some(self.tabs.len() - 1));
|
2024-09-19 03:22:43 +01:00
|
|
|
} else {
|
|
|
|
self.current_tab.select_previous();
|
|
|
|
}
|
|
|
|
self.refresh_tab();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
match &mut self.focus {
|
|
|
|
Focus::FloatingWindow(command) => {
|
|
|
|
if command.handle_key_event(key) {
|
|
|
|
self.focus = Focus::List;
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-09-30 22:48:22 +01:00
|
|
|
Focus::ConfirmationPrompt(confirm) => {
|
|
|
|
confirm.content.handle_key_event(key);
|
|
|
|
match confirm.content.status {
|
|
|
|
ConfirmStatus::Abort => {
|
|
|
|
self.focus = Focus::List;
|
|
|
|
// selected command was pushed to selection list if multi-select was
|
|
|
|
// enabled, need to clear it to prevent state corruption
|
|
|
|
if !self.multi_select {
|
|
|
|
self.selected_commands.clear()
|
2024-11-05 19:59:57 +00:00
|
|
|
} else {
|
|
|
|
// Prevents non multi_selectable cmd from being pushed into the selected list
|
|
|
|
if let Some(node) = self.get_selected_node() {
|
|
|
|
if !node.multi_select {
|
|
|
|
self.selected_commands.retain(|cmd| cmd.name != node.name);
|
|
|
|
}
|
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ConfirmStatus::Confirm => self.handle_confirm_command(),
|
|
|
|
ConfirmStatus::None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-18 20:51:47 +01:00
|
|
|
Focus::Search => match self.filter.handle_key(key) {
|
|
|
|
SearchAction::Exit => self.exit_search(),
|
|
|
|
SearchAction::Update => self.update_items(),
|
2024-09-22 17:50:43 +01:00
|
|
|
SearchAction::None => {}
|
2024-08-18 20:51:47 +01:00
|
|
|
},
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
Focus::TabList => match key.code {
|
2024-09-19 03:22:43 +01:00
|
|
|
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.focus = Focus::List,
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-10-31 18:35:53 +00:00
|
|
|
KeyCode::Char('j') | KeyCode::Down => self.scroll_tab_down(),
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-10-31 18:35:53 +00:00
|
|
|
KeyCode::Char('k') | KeyCode::Up => self.scroll_tab_up(),
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
KeyCode::Char('/') => self.enter_search(),
|
2024-08-17 19:32:53 +01:00
|
|
|
KeyCode::Char('t') => self.theme.next(),
|
|
|
|
KeyCode::Char('T') => self.theme.prev(),
|
2024-09-22 20:08:27 +01:00
|
|
|
KeyCode::Char('g') => self.toggle_task_list_guide(),
|
2024-08-09 10:22:19 +01:00
|
|
|
_ => {}
|
|
|
|
},
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
Focus::List if key.kind != KeyEventKind::Release => match key.code {
|
2024-10-31 18:35:53 +00:00
|
|
|
KeyCode::Char('j') | KeyCode::Down => self.scroll_down(),
|
|
|
|
KeyCode::Char('k') | KeyCode::Up => self.scroll_up(),
|
2024-09-19 18:24:56 +01:00
|
|
|
KeyCode::Char('p') | KeyCode::Char('P') => self.enable_preview(),
|
|
|
|
KeyCode::Char('d') | KeyCode::Char('D') => self.enable_description(),
|
2024-08-09 10:22:19 +01:00
|
|
|
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.handle_enter(),
|
2024-09-28 20:05:19 +01:00
|
|
|
KeyCode::Char('h') | KeyCode::Left => self.go_back(),
|
2024-08-09 10:22:19 +01:00
|
|
|
KeyCode::Char('/') => self.enter_search(),
|
2024-08-17 19:32:53 +01:00
|
|
|
KeyCode::Char('t') => self.theme.next(),
|
|
|
|
KeyCode::Char('T') => self.theme.prev(),
|
2024-09-22 20:08:27 +01:00
|
|
|
KeyCode::Char('g') => self.toggle_task_list_guide(),
|
2024-09-19 19:15:30 +01:00
|
|
|
KeyCode::Char('v') | KeyCode::Char('V') => self.toggle_multi_select(),
|
|
|
|
KeyCode::Char(' ') if self.multi_select => self.toggle_selection(),
|
2024-08-09 10:22:19 +01:00
|
|
|
_ => {}
|
|
|
|
},
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-09-05 18:52:30 +01:00
|
|
|
_ => (),
|
2024-08-09 10:22:19 +01:00
|
|
|
};
|
|
|
|
true
|
|
|
|
}
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-10-31 18:35:53 +00:00
|
|
|
fn scroll_down(&mut self) {
|
|
|
|
let len = self.filter.item_list().len();
|
|
|
|
if len == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let current = self.selection.selected().unwrap_or(0);
|
|
|
|
let max_index = if self.at_root() { len - 1 } else { len };
|
|
|
|
let next = if current + 1 > max_index {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
current + 1
|
|
|
|
};
|
|
|
|
|
|
|
|
self.selection.select(Some(next));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scroll_up(&mut self) {
|
|
|
|
let len = self.filter.item_list().len();
|
|
|
|
if len == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let current = self.selection.selected().unwrap_or(0);
|
|
|
|
let max_index = if self.at_root() { len - 1 } else { len };
|
|
|
|
let next = if current == 0 { max_index } else { current - 1 };
|
|
|
|
|
|
|
|
self.selection.select(Some(next));
|
|
|
|
}
|
|
|
|
|
2024-09-19 19:15:30 +01:00
|
|
|
fn toggle_multi_select(&mut self) {
|
2024-11-05 19:59:57 +00:00
|
|
|
self.multi_select = !self.multi_select;
|
|
|
|
if !self.multi_select {
|
|
|
|
self.selected_commands.clear();
|
2024-09-19 19:15:30 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-09-19 19:15:30 +01:00
|
|
|
fn toggle_selection(&mut self) {
|
2024-11-05 19:59:57 +00:00
|
|
|
if let Some(node) = self.get_selected_node() {
|
|
|
|
if node.multi_select {
|
|
|
|
if self.selected_commands.contains(&node) {
|
|
|
|
self.selected_commands.retain(|c| c != &node);
|
|
|
|
} else {
|
|
|
|
self.selected_commands.push(node);
|
|
|
|
}
|
2024-09-19 19:15:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-08-18 20:51:47 +01:00
|
|
|
fn update_items(&mut self) {
|
|
|
|
self.filter.update_items(
|
|
|
|
&self.tabs,
|
|
|
|
self.current_tab.selected().unwrap(),
|
2024-10-31 18:30:22 +00:00
|
|
|
self.visit_stack.last().unwrap().0,
|
2024-08-18 20:51:47 +01:00
|
|
|
);
|
2024-11-05 19:59:57 +00:00
|
|
|
|
2024-10-31 18:35:53 +00:00
|
|
|
let len = self.filter.item_list().len();
|
|
|
|
if len > 0 {
|
|
|
|
let current = self.selection.selected().unwrap_or(0);
|
|
|
|
self.selection.select(Some(current.min(len - 1)));
|
|
|
|
} else {
|
|
|
|
self.selection.select(None);
|
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-09-14 16:30:03 +01:00
|
|
|
/// Checks either the current tree node is the root node (can we go up the tree or no)
|
2024-08-09 10:22:19 +01:00
|
|
|
/// Returns `true` if we can't go up the tree (we are at the tree root)
|
|
|
|
/// else returns `false`
|
2024-09-06 22:36:12 +01:00
|
|
|
pub fn at_root(&self) -> bool {
|
2024-08-09 10:22:19 +01:00
|
|
|
self.visit_stack.len() == 1
|
|
|
|
}
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-09-28 20:05:19 +01:00
|
|
|
fn go_back(&mut self) {
|
|
|
|
if self.at_root() {
|
|
|
|
self.focus = Focus::TabList;
|
|
|
|
} else {
|
|
|
|
self.enter_parent_directory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
fn enter_parent_directory(&mut self) {
|
2024-10-31 18:30:22 +00:00
|
|
|
if let Some((_, previous_position)) = self.visit_stack.pop() {
|
|
|
|
self.selection.select(Some(previous_position));
|
|
|
|
self.update_items();
|
|
|
|
}
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-28 20:05:19 +01:00
|
|
|
|
2024-09-30 22:48:22 +01:00
|
|
|
fn get_selected_node(&self) -> Option<Rc<ListNode>> {
|
2024-08-09 10:22:19 +01:00
|
|
|
let mut selected_index = self.selection.selected().unwrap_or(0);
|
|
|
|
|
|
|
|
if !self.at_root() && selected_index == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if !self.at_root() {
|
|
|
|
selected_index = selected_index.saturating_sub(1);
|
|
|
|
}
|
|
|
|
|
2024-08-18 20:51:47 +01:00
|
|
|
if let Some(item) = self.filter.item_list().get(selected_index) {
|
2024-08-09 10:22:19 +01:00
|
|
|
if !item.has_children {
|
2024-09-30 22:48:22 +01:00
|
|
|
return Some(item.node.clone());
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-09-19 19:18:55 +01:00
|
|
|
fn get_selected_description(&self) -> Option<String> {
|
|
|
|
self.get_selected_node()
|
|
|
|
.map(|node| node.description.clone())
|
2024-09-19 01:17:08 +01:00
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-09-06 22:36:12 +01:00
|
|
|
pub fn go_to_selected_dir(&mut self) {
|
2024-10-31 18:30:22 +00:00
|
|
|
let selected_index = self.selection.selected().unwrap_or(0);
|
2024-09-06 22:36:12 +01:00
|
|
|
|
|
|
|
if !self.at_root() && selected_index == 0 {
|
|
|
|
self.enter_parent_directory();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-31 18:30:22 +00:00
|
|
|
let actual_index = if self.at_root() {
|
|
|
|
selected_index
|
|
|
|
} else {
|
|
|
|
selected_index - 1
|
|
|
|
};
|
2024-09-06 22:36:12 +01:00
|
|
|
|
2024-10-31 18:30:22 +00:00
|
|
|
if let Some(item) = self.filter.item_list().get(actual_index) {
|
2024-09-06 22:36:12 +01:00
|
|
|
if item.has_children {
|
2024-10-31 18:30:22 +00:00
|
|
|
self.visit_stack.push((item.id, selected_index));
|
2024-08-09 10:22:19 +01:00
|
|
|
self.selection.select(Some(0));
|
|
|
|
self.update_items();
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-09-06 22:36:12 +01:00
|
|
|
pub fn selected_item_is_dir(&self) -> bool {
|
|
|
|
let mut selected_index = self.selection.selected().unwrap_or(0);
|
|
|
|
|
|
|
|
if !self.at_root() && selected_index == 0 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.at_root() {
|
|
|
|
selected_index = selected_index.saturating_sub(1);
|
|
|
|
}
|
|
|
|
|
2024-09-19 19:18:55 +01:00
|
|
|
self.filter
|
|
|
|
.item_list()
|
|
|
|
.get(selected_index)
|
|
|
|
.map_or(false, |item| item.has_children)
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn selected_item_is_cmd(&self) -> bool {
|
2024-09-22 17:11:43 +01:00
|
|
|
// Any item that is not a directory or up directory (..) must be a command
|
2024-09-30 21:23:16 +01:00
|
|
|
self.selection.selected().is_some()
|
|
|
|
&& !(self.selected_item_is_up_dir() || self.selected_item_is_dir())
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-09-06 22:36:12 +01:00
|
|
|
pub fn selected_item_is_up_dir(&self) -> bool {
|
|
|
|
let selected_index = self.selection.selected().unwrap_or(0);
|
|
|
|
!self.at_root() && selected_index == 0
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
fn enable_preview(&mut self) {
|
2024-10-03 21:07:12 +01:00
|
|
|
if let Some(list_node) = self.get_selected_node() {
|
2024-09-23 03:31:52 +01:00
|
|
|
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) {
|
2024-08-09 10:22:19 +01:00
|
|
|
self.spawn_float(preview, 80, 80);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-09-19 01:17:08 +01:00
|
|
|
fn enable_description(&mut self) {
|
|
|
|
if let Some(command_description) = self.get_selected_description() {
|
2024-10-31 18:42:06 +00:00
|
|
|
if !command_description.is_empty() {
|
2024-11-06 15:40:55 +00:00
|
|
|
let description =
|
|
|
|
FloatingText::new(command_description, "Command Description", true);
|
2024-10-31 18:42:06 +00:00
|
|
|
self.spawn_float(description, 80, 80);
|
|
|
|
}
|
2024-09-19 01:17:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-31 18:30:22 +00:00
|
|
|
fn get_selected_item_type(&self) -> SelectedItem {
|
|
|
|
if self.selected_item_is_up_dir() {
|
|
|
|
SelectedItem::UpDir
|
|
|
|
} else if self.selected_item_is_dir() {
|
|
|
|
SelectedItem::Directory
|
|
|
|
} else if self.selected_item_is_cmd() {
|
|
|
|
SelectedItem::Command
|
|
|
|
} else {
|
|
|
|
SelectedItem::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
fn handle_enter(&mut self) {
|
2024-10-31 18:30:22 +00:00
|
|
|
match self.get_selected_item_type() {
|
|
|
|
SelectedItem::UpDir => self.enter_parent_directory(),
|
|
|
|
SelectedItem::Directory => self.go_to_selected_dir(),
|
|
|
|
SelectedItem::Command => {
|
|
|
|
if self.selected_commands.is_empty() {
|
|
|
|
if let Some(node) = self.get_selected_node() {
|
|
|
|
self.selected_commands.push(node);
|
|
|
|
}
|
2024-09-19 19:15:30 +01:00
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-11-07 19:09:18 +00:00
|
|
|
if self.skip_confirmation {
|
|
|
|
self.handle_confirm_command();
|
|
|
|
} else {
|
|
|
|
let cmd_names = self
|
|
|
|
.selected_commands
|
|
|
|
.iter()
|
|
|
|
.map(|node| node.name.as_str())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let prompt = ConfirmPrompt::new(&cmd_names[..]);
|
|
|
|
self.focus = Focus::ConfirmationPrompt(Float::new(Box::new(prompt), 40, 40));
|
|
|
|
}
|
2024-10-31 18:30:22 +00:00
|
|
|
}
|
|
|
|
SelectedItem::None => {}
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
|
|
|
fn handle_confirm_command(&mut self) {
|
|
|
|
let commands = self
|
|
|
|
.selected_commands
|
|
|
|
.iter()
|
|
|
|
.map(|node| node.command.clone())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let command = RunningCommand::new(commands);
|
|
|
|
self.spawn_float(command, 80, 80);
|
|
|
|
self.selected_commands.clear();
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
fn spawn_float<T: FloatContent + 'static>(&mut self, float: T, width: u16, height: u16) {
|
|
|
|
self.focus = Focus::FloatingWindow(Float::new(Box::new(float), width, height));
|
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
fn enter_search(&mut self) {
|
|
|
|
self.focus = Focus::Search;
|
2024-08-18 20:51:47 +01:00
|
|
|
self.filter.activate_search();
|
2024-08-09 10:22:19 +01:00
|
|
|
self.selection.select(None);
|
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
fn exit_search(&mut self) {
|
|
|
|
self.selection.select(Some(0));
|
|
|
|
self.focus = Focus::List;
|
2024-08-18 20:51:47 +01:00
|
|
|
self.filter.deactivate_search();
|
2024-08-09 10:22:19 +01:00
|
|
|
self.update_items();
|
|
|
|
}
|
2024-09-30 22:48:22 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
fn refresh_tab(&mut self) {
|
2024-10-31 18:30:22 +00:00
|
|
|
self.visit_stack = vec![(
|
|
|
|
self.tabs[self.current_tab.selected().unwrap()]
|
|
|
|
.tree
|
|
|
|
.root()
|
|
|
|
.id(),
|
|
|
|
0usize,
|
|
|
|
)];
|
2024-08-09 10:22:19 +01:00
|
|
|
self.selection.select(Some(0));
|
2024-10-31 18:41:03 +00:00
|
|
|
self.filter.clear_search();
|
2024-08-09 10:22:19 +01:00
|
|
|
self.update_items();
|
|
|
|
}
|
2024-09-22 20:08:27 +01:00
|
|
|
|
|
|
|
fn toggle_task_list_guide(&mut self) {
|
|
|
|
self.spawn_float(
|
2024-11-06 15:40:55 +00:00
|
|
|
FloatingText::new(ACTIONS_GUIDE.to_string(), "Important Actions Guide", true),
|
2024-09-22 20:08:27 +01:00
|
|
|
80,
|
|
|
|
80,
|
|
|
|
);
|
|
|
|
}
|
2024-10-31 18:35:53 +00:00
|
|
|
|
|
|
|
fn scroll_tab_down(&mut self) {
|
|
|
|
let len = self.tabs.len();
|
|
|
|
let current = self.current_tab.selected().unwrap_or(0);
|
|
|
|
let next = if current + 1 >= len { 0 } else { current + 1 };
|
|
|
|
|
|
|
|
self.current_tab.select(Some(next));
|
|
|
|
self.refresh_tab();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scroll_tab_up(&mut self) {
|
|
|
|
let len = self.tabs.len();
|
|
|
|
let current = self.current_tab.selected().unwrap_or(0);
|
|
|
|
let next = if current == 0 { len - 1 } else { current - 1 };
|
|
|
|
|
|
|
|
self.current_tab.select(Some(next));
|
|
|
|
self.refresh_tab();
|
|
|
|
}
|
2024-07-28 16:17:06 +01:00
|
|
|
}
|
2024-09-20 16:09:08 +01:00
|
|
|
|
2024-09-20 16:48:09 +01:00
|
|
|
#[cfg(feature = "tips")]
|
2024-09-21 01:12:26 +01:00
|
|
|
const TIPS: &str = include_str!("../cool_tips.txt");
|
|
|
|
|
|
|
|
#[cfg(feature = "tips")]
|
2024-10-31 19:14:17 +00:00
|
|
|
fn get_random_tip() -> String {
|
2024-09-21 01:12:26 +01:00
|
|
|
let tips: Vec<&str> = TIPS.lines().collect();
|
|
|
|
if tips.is_empty() {
|
2024-10-31 19:14:17 +00:00
|
|
|
return "".to_string();
|
2024-09-20 16:09:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
2024-09-21 01:12:26 +01:00
|
|
|
let random_index = rng.gen_range(0..tips.len());
|
2024-10-31 19:14:17 +00:00
|
|
|
format!(" {} ", tips[random_index])
|
2024-09-20 16:09:08 +01:00
|
|
|
}
|