-
Notifications
You must be signed in to change notification settings - Fork 29
/
config.rs
158 lines (140 loc) · 3.67 KB
/
config.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt, time::Duration};
type Prompt = Vec<Segment>;
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub separator: Separators,
#[serde(default)]
pub left: Prompt,
#[serde(default)]
pub right: Prompt,
#[serde(default)]
pub icons: HashMap<String, String>,
#[serde(default)]
pub icon_set: IconSet,
#[serde(default)]
#[serde(with = "humantime_serde")]
pub cmdtime_threshold: Duration,
#[serde(default)]
pub dir: Dir,
#[serde(default)]
pub git: Git,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Separators {
pub right: Separator,
pub left: Separator,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Separator {
pub thick: String,
pub thin: String,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Segment {
#[serde(default)]
pub name: String,
#[serde(default)]
pub color: Colors,
#[serde(default)]
pub args: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Colors {
#[serde(default)]
pub background: Color,
pub foreground: Color,
}
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
#[serde(untagged)]
pub enum Color {
Hex(u32),
Name(String),
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IconSet {
Nerd,
Unicode,
#[allow(clippy::upper_case_acronyms)]
ASCII,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Dir {
#[serde(default)]
pub aliases: HashMap<String, String>,
#[serde(default)]
pub length: Option<usize>,
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Git {
#[serde(default)]
pub ignore_dirs: Vec<String>,
}
fn default_left_prompt() -> Prompt {
vec![Segment {
name: "dir".into(),
color: Colors {
background: Color::Name("7".into()),
foreground: Color::Name("0".into()),
},
args: vec![],
}]
}
// Implement defaults that will render a
// usable prompt.
impl std::default::Default for Config {
fn default() -> Self {
let config_dir = confy::get_configuration_file_path("silver", None)
.expect("Failed to get configuration path")
.into_os_string()
.into_string();
match config_dir {
Ok(config_dir) => eprintln!("Creating default configuration at: {}", config_dir),
Err(_) => eprintln!("Couldn't format the path to the configuration file. Oops"),
}
Self {
left: default_left_prompt(),
separator: Separators::default(),
right: Prompt::default(),
icons: HashMap::default(),
icon_set: IconSet::default(),
cmdtime_threshold: Duration::default(),
dir: Dir::default(),
git: Git::default(),
}
}
}
impl Default for Separators {
fn default() -> Self {
Self {
left: Separator {
thick: "\u{e0b0}".into(),
thin: "\u{e0b1}".into(),
},
right: Separator {
thick: "\u{e0b2}".into(),
thin: "\u{e0b3}".into(),
},
}
}
}
impl Default for Color {
fn default() -> Self {
Self::Name("none".into())
}
}
impl Default for IconSet {
fn default() -> Self {
Self::Unicode
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Name(name) => write!(f, "{}", name),
Self::Hex(num) => write!(f, "{:x}", num),
}
}
}