mirror of
				https://github.com/sharkdp/bat.git
				synced 2025-10-30 22:54:07 +00:00 
			
		
		
		
	Graceful handling of error conditions
This commit is contained in:
		
				
					committed by
					
						 David Peter
						David Peter
					
				
			
			
				
	
			
			
			
						parent
						
							376c556862
						
					
				
				
					commit
					40a827ebcb
				
			| @@ -16,28 +16,31 @@ pub fn config_file() -> PathBuf { | |||||||
|         .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config")) |         .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config")) | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn generate_config_file() { | pub fn generate_config_file() -> bat::errors::Result<()> { | ||||||
|     let config_file = config_file(); |     let config_file = config_file(); | ||||||
|     if config_file.exists() { |     if config_file.exists() { | ||||||
|         println!("A config file already exists at: {}", config_file.to_string_lossy()); |         println!("A config file already exists at: {}", config_file.to_string_lossy()); | ||||||
|  |  | ||||||
|         print!("Overwrite? (y/n): "); |         print!("Overwrite? (y/N): "); | ||||||
|         let _ = io::stdout().flush(); |         io::stdout().flush()?; | ||||||
|         let mut decision = String::new(); |         let mut decision = String::new(); | ||||||
|         io::stdin().read_line(&mut decision).expect("Failed to read input"); |         io::stdin().read_line(&mut decision)?; | ||||||
|  |  | ||||||
|         if !decision.trim().eq_ignore_ascii_case("Y") { |         if !decision.trim().eq_ignore_ascii_case("Y") { | ||||||
|             return; |             return Ok(()); | ||||||
|         } |         } | ||||||
|     } else { |     } else { | ||||||
|         let config_dir = config_file.parent().unwrap(); |         let config_dir = config_file.parent(); | ||||||
|         if !config_dir.exists() { |         match config_dir { | ||||||
|             fs::create_dir(config_dir).expect("Unable to create config directory"); |             Some(path) => fs::create_dir_all(path)?, | ||||||
|  |             None => return Ok(Err(format!("Unable to write config file to: {}", config_file.to_string_lossy()))?), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     let default_config = "# Specify desired theme (e.g. \"TwoDark\") |     let default_config = r#"# bat config | ||||||
| #--theme=\"TwoDark\" |  | ||||||
|  | # Specify desired theme (e.g. "TwoDark").  Issue `bat --list-themes` for a list of all available themes | ||||||
|  | #--theme="TwoDark" | ||||||
|  |  | ||||||
| # Enable this to use italic text on the terminal (not supported on all terminals): | # Enable this to use italic text on the terminal (not supported on all terminals): | ||||||
| #--italic-text=always | #--italic-text=always | ||||||
| @@ -46,14 +49,16 @@ pub fn generate_config_file() { | |||||||
| #--paging=never | #--paging=never | ||||||
|  |  | ||||||
| # Use C++ syntax for .ino files | # Use C++ syntax for .ino files | ||||||
| #--map-syntax \"*.ino:C++\" | #--map-syntax "*.ino:C++" | ||||||
|  |  | ||||||
| # Use \".gitignore\"-style highlighting for \".ignore\" files | # Use ".gitignore"-style highlighting for ".ignore" files | ||||||
| #--map-syntax \".ignore:Git Ignore\" | #--map-syntax ".ignore:Git Ignore" | ||||||
| "; | "#; | ||||||
|  |  | ||||||
|     fs::write(&config_file, default_config).expect("Error writing config file!"); |     fs::write(&config_file, default_config)?; | ||||||
|     println!("Success! Config file written to {}", config_file.to_string_lossy()); |     println!("Success! Config file written to {}", config_file.to_string_lossy()); | ||||||
|  |  | ||||||
|  |     return Ok(()); | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> { | pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> { | ||||||
|   | |||||||
| @@ -179,19 +179,15 @@ fn run() -> Result<bool> { | |||||||
|  |  | ||||||
|             if app.matches.is_present("list-languages") { |             if app.matches.is_present("list-languages") { | ||||||
|                 list_languages(&config)?; |                 list_languages(&config)?; | ||||||
|  |  | ||||||
|                 Ok(true) |                 Ok(true) | ||||||
|             } else if app.matches.is_present("list-themes") { |             } else if app.matches.is_present("list-themes") { | ||||||
|                 list_themes(&config)?; |                 list_themes(&config)?; | ||||||
|  |  | ||||||
|                 Ok(true) |                 Ok(true) | ||||||
|             } else if app.matches.is_present("config-file") { |             } else if app.matches.is_present("config-file") { | ||||||
|                 println!("{}", config_file().to_string_lossy()); |                 println!("{}", config_file().to_string_lossy()); | ||||||
|  |  | ||||||
|                 Ok(true) |                 Ok(true) | ||||||
|             } else if app.matches.is_present("generate-config-file") { |             } else if app.matches.is_present("generate-config-file") { | ||||||
|                 generate_config_file(); |                 generate_config_file()?; | ||||||
|  |  | ||||||
|                 Ok(true) |                 Ok(true) | ||||||
|             } else if app.matches.is_present("config-dir") { |             } else if app.matches.is_present("config-dir") { | ||||||
|                 writeln!(io::stdout(), "{}", config_dir())?; |                 writeln!(io::stdout(), "{}", config_dir())?; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user