Skip to content

Commit

Permalink
Add multi-select toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevithakannan2 committed Sep 7, 2024
1 parent c9dc162 commit ffc3566
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/running_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ impl RunningCommand {
let mut script = String::new();
for command in commands {
match command {
Command::Raw(prompt) => script.push_str(&format!("{}\\\n", prompt)),
Command::Raw(prompt) => script.push_str(&format!("{}\\\n", prompt)), // Merge commands
Command::LocalFile(file) => {
if let Some(parent) = file.parent() {
script.push_str(&format!("cd {}\n", parent.display()));
}
script.push_str(&format!("{} {}\n", "bash", file.display()));
script.push_str(&format!("{} {}\n", "sh", file.display())); // Get the file path for sourcing common-script
}
Command::None => panic!("Command::None was treated as a command"),
}
Expand All @@ -166,7 +166,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
Expand Down
31 changes: 23 additions & 8 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub struct AppState {
/// widget
selection: ListState,
filter: Filter,
selected_commands: Vec<Command>, // Add this field to store selected commands
multi_select: bool, // This keeps track of Multi select toggle
selected_commands: Vec<Command>, // This field is to store selected commands
}

pub enum Focus {
Expand Down Expand Up @@ -61,6 +62,7 @@ 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();
Expand Down Expand Up @@ -122,7 +124,7 @@ impl AppState {
|ListEntry {
node, has_children, ..
}| {
let is_selected = self.selected_commands.contains(&node.command);
let is_selected = self.selected_commands.contains(&node.command); // Add * if multi-selection is enabled
let indicator = if is_selected { "*" } else { "" };
if *has_children {
Line::from(format!(
Expand Down Expand Up @@ -151,11 +153,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);

Expand Down Expand Up @@ -198,7 +204,6 @@ impl AppState {
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(' ') => self.toggle_selection(), // Add space key to toggle selection
KeyCode::Char('p') => self.enable_preview(),
KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.handle_enter(),
KeyCode::Char('h') | KeyCode::Left => {
Expand All @@ -212,13 +217,23 @@ impl AppState {
KeyCode::Tab => self.focus = Focus::TabList,
KeyCode::Char('t') => self.theme.next(),
KeyCode::Char('T') => self.theme.prev(),
KeyCode::Char('v') => self.toggle_multi_select(),
KeyCode::Char(' ') if self.multi_select => self.toggle_selection(), // Add space key to toggle selection

_ => {}
},
_ => {}
};
true
}

fn toggle_multi_select(&mut self) {
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(false) {
if self.selected_commands.contains(&command) {
Expand Down

0 comments on commit ffc3566

Please sign in to comment.