-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathloader.rs
More file actions
157 lines (137 loc) · 4.13 KB
/
Copy pathloader.rs
File metadata and controls
157 lines (137 loc) · 4.13 KB
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
use ansi_term::{ANSIGenericString, Color};
use confy::ConfyError;
use serde::{Deserialize, Serialize};
use crate::{exact_math::float_mode::FloatMode, VERSION};
#[derive(Clone, Serialize, Deserialize)]
pub struct Greeting {
pub greeting_message: String,
pub greeting_color: String,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Prompt {
pub prompt: String,
pub prompt_color: String,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Config {
pub general_color: String,
pub default_float_mode: String,
pub greeting: Greeting,
pub prompt: Prompt,
}
#[derive(Clone)]
pub struct Loaded<'a> {
pub general_color: Color,
pub greeting_message: ANSIGenericString<'a, str>,
pub greeting_color: Color,
pub prompt: String,
pub prompt_style: Color,
pub float_mode: FloatMode,
}
impl Default for Greeting {
fn default() -> Self {
Self {
greeting_message: "Welcome to Calc %version% by %author%, type help for help"
.to_string(),
greeting_color: "blue".to_string(),
}
}
}
impl Default for Prompt {
fn default() -> Self {
Self {
prompt: "> ".to_string(),
prompt_color: "cyan".to_string(),
}
}
}
impl Default for Config {
fn default() -> Self {
Self {
general_color: "purple".to_string(),
default_float_mode: "exact".to_string(),
greeting: Greeting::default(),
prompt: Prompt::default(),
}
}
}
pub fn load() -> Result<Config, confy::ConfyError> {
let cfg = confy::load("mini-calc", Some("mini-calc"));
match cfg {
Ok(config) => Ok(config),
_ => Ok(Config::default()),
}
}
pub fn write_config(c: &Config) -> Result<(), ConfyError> {
confy::store("mini-calc", Some("mini-calc"), c)?;
Ok(())
}
pub fn write_default_config() -> Result<(), ConfyError> {
write_config(&Config::default())
}
pub fn load_rgb_color(str: &str) -> (u8, u8, u8) {
let first = &str[0..2];
let second = &str[2..4];
let last = &str[4..6];
let rd = u8::from_str_radix(first, 16);
let gd = u8::from_str_radix(second, 16);
let bd = u8::from_str_radix(last, 16);
let r = rd.unwrap_or(0xFF);
let g = gd.unwrap_or(0xFF);
let b = bd.unwrap_or(0xFF);
(r, g, b)
}
pub fn load_color(string: String) -> Color {
match string.to_lowercase().as_str() {
"purple" => Color::Purple,
"cyan" => Color::Cyan,
"blue" => Color::Blue,
"black" => Color::Black,
"red" => Color::Red,
"yellow" => Color::Yellow,
"green" => Color::Green,
"white" => Color::White,
s => {
if s.starts_with("#") {
let str = s.strip_prefix("#");
if str.unwrap().len() < 6 {
Color::Cyan
} else {
let (r, g, b) = load_rgb_color(str.unwrap());
if r == 0xFF && g == 0xFF && b == 0xFF {
Color::Cyan
} else {
Color::RGB(r, g, b)
}
}
} else {
Color::Cyan
}
}
}
}
fn load_float_mode(dfm: String) -> FloatMode {
match dfm.to_lowercase().as_str().trim() {
"exact" => FloatMode::Exact,
"rational" => FloatMode::Exact,
"science" => FloatMode::Science,
"scientific" => FloatMode::Science,
_ => FloatMode::Normal,
}
}
pub fn replace_variable(str: String) -> String {
str.replace("%author%", "Charlotte Thomas")
.replace("%version%", VERSION)
.to_string()
}
pub fn load_config<'a>(config: Config) -> Loaded<'a> {
Loaded {
greeting_color: load_color(config.clone().greeting.greeting_color),
general_color: load_color(config.general_color),
greeting_message: load_color(config.greeting.greeting_color)
.paint(replace_variable(config.greeting.greeting_message)),
prompt: config.prompt.prompt,
float_mode: load_float_mode(config.default_float_mode),
prompt_style: load_color(config.prompt.prompt_color),
}
}