forked from lsd-rs/lsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.rs
114 lines (103 loc) · 3.01 KB
/
flags.rs
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
use clap::ArgMatches;
#[derive(Clone, Debug, Copy)]
pub struct Flags {
pub display_all: bool,
pub display_long: bool,
pub display_online: bool,
pub display_tree: bool,
pub display_indicators: bool,
pub recursive: bool,
pub sort_by: SortFlag,
pub sort_order: SortOrder,
pub date: DateFlag,
pub color: WhenFlag,
pub icon: WhenFlag,
}
impl<'a> From<ArgMatches<'a>> for Flags {
fn from(matches: ArgMatches) -> Self {
let color_inputs: Vec<&str> = matches.values_of("color").unwrap().collect();
let icon_inputs: Vec<&str> = matches.values_of("icon").unwrap().collect();
let date_inputs: Vec<&str> = matches.values_of("date").unwrap().collect();
let sort_by = if matches.is_present("timesort") {
SortFlag::Time
} else {
SortFlag::Name
};
let sort_order = if matches.is_present("reverse") {
SortOrder::Reverse
} else {
SortOrder::Default
};
Self {
display_all: matches.is_present("all"),
display_long: matches.is_present("long"),
display_online: matches.is_present("oneline"),
display_tree: matches.is_present("tree"),
display_indicators: matches.is_present("indicators"),
recursive: matches.is_present("recursive"),
sort_by,
sort_order,
// Take only the last value
date: DateFlag::from(date_inputs[date_inputs.len() - 1]),
color: WhenFlag::from(color_inputs[color_inputs.len() - 1]),
icon: WhenFlag::from(icon_inputs[icon_inputs.len() - 1]),
}
}
}
impl Default for Flags {
fn default() -> Self {
Self {
display_all: false,
display_long: false,
display_online: false,
display_tree: false,
display_indicators: false,
recursive: false,
sort_by: SortFlag::Name,
sort_order: SortOrder::Default,
date: DateFlag::Date,
color: WhenFlag::Auto,
icon: WhenFlag::Auto,
}
}
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum DateFlag {
Date,
Relative,
}
impl<'a> From<&'a str> for DateFlag {
fn from(time: &'a str) -> Self {
match time {
"date" => DateFlag::Date,
"relative" => DateFlag::Relative,
_ => panic!("invalid \"time\" flag: {}", time),
}
}
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum WhenFlag {
Always,
Auto,
Never,
}
impl<'a> From<&'a str> for WhenFlag {
fn from(when: &'a str) -> Self {
match when {
"always" => WhenFlag::Always,
"auto" => WhenFlag::Auto,
"never" => WhenFlag::Never,
_ => panic!("invalid \"when\" flag: {}", when),
}
}
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum SortFlag {
Name,
Time,
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum SortOrder {
Default,
Reverse,
}