Skip to content

Commit

Permalink
Add per cmd multi-selection
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevithakannan2 committed Oct 1, 2024
1 parent 476dbf3 commit 8edd12f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 59 deletions.
56 changes: 25 additions & 31 deletions core/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,18 @@ pub fn get_tabs(validate: bool) -> Vec<Tab> {
});

let tabs: Vec<Tab> = tabs
.map(
|(
TabEntry {
name,
data,
multi_selectable,
},
directory,
)| {
let mut tree = Tree::new(Rc::new(ListNode {
name: "root".to_string(),
description: String::new(),
command: Command::None,
task_list: String::new(),
}));
let mut root = tree.root_mut();
create_directory(data, &mut root, &directory, validate);
Tab {
name,
tree,
multi_selectable,
}
},
)
.map(|(TabEntry { name, data }, directory)| {
let mut tree = Tree::new(Rc::new(ListNode {
name: "root".to_string(),
description: String::new(),
command: Command::None,
task_list: String::new(),
multi_select: false,
}));
let mut root = tree.root_mut();
create_directory(data, &mut root, &directory, validate, true);
Tab { name, tree }
})
.collect();

if tabs.is_empty() {
Expand All @@ -69,12 +57,6 @@ struct TabList {
struct TabEntry {
name: String,
data: Vec<Entry>,
#[serde(default = "default_multi_selectable")]
multi_selectable: bool,
}

fn default_multi_selectable() -> bool {
true
}

#[derive(Deserialize)]
Expand All @@ -89,6 +71,12 @@ struct Entry {
entry_type: EntryType,
#[serde(default)]
task_list: String,
#[serde(default = "default_true")]
multi_select: bool,
}

fn default_true() -> bool {
true
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -169,24 +157,29 @@ fn create_directory(
node: &mut NodeMut<Rc<ListNode>>,
command_dir: &Path,
validate: bool,
parent_multi_select: bool,
) {
for entry in data {
let multi_select = parent_multi_select && entry.multi_select;

match entry.entry_type {
EntryType::Entries(entries) => {
let mut node = node.append(Rc::new(ListNode {
name: entry.name,
description: entry.description,
command: Command::None,
task_list: String::new(),
multi_select,
}));
create_directory(entries, &mut node, command_dir, validate);
create_directory(entries, &mut node, command_dir, validate, multi_select);
}
EntryType::Command(command) => {
node.append(Rc::new(ListNode {
name: entry.name,
description: entry.description,
command: Command::Raw(command),
task_list: String::new(),
multi_select,
}));
}
EntryType::Script(script) => {
Expand All @@ -205,6 +198,7 @@ fn create_directory(
file: script,
},
task_list: entry.task_list,
multi_select,
}));
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub enum Command {
pub struct Tab {
pub name: String,
pub tree: Tree<Rc<ListNode>>,
pub multi_selectable: bool,
}

#[derive(Clone, Hash, Eq, PartialEq)]
Expand All @@ -32,4 +31,5 @@ pub struct ListNode {
pub description: String,
pub command: Command,
pub task_list: String,
pub multi_select: bool,
}
4 changes: 3 additions & 1 deletion core/tabs/system-setup/tab_data.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name = "System Setup"
multi_selectable = false

[[data]]
name = "Arch Linux"
Expand All @@ -14,6 +13,7 @@ name = "Arch Server Setup"
description = "This command installs a minimal arch server setup under 5 minutes."
script = "arch/server-setup.sh"
task_list = "SI D"
multi_select = false

[[data.entries]]
name = "Paru AUR Helper"
Expand Down Expand Up @@ -76,12 +76,14 @@ name = "Full System Cleanup"
description = "This script is designed to remove unnecessary packages, clean old cache files, remove temporary files, and to empty the trash."
script = "system-cleanup.sh"
task_list = "RP PFM"
multi_select = false

[[data]]
name = "Full System Update"
description = "This command updates your system to the latest packages available for your distro"
script = "system-update.sh"
task_list = "PFM"
multi_select = false

[[data]]
name = "Gaming Dependencies"
Expand Down
5 changes: 4 additions & 1 deletion core/tabs/utils/tab_data.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name = "Utilities"
multi_selectable = false

[[data]]
name = "Monitor Control"
multi_select = false

[[data.preconditions]]
matches = true
Expand Down Expand Up @@ -72,6 +72,7 @@ script = "monitor-control/set_primary_monitor.sh"

[[data]]
name = "User Account Manager"
multi_select = false

[[data.entries]]
name = "Add User"
Expand All @@ -98,6 +99,7 @@ name = "Auto Mount Drive"
description = "This utility is designed to help with automating the process of mounting a drive on to your system."
script = "auto-mount.sh"
task_list = "PFM"
multi_select = false

[[data]]
name = "Auto Login"
Expand All @@ -114,6 +116,7 @@ name = "Bluetooth Manager"
description = "This utility is designed to manage bluetooth in your system"
script = "bluetooth-control.sh"
task_list = "I SS"
multi_select = false

[[data]]
name = "Bootable USB Creator"
Expand Down
44 changes: 19 additions & 25 deletions tui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,10 @@ impl AppState {
hints.push(Shortcut::new("Select item below", ["j", "Down"]));
hints.push(Shortcut::new("Next theme", ["t"]));
hints.push(Shortcut::new("Previous theme", ["T"]));

if self.is_current_tab_multi_selectable() {
hints.push(Shortcut::new("Toggle multi-selection mode", ["v"]));
hints.push(Shortcut::new("Multi-selection mode", ["v"]));
if self.multi_select {
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"]));
Expand Down Expand Up @@ -462,6 +460,13 @@ impl AppState {
// enabled, need to clear it to prevent state corruption
if !self.multi_select {
self.selected_commands.clear()
} 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);
}
}
}
}
ConfirmStatus::Confirm => self.handle_confirm_command(),
Expand Down Expand Up @@ -519,41 +524,30 @@ impl AppState {
}

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();
}
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_node() {
if self.selected_commands.contains(&command) {
self.selected_commands.retain(|c| c != &command);
} else {
self.selected_commands.push(command);
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);
}
}
}
}

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 either the current tree node is the root node (can we go up the tree or no)
Expand Down

0 comments on commit 8edd12f

Please sign in to comment.