Allow to pretty print the save file
This commit is contained in:
parent
9adc650661
commit
22823fb39f
1 changed files with 14 additions and 4 deletions
18
src/main.rs
18
src/main.rs
|
|
@ -896,7 +896,7 @@ impl EditState {
|
|||
}
|
||||
}
|
||||
|
||||
fn save(&self, archive: BTreeMap<ReportDate, Report>) {
|
||||
fn save(&self, archive: BTreeMap<ReportDate, Report>, pretty: bool) {
|
||||
let mut save = SaveFile {
|
||||
current: Default::default(),
|
||||
archive,
|
||||
|
|
@ -916,7 +916,11 @@ impl EditState {
|
|||
.truncate(true)
|
||||
.open(&tmp_path)
|
||||
.expect("Can't open temp save file");
|
||||
serde_json::to_writer(save_file, &save).expect("could not write save file");
|
||||
let to_writer = match pretty {
|
||||
true => serde_json::to_writer_pretty,
|
||||
false => serde_json::to_writer,
|
||||
};
|
||||
to_writer(save_file, &save).expect("could not write save file");
|
||||
|
||||
std::fs::rename(tmp_path, &self.save_file).expect("could not save data");
|
||||
}
|
||||
|
|
@ -976,7 +980,8 @@ impl Application for Glaurung {
|
|||
Message::FontLoaded(r) => r.expect("could not load font"),
|
||||
Message::Event(ev) => {
|
||||
if let Event::Window(window::Event::CloseRequested) = ev {
|
||||
self.edit.save(self.archive.clone());
|
||||
self.edit
|
||||
.save(self.archive.clone(), self.config.pretty_save);
|
||||
return window::close();
|
||||
}
|
||||
}
|
||||
|
|
@ -997,7 +1002,8 @@ impl Application for Glaurung {
|
|||
}
|
||||
Message::SetDate(d) => self.edit.date = Some(d),
|
||||
Message::Load(d) => {
|
||||
self.edit.save(self.archive.clone());
|
||||
self.edit
|
||||
.save(self.archive.clone(), self.config.pretty_save);
|
||||
self.edit.date = Some(d);
|
||||
self.edit
|
||||
.load(self.archive.get(&d).cloned().unwrap_or_default());
|
||||
|
|
@ -1239,6 +1245,9 @@ struct Config {
|
|||
|
||||
#[serde(default)]
|
||||
data_file: Option<PathBuf>,
|
||||
|
||||
#[serde(default)]
|
||||
pretty_save: bool,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
|
|
@ -1247,6 +1256,7 @@ impl Default for Config {
|
|||
person_1: person_1(),
|
||||
person_2: person_2(),
|
||||
data_file: None,
|
||||
pretty_save: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue