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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
use crate::graph::GraphOptions;
use std::str::FromStr;
pub enum GraphType {
Star,
Ascii,
}
pub struct Opts {
pub width: Option<u64>,
pub height: Option<u64>,
pub graph_type: GraphType,
pub interpolate: bool,
pub axis: bool,
pub last_n: Option<u64>,
}
impl From<&Opts> for GraphOptions {
fn from(opts: &Opts) -> Self {
GraphOptions {
width: opts.width.unwrap_or_else(|| {
if let Ok((width, _)) = crate::term::get_terminal_size() {
// Here it would maybe be a good idea to keep the size of the graph if it is smaller than
// the specified value
width as u64
} else {
println!("Could not determine TTY columns, specify with -r");
std::process::exit(1);
}
}),
height: opts.height.unwrap_or_else(|| {
if let Ok((_, height)) = crate::term::get_terminal_size() {
// Here it would maybe be a good idea to keep the size of the graph if it is smaller than
// the specified value
height as u64 - 1
} else {
println!("Could not determine TTY rows, specify with -h");
std::process::exit(1);
}
}),
interpolate: opts.interpolate,
axis: opts.axis,
}
}
}
macro_rules! parseopts_panic {
($progname:expr) => {
println!(
"Usage: {} [-h|--height <height>] [-w|--width <width>] [-t <star|ascii>]",
$progname
);
std::process::exit(1);
};
}
pub fn parseopts() -> Opts {
let mut opts = Opts {
width: None,
height: None,
graph_type: GraphType::Star,
interpolate: false,
axis: false,
last_n: None,
};
let mut it = std::env::args();
let progname = it.next().expect("TG1");
while let Some(arg) = it.next() {
match arg.as_str() {
"--interpolate" => {
opts.interpolate = true;
}
"-t" => {
let Some(graph_type) = it.next() else {
println!("Missing value for {}", arg);
parseopts_panic!(progname);
};
match graph_type.as_str() {
"star" => {
opts.graph_type = GraphType::Star;
}
"ascii" => {
opts.graph_type = GraphType::Ascii;
}
t => {
println!(
"Unknown type \"{}\", valid options are \"star\", \"ascii_trailing\".",
t
);
parseopts_panic!(progname);
}
}
}
"-h" | "--height" => {
let Some(height) = it.next() else {
println!("Missing value for {}", arg);
parseopts_panic!(progname);
};
let Ok(height) = u64::from_str(&height) else {
println!("Cannot parse integer from \"{}\"", height);
parseopts_panic!(progname);
};
opts.height = Some(height);
}
"-l" | "--last-n" => {
let Some(last_n) = it.next() else {
println!("Missing value for {}", arg);
parseopts_panic!(progname);
};
let Ok(last_n) = u64::from_str(&last_n) else {
println!("Cannot parse integer from \"{}\"", last_n);
parseopts_panic!(progname);
};
opts.last_n = Some(last_n);
}
"-a" | "--axis" => {
opts.axis = true;
}
"-w" | "--width" => {
let Some(width) = it.next() else {
println!("Missing value for {}", arg);
parseopts_panic!(progname);
};
let Ok(width) = u64::from_str(&width) else {
println!("Cannot parse integer from \"{}\"", width);
parseopts_panic!(progname);
};
opts.width = Some(width);
}
opt => {
println!("Unknown option \"{}\"", opt);
parseopts_panic!(progname);
}
}
}
return opts;
}
|