Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add select all/deselect all key #53

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ impl SearchState {
self.selected = self.results.len().saturating_sub(1);
}
}

pub fn toggle_all_selected(&mut self) {
let all_included = self.results.iter().all(|res| res.included);
self.results
.iter_mut()
.for_each(|res| res.included = !all_included);
}
}

#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -539,6 +546,11 @@ impl App {
.search_results_mut()
.toggle_selected_inclusion();
}
(KeyCode::Char('a'), _) => {
self.current_screen
.search_results_mut()
.toggle_all_selected();
}
(KeyCode::Enter, _) => {
if matches!(self.current_screen, Screen::SearchComplete(_)) {
if let Screen::SearchComplete(search_state) =
Expand Down Expand Up @@ -785,6 +797,97 @@ mod tests {
rng.gen_range(1..10000)
}

fn search_result(included: bool) -> SearchResult {
SearchResult {
path: Path::new("random/file").to_path_buf(),
line_number: random_num(),
line: "foo".to_owned(),
replacement: "bar".to_owned(),
included,
replace_result: None,
}
}

#[test]
fn test_toggle_all_selected_when_all_selected() {
let mut search_state = SearchState {
results: vec![
search_result(true),
search_result(true),
search_result(true),
],
selected: 0,
};
search_state.toggle_all_selected();
assert_eq!(
search_state
.results
.iter()
.map(|res| res.included)
.collect::<Vec<_>>(),
vec![false, false, false]
);
}

#[test]
fn test_toggle_all_selected_when_none_selected() {
let mut search_state = SearchState {
results: vec![
search_result(false),
search_result(false),
search_result(false),
],
selected: 0,
};
search_state.toggle_all_selected();
assert_eq!(
search_state
.results
.iter()
.map(|res| res.included)
.collect::<Vec<_>>(),
vec![true, true, true]
);
}

#[test]
fn test_toggle_all_selected_when_some_selected() {
let mut search_state = SearchState {
results: vec![
search_result(true),
search_result(false),
search_result(true),
],
selected: 0,
};
search_state.toggle_all_selected();
assert_eq!(
search_state
.results
.iter()
.map(|res| res.included)
.collect::<Vec<_>>(),
vec![true, true, true]
);
}

#[test]
fn test_toggle_all_selected_when_no_results() {
let mut search_state = SearchState {
results: vec![],
selected: 0,
};
search_state.toggle_all_selected();
assert_eq!(
search_state
.results
.iter()
.map(|res| res.included)
.collect::<Vec<_>>(),
vec![] as Vec<bool>
);
}

fn success_result() -> SearchResult {
SearchResult {
path: Path::new("random/file").to_path_buf(),
Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,13 @@ pub fn render(app: &App, frame: &mut Frame<'_>) {
}
Screen::SearchProgressing(_) | Screen::SearchComplete(_) => {
let mut keys = if let Screen::SearchComplete(_) = app.current_screen {
// TODO: actually prevent confirmation when search is in progress
vec!["<enter> replace"]
} else {
vec![]
};
keys.append(&mut vec![
"<space> toggle",
"<a> toggle all",
"<j> down",
"<k> up",
"<C-o> back",
Expand Down
Loading