on master help sign in
Graceful handling of error conditions
40a827eJosh Mickley
src/bin/bat/config.rs+20−15
@@ -16,28 +16,31 @@ pub fn config_file() -> PathBuf {
16 .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
16 .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
17}
17}
18
18
19−pub fn generate_config_file() {
19+pub fn generate_config_file() -> bat::errors::Result<()> {
20 let config_file = config_file();
20 let config_file = config_file();
21 if config_file.exists() {
21 if config_file.exists() {
22 println!("A config file already exists at: {}", config_file.to_string_lossy());
22 println!("A config file already exists at: {}", config_file.to_string_lossy());
23
23
24− print!("Overwrite? (y/n): ");
24+ print!("Overwrite? (y/N): ");
25− let _ = io::stdout().flush();
25+ io::stdout().flush()?;
26 let mut decision = String::new();
26 let mut decision = String::new();
27− io::stdin().read_line(&mut decision).expect("Failed to read input");
27+ io::stdin().read_line(&mut decision)?;
28
28
29 if !decision.trim().eq_ignore_ascii_case("Y") {
29 if !decision.trim().eq_ignore_ascii_case("Y") {
30− return;
30+ return Ok(());
31 }
31 }
32 } else {
32 } else {
33− let config_dir = config_file.parent().unwrap();
33+ let config_dir = config_file.parent();
34− if !config_dir.exists() {
34+ match config_dir {
35− fs::create_dir(config_dir).expect("Unable to create config directory");
35+ Some(path) => fs::create_dir_all(path)?,
36+ None => return Ok(Err(format!("Unable to write config file to: {}", config_file.to_string_lossy()))?),
36 }
37 }
37 }
38 }
38
39
39− let default_config = "# Specify desired theme (e.g. \"TwoDark\")
40+ let default_config = r#"# bat config
41+
42+# Specify desired theme (e.g. "TwoDark"). Issue `bat --list-themes` for a list of all available themes
40−#--theme=\"TwoDark\"
43+#--theme="TwoDark"
41
44
42# Enable this to use italic text on the terminal (not supported on all terminals):
45# Enable this to use italic text on the terminal (not supported on all terminals):
43#--italic-text=always
46#--italic-text=always
@@ -46,14 +49,16 @@ pub fn generate_config_file() {
46#--paging=never
49#--paging=never
47
50
48# Use C++ syntax for .ino files
51# Use C++ syntax for .ino files
49−#--map-syntax \"*.ino:C++\"
52+#--map-syntax "*.ino:C++"
50
53
51−# Use \".gitignore\"-style highlighting for \".ignore\" files
54+# Use ".gitignore"-style highlighting for ".ignore" files
52−#--map-syntax \".ignore:Git Ignore\"
55+#--map-syntax ".ignore:Git Ignore"
53−";
56+"#;
54
57
55− fs::write(&config_file, default_config).expect("Error writing config file!");
58+ fs::write(&config_file, default_config)?;
56 println!("Success! Config file written to {}", config_file.to_string_lossy());
59 println!("Success! Config file written to {}", config_file.to_string_lossy());
60+
61+ return Ok(());
57}
62}
58
63
59pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
64pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
src/bin/bat/main.rs+1−5
@@ -179,19 +179,15 @@ fn run() -> Result<bool> {
179
179
180 if app.matches.is_present("list-languages") {
180 if app.matches.is_present("list-languages") {
181 list_languages(&config)?;
181 list_languages(&config)?;
182−
183 Ok(true)
182 Ok(true)
184 } else if app.matches.is_present("list-themes") {
183 } else if app.matches.is_present("list-themes") {
185 list_themes(&config)?;
184 list_themes(&config)?;
186−
187 Ok(true)
185 Ok(true)
188 } else if app.matches.is_present("config-file") {
186 } else if app.matches.is_present("config-file") {
189 println!("{}", config_file().to_string_lossy());
187 println!("{}", config_file().to_string_lossy());
190−
191 Ok(true)
188 Ok(true)
192 } else if app.matches.is_present("generate-config-file") {
189 } else if app.matches.is_present("generate-config-file") {
193− generate_config_file();
190+ generate_config_file()?;
194−
195 Ok(true)
191 Ok(true)
196 } else if app.matches.is_present("config-dir") {
192 } else if app.matches.is_present("config-dir") {
197 writeln!(io::stdout(), "{}", config_dir())?;
193 writeln!(io::stdout(), "{}", config_dir())?;