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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use super::Module;
use clap::{App, Arg, ArgMatches, SubCommand};
use env_logger;
use std::env;

pub struct LogModule;

impl LogModule {
    pub fn verbosity(&self) -> isize {
        0
    }
}

impl Module for LogModule {
    fn args_declare<'a, 'b>(&self, app: App<'a, 'b>) -> App<'a, 'b> {
        app.arg(
            Arg::with_name("v")
                .short("v")
                .multiple(true)
                .help("Sets the level of verbosity"),
        )
    }

    fn args_consume(&mut self, matches: &ArgMatches) -> bool {
        if env::var("RUST_LOG").is_err() {
            match matches.occurrences_of("v") {
                0 => env::set_var("RUST_LOG", "error"),
                1 => env::set_var("RUST_LOG", "info"),
                2 => env::set_var(
                    "RUST_LOG",
                    "info,gu_net=debug,gu_provider=debug,gu_hub=debug,gu_event_bus=debug",
                ),
                _ => env::set_var("RUST_LOG", "debug"),
            }
        }
        env_logger::init();
        false
    }
}

pub struct AutocompleteModule(String);

impl AutocompleteModule {
    pub fn new() -> AutocompleteModule {
        let shell: String = env::args().take(1).into_iter().next().unwrap().into();
        AutocompleteModule(shell)
    }
}

impl Module for AutocompleteModule {
    fn args_declare<'a, 'b>(&self, app: App<'a, 'b>) -> App<'a, 'b> {
        app.subcommand(
            SubCommand::with_name("completions")
                .about("Generates completion scripts for your shell")
                .arg(
                    Arg::with_name("SHELL")
                        .required(true)
                        .possible_values(&["bash", "fish", "zsh"])
                        .help("The shell to generate the script for"),
                ),
        )
    }

    fn args_autocomplete<F>(&self, matches: &ArgMatches, app_gen: &F) -> bool
    where
        F: Fn() -> App<'static, 'static>,
    {
        use std::io;

        if let Some(sub_matches) = matches.subcommand_matches("completions") {
            let shell = sub_matches.value_of("SHELL").unwrap();
            let prg = self.0.as_ref();
            app_gen().gen_completions_to(prg, shell.parse().unwrap(), &mut io::stdout());
            return true;
        }
        false
    }
}