fix: #4 exit with non-zero status on cancel.

0 -> normal exit; 1 -> no match; 130 -> cancel.
The 130 is chosen following fzf's spec.
This commit is contained in:
zhang_ji 2016-07-04 09:58:07 +08:00
parent bc43550f7c
commit 82503d3569
3 changed files with 25 additions and 4 deletions

View file

@ -60,3 +60,13 @@ different from fzf. For example:
Feel free to [create new
issue](https://github.com/lotabout/fzf-rs/issues/new) if you meet any bugs
or have any ideas.
# Manual
## exit code
| Exit Code | Meaning |
|---|---|
| 0 | Exit normally |
| 1 | No Match found |
| 130 | Abort by Ctrl-C/Ctrl-G/ESC/etc... |

View file

@ -34,6 +34,11 @@ use getopts::Options;
use std::env;
fn main() {
let exit_code = real_main();
std::process::exit(exit_code);
}
fn real_main() -> i32 {
// option parsing
let args: Vec<String> = env::args().collect();
@ -54,7 +59,7 @@ fn main() {
// print help message
if options.opt_present("h") {
print_usage(&program, opts);
return
return 0;
}
let theme = ColorTheme::new();
@ -122,6 +127,8 @@ fn main() {
input.run();
});
let mut exit_code = 0;
'outer: loop {
let mut need_refresh = true;
for (e, val) in eb.wait() {
@ -172,12 +179,13 @@ fn main() {
}
// Actions
Event::EvActAbort => { break 'outer; }
Event::EvActAbort => {exit_code = 130; break 'outer; }
Event::EvActAccept => {
// break out of the loop and output the selected item.
//if model.get_num_selected() <= 0 { model.act_toggle(Some(true)); }
let args: Option<String> = *val.downcast().unwrap();
model.act_accept(args);
let num_selected = model.act_accept(args);
exit_code = if num_selected > 0 {0} else {1};
break 'outer;
}
Event::EvActBackwardChar => {model.act_backward_char();}
@ -237,6 +245,7 @@ fn main() {
model.close();
model.output();
return exit_code;
}
fn print_usage(program: &str, opts: Options) {

View file

@ -275,7 +275,8 @@ impl Model {
//============================================================================
// Actions
pub fn act_accept(&mut self, accept_key: Option<String>) {
// return the number selected.
pub fn act_accept(&mut self, accept_key: Option<String>) -> usize {
self.accept_key = accept_key;
let mut matched_items = self.matched_items.borrow_mut();
@ -283,6 +284,7 @@ impl Model {
let item_index = matched.index;
self.selected_indics.insert(item_index);
}
self.selected_indics.len()
}
pub fn act_add_char(&mut self, ch: char) {