diff --git a/Cargo.lock b/Cargo.lock index bc6b5eebb..29c679264 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,6 +120,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "cassowary" version = "0.3.0" @@ -360,6 +366,29 @@ dependencies = [ "cc", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "include_dir" version = "0.7.4" diff --git a/src/commands/utils/tab_data.toml b/src/commands/utils/tab_data.toml index 67ba893fd..72be5a4e9 100644 --- a/src/commands/utils/tab_data.toml +++ b/src/commands/utils/tab_data.toml @@ -1,4 +1,5 @@ name = "Utilities" +multi_selectable = false [[data]] name = "WiFi Manager" diff --git a/src/hint.rs b/src/hint.rs index 27474ea99..76e2e9eab 100644 --- a/src/hint.rs +++ b/src/hint.rs @@ -139,6 +139,10 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) { hints.push(Shortcut::new(vec!["j", "Down"], "Select item below")); hints.push(Shortcut::new(vec!["t"], "Next theme")); hints.push(Shortcut::new(vec!["T"], "Previous theme")); + if state.is_current_tab_multi_selectable() { + hints.push(Shortcut::new(vec!["v"], "Toggle Multi selection")); + hints.push(Shortcut::new(vec!["Space"], "Multi select commands")); + } ShortcutList { scope_name: "Item list", hints, diff --git a/src/running_command.rs b/src/running_command.rs index 0840f189b..9742e53a0 100644 --- a/src/running_command.rs +++ b/src/running_command.rs @@ -143,24 +143,30 @@ impl FloatContent for RunningCommand { } impl RunningCommand { - pub fn new(command: Command) -> Self { + pub fn new(commands: Vec) -> Self { let pty_system = NativePtySystem::default(); - // Build the command based on the provided Command enum variant - let mut cmd = CommandBuilder::new("sh"); - match command { - Command::Raw(prompt) => { - cmd.arg("-c"); - cmd.arg(prompt); - } - Command::LocalFile(file) => { - cmd.arg(&file); - if let Some(parent) = file.parent() { - cmd.cwd(parent); + // Create a command to execute all the selected commands + let mut cmd: CommandBuilder = CommandBuilder::new("sh"); + cmd.arg("-c"); + + // Initialize an empty string to hold the merged commands + // All the merged commands are passed as a single argument to reduce the overhead of rebuilding the command arguments for each and every command + let mut script = String::new(); + for command in commands { + match command { + Command::Raw(prompt) => script.push_str(&format!("{}\n", prompt)), // Merge raw commands + Command::LocalFile(file) => { + if let Some(parent) = file.parent() { + script.push_str(&format!("cd {}\n", parent.display())); + // Merge local file path + } + script.push_str(&format!("sh {}\n", file.display())); } + Command::None => panic!("Command::None was treated as a command"), } - Command::None => panic!("Command::None was treated as a command"), } + cmd.arg(script); // Open a pseudo-terminal with initial size let pair = pty_system @@ -181,7 +187,7 @@ impl RunningCommand { child.wait().unwrap() }); - let mut reader = pair.master.try_clone_reader().unwrap(); // This is a reader, this is where we + let mut reader = pair.master.try_clone_reader().unwrap(); // A buffer, shared between the thread that reads the command output, and the main tread. // The main thread only reads the contents diff --git a/src/state.rs b/src/state.rs index 6bb4f5f33..f18c3878b 100644 --- a/src/state.rs +++ b/src/state.rs @@ -34,6 +34,8 @@ pub struct AppState { /// widget selection: ListState, filter: Filter, + multi_select: bool, // This keeps track of Multi select toggle + selected_commands: Vec, // This field is to store selected commands } pub enum Focus { @@ -61,6 +63,8 @@ impl AppState { visit_stack: vec![root_id], selection: ListState::default().with_selected(Some(0)), filter: Filter::new(), + multi_select: false, + selected_commands: Vec::new(), // Initialize with an empty vector }; state.update_items(); state @@ -155,12 +159,24 @@ impl AppState { |ListEntry { node, has_children, .. }| { + let is_selected = self.selected_commands.contains(&node.command); // Add * if command is selected + let indicator = if is_selected { "*" } else { "" }; if *has_children { - Line::from(format!("{} {}", self.theme.dir_icon(), node.name)) - .style(self.theme.dir_color()) + Line::from(format!( + "{} {} {}", + self.theme.dir_icon(), + node.name, + indicator + )) + .style(self.theme.dir_color()) } else { - Line::from(format!("{} {}", self.theme.cmd_icon(), node.name)) - .style(self.theme.cmd_color()) + Line::from(format!( + "{} {} {}", + self.theme.cmd_icon(), + node.name, + indicator + )) + .style(self.theme.cmd_color()) } }, )); @@ -172,11 +188,15 @@ impl AppState { } else { Style::new() }) - .block( - Block::default() - .borders(Borders::ALL) - .title(format!("Linux Toolbox - {}", env!("BUILD_DATE"))), - ) + .block(Block::default().borders(Borders::ALL).title(format!( + "Linux Toolbox - {} {}", + env!("BUILD_DATE"), + if self.multi_select { + "[Multi-Select]" + } else { + "" + } + ))) .scroll_padding(1); frame.render_stateful_widget(list, chunks[1], &mut self.selection); @@ -234,18 +254,53 @@ impl AppState { KeyCode::Tab => self.focus = Focus::TabList, KeyCode::Char('t') => self.theme.next(), KeyCode::Char('T') => self.theme.prev(), + KeyCode::Char('v') | KeyCode::Char('V') => self.toggle_multi_select(), + KeyCode::Char(' ') if self.multi_select => self.toggle_selection(), + _ => {} }, _ => {} }; true } + + fn toggle_multi_select(&mut self) { + if self.is_current_tab_multi_selectable() { + self.multi_select = !self.multi_select; + if !self.multi_select { + self.selected_commands.clear(); + } + } + } + + 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); + } else { + self.selected_commands.push(command); + } + } + } + + pub fn is_current_tab_multi_selectable(&self) -> bool { + let index = self.current_tab.selected().unwrap_or(0); + self.tabs + .get(index) + .map_or(false, |tab| tab.multi_selectable) + } + fn update_items(&mut self) { self.filter.update_items( &self.tabs, self.current_tab.selected().unwrap(), *self.visit_stack.last().unwrap(), ); + + if !self.is_current_tab_multi_selectable() { + self.multi_select = false; + self.selected_commands.clear(); + } } /// Checks ehther the current tree node is the root node (can we go up the tree or no) /// Returns `true` if we can't go up the tree (we are at the tree root) @@ -344,8 +399,12 @@ impl AppState { } fn handle_enter(&mut self) { if let Some(cmd) = self.get_selected_command() { - let command = RunningCommand::new(cmd); + if self.selected_commands.is_empty() { + self.selected_commands.push(cmd); + } + let command = RunningCommand::new(self.selected_commands.clone()); self.spawn_float(command, 80, 80); + self.selected_commands.clear(); } else { self.go_to_selected_dir(); } diff --git a/src/tabs.rs b/src/tabs.rs index 979693262..970884a0e 100644 --- a/src/tabs.rs +++ b/src/tabs.rs @@ -12,6 +12,12 @@ struct TabList { struct TabEntry { name: String, data: Vec, + #[serde(default = "default_multi_selectable")] + multi_selectable: bool, +} + +fn default_multi_selectable() -> bool { + true } #[derive(Deserialize)] @@ -83,6 +89,7 @@ enum SystemDataType { pub struct Tab { pub name: String, pub tree: Tree, + pub multi_selectable: bool, } #[derive(Clone, Hash, Eq, PartialEq)] @@ -105,15 +112,28 @@ pub fn get_tabs(command_dir: &Path, validate: bool) -> Vec { }); let tabs: Vec = tabs - .map(|(TabEntry { name, data }, directory)| { - let mut tree = Tree::new(ListNode { - name: "root".to_string(), - command: Command::None, - }); - let mut root = tree.root_mut(); - create_directory(data, &mut root, &directory); - Tab { name, tree } - }) + .map( + |( + TabEntry { + name, + data, + multi_selectable, + }, + directory, + )| { + let mut tree = Tree::new(ListNode { + name: "root".to_string(), + command: Command::None, + }); + let mut root = tree.root_mut(); + create_directory(data, &mut root, &directory); + Tab { + name, + tree, + multi_selectable, + } + }, + ) .collect(); if tabs.is_empty() {