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
use std::{fs::File, path};
use serde_yaml;
use tuikit::attr::Color;
use crate::constant_strings_paths::DEFAULT_TERMINAL_APPLICATION;
use crate::fm_error::FmResult;
use crate::keybindings::Bindings;
#[derive(Debug, Clone)]
pub struct Config {
pub colors: Colors,
pub terminal: String,
pub binds: Bindings,
}
impl Config {
fn new() -> Self {
Self {
colors: Colors::default(),
terminal: DEFAULT_TERMINAL_APPLICATION.to_owned(),
binds: Bindings::default(),
}
}
pub fn terminal(&self) -> &str {
&self.terminal
}
fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) -> FmResult<()> {
self.colors.update_from_config(&yaml["colors"]);
self.binds.update_from_config(&yaml["keys"])?;
if let Some(terminal) = yaml["terminal"].as_str().map(|s| s.to_string()) {
self.terminal = terminal;
}
Ok(())
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct Colors {
pub directory: String,
pub block: String,
pub char: String,
pub fifo: String,
pub socket: String,
pub symlink: String,
}
impl Colors {
fn update_from_config(&mut self, yaml: &serde_yaml::value::Value) {
if let Some(directory) = yaml["directory"].as_str().map(|s| s.to_string()) {
self.directory = directory;
}
if let Some(block) = yaml["block"].as_str().map(|s| s.to_string()) {
self.block = block;
}
if let Some(char) = yaml["char"].as_str().map(|s| s.to_string()) {
self.char = char;
}
if let Some(fifo) = yaml["fifo"].as_str().map(|s| s.to_string()) {
self.fifo = fifo;
}
if let Some(socket) = yaml["socket"].as_str().map(|s| s.to_string()) {
self.socket = socket;
}
if let Some(symlink) = yaml["symlink"].as_str().map(|s| s.to_string()) {
self.symlink = symlink;
}
}
fn new() -> Self {
Self {
directory: "red".to_owned(),
block: "yellow".to_owned(),
char: "green".to_owned(),
fifo: "blue".to_owned(),
socket: "cyan".to_owned(),
symlink: "magenta".to_owned(),
}
}
}
impl Default for Colors {
fn default() -> Self {
Self::new()
}
}
pub fn str_to_tuikit(color: &str) -> Color {
match color {
"white" => Color::WHITE,
"red" => Color::RED,
"green" => Color::GREEN,
"blue" => Color::BLUE,
"yellow" => Color::YELLOW,
"cyan" => Color::CYAN,
"magenta" => Color::MAGENTA,
"black" => Color::BLACK,
"light_white" => Color::LIGHT_WHITE,
"light_red" => Color::LIGHT_RED,
"light_green" => Color::LIGHT_GREEN,
"light_blue" => Color::LIGHT_BLUE,
"light_yellow" => Color::LIGHT_YELLOW,
"light_cyan" => Color::LIGHT_CYAN,
"light_magenta" => Color::LIGHT_MAGENTA,
"light_black" => Color::LIGHT_BLACK,
_ => Color::default(),
}
}
pub fn load_config(path: &str) -> FmResult<Config> {
let mut config = Config::default();
if let Ok(file) = File::open(path::Path::new(&shellexpand::tilde(path).to_string())) {
if let Ok(yaml) = serde_yaml::from_reader(file) {
config.update_from_config(&yaml)?;
}
}
Ok(config)
}