2024-08-09 10:22:19 +01:00
|
|
|
use crate::{
|
2024-08-21 17:48:14 +01:00
|
|
|
filter::{Filter, SearchAction},
|
2024-08-09 10:22:19 +01:00
|
|
|
float::{Float, FloatContent},
|
|
|
|
floating_text::FloatingText,
|
2024-09-06 22:36:12 +01:00
|
|
|
hint::{draw_shortcuts, SHORTCUT_LINES},
|
2024-09-06 00:39:10 +01:00
|
|
|
running_command::RunningCommand,
|
2024-08-09 10:22:19 +01:00
|
|
|
theme::Theme,
|
|
|
|
};
|
2024-09-05 18:52:30 +01:00
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
2024-08-09 10:22:19 +01:00
|
|
|
use ego_tree::NodeId;
|
2024-09-06 00:39:10 +01:00
|
|
|
use linutil_core::{Command, ListNode, Tab};
|
2024-08-09 10:22:19 +01:00
|
|
|
use ratatui::{
|
2024-09-07 15:25:56 +01:00
|
|
|
layout::{Alignment, Constraint, Direction, Layout},
|
2024-08-18 20:51:47 +01:00
|
|
|
style::{Style, Stylize},
|
2024-09-07 15:25:56 +01:00
|
|
|
text::{Line, Span},
|
|
|
|
widgets::{Block, Borders, List, ListState, Paragraph},
|
2024-08-09 10:22:19 +01:00
|
|
|
Frame,
|
|
|
|
};
|
2024-07-28 16:17:06 +01:00
|
|
|
|
|
|
|
pub struct AppState {
|
|
|
|
/// 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
|
|
|
|
tabs: Vec<Tab>,
|
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 .."
|
|
|
|
visit_stack: Vec<NodeId>,
|
|
|
|
/// This is the state asociated with the list widget, used to display the selection in the
|
|
|
|
/// widget
|
|
|
|
selection: ListState,
|
2024-08-21 17:48:14 +01:00
|
|
|
filter: Filter,
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Focus {
|
|
|
|
Search,
|
|
|
|
TabList,
|
|
|
|
List,
|
|
|
|
FloatingWindow(Float),
|
|
|
|
}
|
|
|
|
|
2024-08-18 20:51:47 +01:00
|
|
|
pub struct ListEntry {
|
|
|
|
pub node: ListNode,
|
|
|
|
pub id: NodeId,
|
|
|
|
pub has_children: bool,
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppState {
|
2024-09-06 00:39:10 +01:00
|
|
|
pub fn new(theme: Theme, override_validation: bool) -> Self {
|
|
|
|
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-08-09 10:22:19 +01:00
|
|
|
let mut state = Self {
|
|
|
|
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)),
|
|
|
|
visit_stack: vec![root_id],
|
|
|
|
selection: ListState::default().with_selected(Some(0)),
|
2024-08-21 17:48:14 +01:00
|
|
|
filter: Filter::new(),
|
2024-08-09 10:22:19 +01:00
|
|
|
};
|
|
|
|
state.update_items();
|
|
|
|
state
|
|
|
|
}
|
|
|
|
pub fn draw(&mut self, frame: &mut Frame) {
|
2024-09-07 15:25:56 +01:00
|
|
|
let label_block =
|
|
|
|
Block::default()
|
|
|
|
.borders(Borders::all())
|
|
|
|
.border_set(ratatui::symbols::border::Set {
|
|
|
|
top_left: " ",
|
|
|
|
top_right: " ",
|
|
|
|
bottom_left: " ",
|
|
|
|
bottom_right: " ",
|
|
|
|
vertical_left: " ",
|
|
|
|
vertical_right: " ",
|
|
|
|
horizontal_top: "*",
|
|
|
|
horizontal_bottom: "*",
|
|
|
|
});
|
|
|
|
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-06 22:36:12 +01:00
|
|
|
let vertical = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints([
|
|
|
|
Constraint::Percentage(100),
|
|
|
|
Constraint::Min(2 + SHORTCUT_LINES as u16),
|
|
|
|
])
|
|
|
|
.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-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)
|
|
|
|
.block(Block::default().borders(Borders::ALL))
|
|
|
|
.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-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();
|
|
|
|
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-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, ..
|
|
|
|
}| {
|
|
|
|
if *has_children {
|
2024-08-15 23:13:47 +01:00
|
|
|
Line::from(format!("{} {}", self.theme.dir_icon(), node.name))
|
|
|
|
.style(self.theme.dir_color())
|
2024-08-09 10:22:19 +01:00
|
|
|
} else {
|
2024-08-15 23:13:47 +01:00
|
|
|
Line::from(format!("{} {}", self.theme.cmd_icon(), node.name))
|
|
|
|
.style(self.theme.cmd_color())
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
|
|
|
// Create the list widget with items
|
|
|
|
let list = List::new(items)
|
|
|
|
.highlight_style(if let Focus::List = self.focus {
|
|
|
|
Style::default().reversed()
|
|
|
|
} else {
|
|
|
|
Style::new()
|
|
|
|
})
|
2024-09-02 21:55:40 +01:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.title(format!("Linux Toolbox - {}", env!("BUILD_DATE"))),
|
|
|
|
)
|
2024-08-09 10:22:19 +01:00
|
|
|
.scroll_padding(1);
|
|
|
|
frame.render_stateful_widget(list, chunks[1], &mut self.selection);
|
|
|
|
|
|
|
|
if let Focus::FloatingWindow(float) = &mut self.focus {
|
|
|
|
float.draw(frame, chunks[1]);
|
|
|
|
}
|
2024-09-06 22:36:12 +01:00
|
|
|
|
|
|
|
draw_shortcuts(self, frame, vertical[1]);
|
2024-08-09 10:22:19 +01:00
|
|
|
}
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
pub fn handle_key(&mut self, key: &KeyEvent) -> bool {
|
|
|
|
match &mut self.focus {
|
|
|
|
Focus::FloatingWindow(command) => {
|
|
|
|
if command.handle_key_event(key) {
|
|
|
|
self.focus = Focus::List;
|
|
|
|
}
|
|
|
|
}
|
2024-09-05 18:52:30 +01:00
|
|
|
|
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-05 18:52:30 +01:00
|
|
|
|
|
|
|
_ if key.code == KeyCode::Char('q')
|
|
|
|
|| key.code == KeyCode::Char('c')
|
|
|
|
&& key.modifiers.contains(KeyModifiers::CONTROL) =>
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
Focus::TabList => match key.code {
|
|
|
|
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Tab => {
|
|
|
|
self.focus = Focus::List
|
|
|
|
}
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
KeyCode::Char('j') | KeyCode::Down
|
2024-08-11 03:52:49 +01:00
|
|
|
if self.current_tab.selected().unwrap() + 1 < self.tabs.len() =>
|
2024-08-09 10:22:19 +01:00
|
|
|
{
|
|
|
|
self.current_tab.select_next();
|
|
|
|
self.refresh_tab();
|
|
|
|
}
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
KeyCode::Char('k') | KeyCode::Up => {
|
|
|
|
self.current_tab.select_previous();
|
|
|
|
self.refresh_tab();
|
|
|
|
}
|
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-08-09 10:22:19 +01:00
|
|
|
_ => {}
|
|
|
|
},
|
2024-09-05 18:52:30 +01:00
|
|
|
|
2024-08-09 10:22:19 +01:00
|
|
|
Focus::List if key.kind != KeyEventKind::Release => match key.code {
|
|
|
|
KeyCode::Char('j') | KeyCode::Down => self.selection.select_next(),
|
|
|
|
KeyCode::Char('k') | KeyCode::Up => self.selection.select_previous(),
|
|
|
|
KeyCode::Char('p') => self.enable_preview(),
|
|
|
|
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.handle_enter(),
|
|
|
|
KeyCode::Char('h') | KeyCode::Left => {
|
|
|
|
if self.at_root() {
|
|
|
|
self.focus = Focus::TabList;
|
|
|
|
} else {
|
|
|
|
self.enter_parent_directory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
KeyCode::Char('/') => self.enter_search(),
|
|
|
|
KeyCode::Tab => self.focus = Focus::TabList,
|
2024-08-17 19:32:53 +01:00
|
|
|
KeyCode::Char('t') => self.theme.next(),
|
|
|
|
KeyCode::Char('T') => self.theme.prev(),
|
2024-08-09 10:22:19 +01:00
|
|
|
_ => {}
|
|
|
|
},
|
2024-09-05 18:52:30 +01:00
|
|
|
|
|
|
|
_ => (),
|
2024-08-09 10:22:19 +01:00
|
|
|
};
|
|
|
|
true
|
|
|
|
}
|
2024-09-05 18:52:30 +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(),
|
|
|
|
*self.visit_stack.last().unwrap(),
|
|
|
|
);
|
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-08-09 10:22:19 +01:00
|
|
|
fn enter_parent_directory(&mut self) {
|
|
|
|
self.visit_stack.pop();
|
|
|
|
self.selection.select(Some(0));
|
|
|
|
self.update_items();
|
|
|
|
}
|
2024-09-06 22:36:12 +01:00
|
|
|
pub fn get_selected_command(&self) -> Option<Command> {
|
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 {
|
|
|
|
return Some(item.node.command.clone());
|
2024-09-06 22:36:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
pub fn go_to_selected_dir(&mut self) {
|
|
|
|
let mut selected_index = self.selection.selected().unwrap_or(0);
|
|
|
|
|
|
|
|
if !self.at_root() && selected_index == 0 {
|
|
|
|
self.enter_parent_directory();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.at_root() {
|
|
|
|
selected_index = selected_index.saturating_sub(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(item) = self.filter.item_list().get(selected_index) {
|
|
|
|
if item.has_children {
|
2024-08-09 10:22:19 +01:00
|
|
|
self.visit_stack.push(item.id);
|
|
|
|
self.selection.select(Some(0));
|
|
|
|
self.update_items();
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(item) = self.filter.item_list().get(selected_index) {
|
|
|
|
item.has_children
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn selected_item_is_cmd(&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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(item) = self.filter.item_list().get(selected_index) {
|
|
|
|
!item.has_children
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
fn enable_preview(&mut self) {
|
2024-09-06 22:36:12 +01:00
|
|
|
if let Some(command) = self.get_selected_command() {
|
2024-08-13 22:55:59 +01:00
|
|
|
if let Some(preview) = FloatingText::from_command(&command) {
|
2024-08-09 10:22:19 +01:00
|
|
|
self.spawn_float(preview, 80, 80);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn handle_enter(&mut self) {
|
2024-09-06 22:36:12 +01:00
|
|
|
if let Some(cmd) = self.get_selected_command() {
|
2024-08-13 22:55:59 +01:00
|
|
|
let command = RunningCommand::new(cmd);
|
2024-08-09 10:22:19 +01:00
|
|
|
self.spawn_float(command, 80, 80);
|
2024-09-06 22:36:12 +01:00
|
|
|
} else {
|
|
|
|
self.go_to_selected_dir();
|
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));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
fn refresh_tab(&mut self) {
|
2024-08-11 03:52:49 +01:00
|
|
|
self.visit_stack = vec![self.tabs[self.current_tab.selected().unwrap()]
|
|
|
|
.tree
|
|
|
|
.root()
|
|
|
|
.id()];
|
2024-08-09 10:22:19 +01:00
|
|
|
self.selection.select(Some(0));
|
|
|
|
self.update_items();
|
|
|
|
}
|
2024-07-28 16:17:06 +01:00
|
|
|
}
|