|
1 |
| -use anyhow::Result; |
2 | 1 | use asyncgit::{DiffLineType, StatusItemType};
|
3 | 2 | use ratatui::style::{Color, Modifier, Style};
|
4 |
| -use ron::{ |
5 |
| - de::from_bytes, |
6 |
| - ser::{to_string_pretty, PrettyConfig}, |
7 |
| -}; |
8 | 3 | use serde::{Deserialize, Serialize};
|
9 |
| -use std::{ |
10 |
| - fs::{self, File}, |
11 |
| - io::{Read, Write}, |
12 |
| - path::PathBuf, |
13 |
| - rc::Rc, |
14 |
| -}; |
| 4 | +use std::{fs::File, path::PathBuf, rc::Rc}; |
| 5 | +use struct_patch::Patch; |
15 | 6 |
|
16 | 7 | pub type SharedTheme = Rc<Theme>;
|
17 | 8 |
|
18 |
| -#[derive(Serialize, Deserialize, Debug, Copy, Clone)] |
| 9 | +#[derive(Serialize, Deserialize, Debug, Copy, Clone, Patch)] |
| 10 | +#[patch_derive(Deserialize)] |
19 | 11 | pub struct Theme {
|
20 | 12 | selected_tab: Color,
|
21 | 13 | command_fg: Color,
|
@@ -261,44 +253,16 @@ impl Theme {
|
261 | 253 | .bg(self.push_gauge_bg)
|
262 | 254 | }
|
263 | 255 |
|
264 |
| - // This will only be called when theme.ron doesn't already exists |
265 |
| - fn save(&self, theme_file: &PathBuf) -> Result<()> { |
266 |
| - let mut file = File::create(theme_file)?; |
267 |
| - let data = to_string_pretty(self, PrettyConfig::default())?; |
268 |
| - file.write_all(data.as_bytes())?; |
269 |
| - Ok(()) |
270 |
| - } |
271 |
| - |
272 |
| - fn read_file(theme_file: PathBuf) -> Result<Self> { |
273 |
| - let mut f = File::open(theme_file)?; |
274 |
| - let mut buffer = Vec::new(); |
275 |
| - f.read_to_end(&mut buffer)?; |
276 |
| - Ok(from_bytes(&buffer)?) |
277 |
| - } |
| 256 | + pub fn init(theme_path: &PathBuf) -> Self { |
| 257 | + let mut theme = Self::default(); |
278 | 258 |
|
279 |
| - pub fn init(file: &PathBuf) -> Result<Self> { |
280 |
| - if file.exists() { |
281 |
| - match Self::read_file(file.clone()) { |
282 |
| - Err(e) => { |
283 |
| - let config_path = file.clone(); |
284 |
| - let config_path_old = |
285 |
| - format!("{}.old", file.to_string_lossy()); |
286 |
| - fs::rename( |
287 |
| - config_path.clone(), |
288 |
| - config_path_old.clone(), |
289 |
| - )?; |
290 |
| - |
291 |
| - Self::default().save(file)?; |
292 |
| - |
293 |
| - Err(anyhow::anyhow!("{}\n Old file was renamed to {:?}.\n Defaults loaded and saved as {:?}", |
294 |
| - e,config_path_old,config_path.to_string_lossy())) |
295 |
| - } |
296 |
| - Ok(res) => Ok(res), |
| 259 | + if let Ok(file) = File::open(theme_path) { |
| 260 | + if let Ok(patch) = ron::de::from_reader(file) { |
| 261 | + theme.apply(patch); |
297 | 262 | }
|
298 |
| - } else { |
299 |
| - Self::default().save(file)?; |
300 |
| - Ok(Self::default()) |
301 | 263 | }
|
| 264 | + |
| 265 | + theme |
302 | 266 | }
|
303 | 267 | }
|
304 | 268 |
|
@@ -329,3 +293,31 @@ impl Default for Theme {
|
329 | 293 | }
|
330 | 294 | }
|
331 | 295 | }
|
| 296 | + |
| 297 | +#[cfg(test)] |
| 298 | +mod tests { |
| 299 | + use super::*; |
| 300 | + use pretty_assertions::assert_eq; |
| 301 | + use std::io::Write; |
| 302 | + use tempfile::NamedTempFile; |
| 303 | + |
| 304 | + #[test] |
| 305 | + fn test_smoke() { |
| 306 | + let mut file = NamedTempFile::new().unwrap(); |
| 307 | + |
| 308 | + writeln!( |
| 309 | + file, |
| 310 | + r" |
| 311 | +( |
| 312 | + selection_bg: Some(White), |
| 313 | +) |
| 314 | +" |
| 315 | + ) |
| 316 | + .unwrap(); |
| 317 | + |
| 318 | + let theme = Theme::init(&file.path().to_path_buf()); |
| 319 | + |
| 320 | + assert_eq!(theme.selection_fg, Theme::default().selection_fg); |
| 321 | + assert_eq!(theme.selection_bg, Color::White); |
| 322 | + } |
| 323 | +} |
0 commit comments