Skip to content

Commit 00eca12

Browse files
author
Stephan Dilly
committed
smarter config loading giving more diagnostics to the user about whats going wrong (#589)
1 parent b046085 commit 00eca12

File tree

4 files changed

+62
-45
lines changed

4 files changed

+62
-45
lines changed

src/app.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crossbeam_channel::Sender;
2525
use crossterm::event::{Event, KeyEvent};
2626
use std::{
2727
cell::{Cell, RefCell},
28-
path::{Path, PathBuf},
28+
path::Path,
2929
rc::Rc,
3030
};
3131
use tui::{
@@ -76,11 +76,12 @@ impl App {
7676
pub fn new(
7777
sender: &Sender<AsyncNotification>,
7878
input: Input,
79-
theme_path: PathBuf,
79+
theme: Theme,
80+
key_config: KeyConfig,
8081
) -> Self {
8182
let queue = Queue::default();
82-
let theme = Rc::new(Theme::init(theme_path));
83-
let key_config = Rc::new(KeyConfig::init());
83+
let theme = Rc::new(theme);
84+
let key_config = Rc::new(key_config);
8485

8586
Self {
8687
input,

src/keys.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use crate::get_app_config_path;
55
use anyhow::Result;
66
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
77
use ron::{
8-
de::from_bytes,
8+
self,
99
ser::{to_string_pretty, PrettyConfig},
1010
};
1111
use serde::{Deserialize, Serialize};
1212
use std::{
13-
fs::File,
13+
fs::{self, File},
1414
io::{Read, Write},
1515
path::PathBuf,
1616
rc::Rc,
@@ -127,15 +127,14 @@ impl Default for KeyConfig {
127127
}
128128

129129
impl KeyConfig {
130-
fn save(&self) -> Result<()> {
131-
let config_file = Self::get_config_file()?;
132-
let mut file = File::create(config_file)?;
130+
fn save(&self, file: PathBuf) -> Result<()> {
131+
let mut file = File::create(file)?;
133132
let data = to_string_pretty(self, PrettyConfig::default())?;
134133
file.write_all(data.as_bytes())?;
135134
Ok(())
136135
}
137136

138-
fn get_config_file() -> Result<PathBuf> {
137+
pub fn get_config_file() -> Result<PathBuf> {
139138
let app_home = get_app_config_path()?;
140139
Ok(app_home.join("key_config.ron"))
141140
}
@@ -144,31 +143,31 @@ impl KeyConfig {
144143
let mut f = File::open(config_file)?;
145144
let mut buffer = Vec::new();
146145
f.read_to_end(&mut buffer)?;
147-
Ok(from_bytes(&buffer)?)
146+
Ok(ron::de::from_bytes(&buffer)?)
148147
}
149148

150-
fn init_internal() -> Result<Self> {
151-
let file = Self::get_config_file()?;
149+
pub fn init(file: PathBuf) -> Result<Self> {
152150
if file.exists() {
153-
Ok(Self::read_file(file)?)
154-
} else {
155-
let def = Self::default();
156-
if def.save().is_err() {
157-
log::warn!(
158-
"failed to store default key config to disk."
159-
)
160-
}
161-
Ok(def)
162-
}
163-
}
151+
match Self::read_file(file.clone()) {
152+
Err(e) => {
153+
let config_path = file.clone();
154+
let config_path_old =
155+
format!("{}.old", file.to_string_lossy());
156+
fs::rename(
157+
config_path.clone(),
158+
config_path_old.clone(),
159+
)?;
160+
161+
Self::default().save(file)?;
164162

165-
pub fn init() -> Self {
166-
match Self::init_internal() {
167-
Ok(v) => v,
168-
Err(e) => {
169-
log::error!("failed loading key binding: {}", e);
170-
Self::default()
163+
Err(anyhow::anyhow!("{}\n Old file was renamed to {:?}.\n Defaults loaded and saved as {:?}",
164+
e,config_path_old,config_path.to_string_lossy()))
165+
}
166+
Ok(res) => Ok(res),
171167
}
168+
} else {
169+
Self::default().save(file)?;
170+
Ok(Self::default())
172171
}
173172
}
174173

src/main.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use crossterm::{
4343
ExecutableCommand,
4444
};
4545
use input::{Input, InputEvent, InputState};
46+
use keys::KeyConfig;
4647
use profiler::Profiler;
4748
use scopeguard::defer;
4849
use scopetime::scope_time;
@@ -61,6 +62,7 @@ use tui::{
6162
backend::{Backend, CrosstermBackend},
6263
Terminal,
6364
};
65+
use ui::style::Theme;
6466

6567
static TICK_INTERVAL: Duration = Duration::from_secs(5);
6668
static SPINNER_INTERVAL: Duration = Duration::from_millis(80);
@@ -88,6 +90,13 @@ fn main() -> Result<()> {
8890
return Ok(());
8991
}
9092

93+
let key_config = KeyConfig::init(KeyConfig::get_config_file()?)
94+
.map_err(|e| eprintln!("KeyConfig loading error: {}", e))
95+
.unwrap_or_default();
96+
let theme = Theme::init(cliargs.theme)
97+
.map_err(|e| eprintln!("Theme loading error: {}", e))
98+
.unwrap_or_default();
99+
91100
setup_terminal()?;
92101
defer! {
93102
shutdown_terminal().expect("shutdown failed");
@@ -105,7 +114,7 @@ fn main() -> Result<()> {
105114
let ticker = tick(TICK_INTERVAL);
106115
let spinner_ticker = tick(SPINNER_INTERVAL);
107116

108-
let mut app = App::new(&tx_git, input, cliargs.theme);
117+
let mut app = App::new(&tx_git, input, theme, key_config);
109118

110119
let mut spinner = Spinner::default();
111120
let mut first_update = true;

src/ui/style.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ron::{
99
};
1010
use serde::{Deserialize, Serialize};
1111
use std::{
12-
fs::File,
12+
fs::{self, File},
1313
io::{Read, Write},
1414
path::PathBuf,
1515
rc::Rc,
@@ -250,22 +250,30 @@ impl Theme {
250250
Ok(from_bytes(&buffer)?)
251251
}
252252

253-
fn init_internal(theme: PathBuf) -> Result<Self> {
254-
if theme.exists() {
255-
Ok(Self::read_file(theme)?)
256-
} else {
257-
// This will only be called when theme.ron doesn't already exists
258-
let def = Self::default();
259-
if def.save(theme).is_err() {
260-
log::warn!("failed to store default theme to disk.")
253+
pub fn init(file: PathBuf) -> Result<Self> {
254+
if file.exists() {
255+
match Self::read_file(file.clone()) {
256+
Err(e) => {
257+
let config_path = file.clone();
258+
let config_path_old =
259+
format!("{}.old", file.to_string_lossy());
260+
fs::rename(
261+
config_path.clone(),
262+
config_path_old.clone(),
263+
)?;
264+
265+
Self::default().save(file)?;
266+
267+
Err(anyhow::anyhow!("{}\n Old file was renamed to {:?}.\n Defaults loaded and saved as {:?}",
268+
e,config_path_old,config_path.to_string_lossy()))
269+
}
270+
Ok(res) => Ok(res),
261271
}
262-
Ok(def)
272+
} else {
273+
Self::default().save(file)?;
274+
Ok(Self::default())
263275
}
264276
}
265-
266-
pub fn init(theme_path: PathBuf) -> Self {
267-
Self::init_internal(theme_path).unwrap_or_default()
268-
}
269277
}
270278

271279
impl Default for Theme {

0 commit comments

Comments
 (0)