1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use reasonable_main::command;
use gui;
use config;
use gui_result;

use std::process;
use std::io;

#[doc = "A reasonable main function.
Commands are filtered by command.key().starts_with(input),
         then selected_command.command() is executed."]
pub fn reasonable_main(options: &[command::Command], config: &config::Config) -> io::Result<()> {
    let ans = gui::run_config(
        |input| filter(options, |option| option.key().starts_with(input)),
        config,
    );

    // Execution
    match ans {
        gui_result::GuiResult::Option(ref cmd) |
        gui_result::GuiResult::Custom(ref cmd) => {
            let (shell, flag) = if cfg!(target_os = "windows") {
                ("cmd", "/C")
            } else {
                ("sh", "-c")
            };
            process::Command::new(shell)
                .arg(flag)
                .arg(&cmd.command())
                .spawn()?;
        }
        gui_result::GuiResult::Cancel => {}
    }

    Ok(())
}

fn filter<T, F>(vector: &[T], function: F) -> Vec<T>
where
    F: FnMut(&&T) -> bool,
    T: Clone,
{
    vector.iter().filter(function).cloned().collect()
}