@@ -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"))
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());
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)?;
29 if !decision.trim().eq_ignore_ascii_case("Y") {
29 if !decision.trim().eq_ignore_ascii_case("Y") {
30− return;
30+ return Ok(());
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()))?),
39− let default_config = "# Specify desired theme (e.g. \"TwoDark\")
40+ let default_config = r#"# bat config
42+# Specify desired theme (e.g. "TwoDark"). Issue `bat --list-themes` for a list of all available themes
40−#--theme=\"TwoDark\"
43+#--theme="TwoDark"
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
48# Use C++ syntax for .ino files
51# Use C++ syntax for .ino files
49−#--map-syntax \"*.ino:C++\"
52+#--map-syntax "*.ino:C++"
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"
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());
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> {