-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.rs
222 lines (171 loc) · 6.07 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use anyhow::{bail, Context, Result};
use directories::ProjectDirs;
use ratatui::{
style::{Color, Style},
widgets::BorderType,
};
use serde::Deserialize;
use std::path::PathBuf;
pub const CACHE_ENV: &str = "WIKI_TUI_CACHE";
pub const CONFIG_ENV: &str = "WIKI_TUI_CONFIG";
pub const THEME_FILE_NAME: &str = "theme.toml";
pub const CONFIG_FILE_NAME: &str = "config.toml";
pub fn project_dir() -> Option<ProjectDirs> {
ProjectDirs::from("com", "builditluc", "wiki-tui")
}
pub fn cache_dir() -> Result<PathBuf> {
let directory = if let Ok(dir) = std::env::var(CACHE_ENV) {
PathBuf::from(dir)
} else if let Some(project_dir) = project_dir() {
project_dir.cache_dir().to_path_buf()
} else {
bail!("Unable to find data directory for wiki-tui");
};
Ok(directory)
}
pub fn config_dir() -> Result<PathBuf> {
let directory = if let Ok(dir) = std::env::var(CONFIG_ENV) {
PathBuf::from(dir)
} else if let Some(project_dir) = project_dir() {
project_dir.config_local_dir().to_path_buf()
} else {
bail!("Unable to find config directory for wiki-tui");
};
Ok(directory)
}
pub fn load_theme() -> Result<Theme> {
macro_rules! override_color {
($default: expr, $user: expr, $color: ident) => {
if let Some(color) = $user.$color {
$default.$color = color;
}
};
}
let mut default_theme = Theme::default();
let user_theme = load_user_theme().context("failed loading the user theme")?;
override_color!(default_theme, user_theme, bg);
override_color!(default_theme, user_theme, fg);
override_color!(default_theme, user_theme, title);
override_color!(default_theme, user_theme, selected_bg);
override_color!(default_theme, user_theme, selected_fg);
override_color!(default_theme, user_theme, inactive_fg);
override_color!(default_theme, user_theme, highlight_fg);
override_color!(default_theme, user_theme, border_fg);
override_color!(default_theme, user_theme, border_bg);
override_color!(default_theme, user_theme, border_type);
override_color!(default_theme, user_theme, border_highlight_fg);
override_color!(default_theme, user_theme, border_highlight_bg);
override_color!(default_theme, user_theme, scrollbar_track_fg);
override_color!(default_theme, user_theme, scrollbar_thumb_fg);
override_color!(default_theme, user_theme, search_title_fg);
Ok(default_theme)
}
fn load_user_theme() -> Result<UserTheme> {
let path = config_dir()
.context("failed retrieving the config dir")?
.join(THEME_FILE_NAME);
if !path.exists() {
std::fs::write(&path, "").context("failed creating the theme config file")?;
}
let user_theme_str =
std::fs::read_to_string(&path).context("failed reading the theme config file")?;
toml::from_str::<UserTheme>(&user_theme_str).context("failed parsing the user theme")
}
#[derive(Clone)]
pub struct Theme {
pub bg: Color,
pub fg: Color,
pub title: Color,
pub selected_bg: Color,
pub selected_fg: Color,
pub inactive_fg: Color,
pub highlight_fg: Color,
pub border_fg: Color,
pub border_bg: Color,
pub border_type: ThemeBorderType,
pub border_highlight_fg: Color,
pub border_highlight_bg: Color,
pub scrollbar_track_fg: Color,
pub scrollbar_thumb_fg: Color,
pub search_title_fg: Color,
}
impl Theme {
pub fn new() -> Self {
Theme {
bg: Color::Reset,
fg: Color::Reset,
title: Color::White,
selected_bg: Color::DarkGray,
selected_fg: Color::Reset,
inactive_fg: Color::Blue,
highlight_fg: Color::White,
border_fg: Color::White,
border_bg: Color::Reset,
border_type: ThemeBorderType::Rounded,
border_highlight_fg: Color::Yellow,
border_highlight_bg: Color::Reset,
scrollbar_track_fg: Color::Black,
scrollbar_thumb_fg: Color::Blue,
search_title_fg: Color::Red,
}
}
/// Returns a Paragraph with the background and foreground colors set
pub fn default_paragraph<'a, T>(&self, text: T) -> ratatui::widgets::Paragraph<'a>
where
T: Into<ratatui::text::Text<'a>>,
{
ratatui::widgets::Paragraph::new(text).style(Style::default().bg(self.bg).fg(self.fg))
}
/// Returns a Block with Borders::ALL, BorderType::Rounded and bg and fg colors set
pub fn default_block(&self) -> ratatui::widgets::Block {
ratatui::widgets::Block::default()
.borders(ratatui::widgets::Borders::ALL)
.border_type(self.border_type.clone().into())
.border_style(Style::default().fg(self.border_fg).bg(self.border_bg))
.title_style(Style::default().fg(self.title))
}
}
impl Default for Theme {
fn default() -> Self {
Theme::new()
}
}
#[derive(Deserialize, Clone)]
pub enum ThemeBorderType {
Plain,
Rounded,
Double,
Thick,
QuadrantInside,
QuadrantOutside,
}
impl From<ThemeBorderType> for BorderType {
fn from(val: ThemeBorderType) -> Self {
match val {
ThemeBorderType::Plain => BorderType::Plain,
ThemeBorderType::Rounded => BorderType::Rounded,
ThemeBorderType::Double => BorderType::Double,
ThemeBorderType::Thick => BorderType::Thick,
ThemeBorderType::QuadrantInside => BorderType::QuadrantInside,
ThemeBorderType::QuadrantOutside => BorderType::QuadrantOutside,
}
}
}
#[derive(Deserialize)]
struct UserTheme {
bg: Option<Color>,
fg: Option<Color>,
title: Option<Color>,
selected_bg: Option<Color>,
selected_fg: Option<Color>,
inactive_fg: Option<Color>,
highlight_fg: Option<Color>,
border_fg: Option<Color>,
border_bg: Option<Color>,
border_type: Option<ThemeBorderType>,
border_highlight_fg: Option<Color>,
border_highlight_bg: Option<Color>,
scrollbar_track_fg: Option<Color>,
scrollbar_thumb_fg: Option<Color>,
search_title_fg: Option<Color>,
}