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
use atty::Stream;
use clap::{App, Arg};
pub struct RuntimeConfig {
pub search_term: String,
pub interactive: bool,
pub number_of_results: usize,
}
impl RuntimeConfig {
pub fn new() -> Self {
let clap = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.author("alpha-tango-kilo <git@heyatk.com>")
.about(env!("CARGO_PKG_DESCRIPTION"))
.arg(Arg::new("non-interactive")
.short('n')
.long("non-interactive")
.about("Disables interactive features (effectively makes search \"I'm feeling lucky\"). Implies -r 1"))
.arg(Arg::new("number_of_results")
.short('r')
.long("results")
.about("The maximum number of results to show from IMDb")
.takes_value(true)
.validator(|s| s.parse::<usize>()))
.arg(Arg::new("search_term")
.about("The title of the movie/show you're looking for")
.required(true))
.get_matches();
let search_term = clap.value_of("search_term").unwrap().to_string();
let interactive = !clap.is_present("non-interactive")
&& atty::is(Stream::Stdout)
&& atty::is(Stream::Stdin);
let number_of_results = if interactive {
match clap.value_of("number_of_results") {
Some(n) => n.parse().unwrap(),
None => RuntimeConfig::default().number_of_results,
}
} else {
1
};
RuntimeConfig {
search_term,
interactive,
number_of_results,
}
}
}
impl Default for RuntimeConfig {
fn default() -> Self {
RuntimeConfig {
search_term: String::new(),
interactive: true,
number_of_results: 10,
}
}
}