|
1 | 1 | use anyhow::Result;
|
2 | 2 | use asyncgit::{DiffLineType, StatusItemType};
|
3 | 3 | use ratatui::style::{Color, Modifier, Style};
|
4 |
| -use ron::{ |
5 |
| - de::from_bytes, |
6 |
| - ser::{to_string_pretty, PrettyConfig}, |
7 |
| -}; |
| 4 | +use ron::ser::{to_string_pretty, PrettyConfig}; |
8 | 5 | use serde::{Deserialize, Serialize};
|
9 |
| -use std::{ |
10 |
| - fs::{self, File}, |
11 |
| - io::{Read, Write}, |
12 |
| - path::PathBuf, |
13 |
| - rc::Rc, |
14 |
| -}; |
| 6 | +use std::{fs::File, io::Write, path::PathBuf, rc::Rc}; |
| 7 | +use struct_patch::Patch; |
15 | 8 |
|
16 | 9 | pub type SharedTheme = Rc<Theme>;
|
17 | 10 |
|
18 |
| -#[derive(Serialize, Deserialize, Debug, Copy, Clone)] |
| 11 | +#[derive(Serialize, Deserialize, Debug, Copy, Clone, Patch)] |
| 12 | +#[patch_derive(Serialize, Deserialize)] |
19 | 13 | pub struct Theme {
|
20 | 14 | selected_tab: Color,
|
21 | 15 | command_fg: Color,
|
@@ -261,44 +255,46 @@ impl Theme {
|
261 | 255 | .bg(self.push_gauge_bg)
|
262 | 256 | }
|
263 | 257 |
|
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())?; |
| 258 | + fn load_patch(theme_path: &PathBuf) -> Result<ThemePatch> { |
| 259 | + let file = File::open(theme_path)?; |
| 260 | + |
| 261 | + Ok(ron::de::from_reader(file)?) |
| 262 | + } |
| 263 | + |
| 264 | + fn load_old_theme(theme_path: &PathBuf) -> Result<Self> { |
| 265 | + let old_file = File::open(theme_path)?; |
| 266 | + |
| 267 | + Ok(ron::de::from_reader::<File, Self>(old_file)?) |
| 268 | + } |
| 269 | + |
| 270 | + // This is supposed to be called when theme.ron doesn't already exists. |
| 271 | + fn save_patch(&self, theme_path: &PathBuf) -> Result<()> { |
| 272 | + let mut file = File::create(theme_path)?; |
| 273 | + let patch = self.into_patch_by_diff(Self::default()); |
| 274 | + let data = to_string_pretty(&patch, PrettyConfig::default())?; |
| 275 | + |
268 | 276 | file.write_all(data.as_bytes())?;
|
| 277 | + |
269 | 278 | Ok(())
|
270 | 279 | }
|
271 | 280 |
|
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 |
| - } |
| 281 | + pub fn init(theme_path: &PathBuf) -> Self { |
| 282 | + let mut theme = Self::default(); |
| 283 | + |
| 284 | + if let Ok(patch) = Self::load_patch(theme_path) { |
| 285 | + theme.apply(patch); |
| 286 | + } else if let Ok(old_theme) = Self::load_old_theme(theme_path) |
| 287 | + { |
| 288 | + theme = old_theme; |
278 | 289 |
|
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), |
| 290 | + if theme.save_patch(theme_path).is_ok() { |
| 291 | + log::info!("Converted old theme to new format."); |
| 292 | + } else { |
| 293 | + log::warn!("Failed to save theme in new format."); |
297 | 294 | }
|
298 |
| - } else { |
299 |
| - Self::default().save(file)?; |
300 |
| - Ok(Self::default()) |
301 | 295 | }
|
| 296 | + |
| 297 | + theme |
302 | 298 | }
|
303 | 299 | }
|
304 | 300 |
|
@@ -329,3 +325,32 @@ impl Default for Theme {
|
329 | 325 | }
|
330 | 326 | }
|
331 | 327 | }
|
| 328 | + |
| 329 | +#[cfg(test)] |
| 330 | +mod tests { |
| 331 | + use super::*; |
| 332 | + use pretty_assertions::assert_eq; |
| 333 | + use std::io::Write; |
| 334 | + use tempfile::NamedTempFile; |
| 335 | + |
| 336 | + #[test] |
| 337 | + fn test_smoke() { |
| 338 | + let mut file = NamedTempFile::new().unwrap(); |
| 339 | + |
| 340 | + writeln!( |
| 341 | + file, |
| 342 | + r" |
| 343 | +( |
| 344 | + selection_bg: Some(White), |
| 345 | +) |
| 346 | +" |
| 347 | + ) |
| 348 | + .unwrap(); |
| 349 | + |
| 350 | + let theme = Theme::init(&file.path().to_path_buf()); |
| 351 | + |
| 352 | + assert_eq!(theme.selection_fg, Theme::default().selection_fg); |
| 353 | + assert_eq!(theme.selection_bg, Color::White); |
| 354 | + assert_ne!(theme.selection_bg, Theme::default().selection_bg); |
| 355 | + } |
| 356 | +} |
0 commit comments