-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to generate a default config file, fixes #870 #885
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for your contribution and welcome to the Rust community 😄.
I added a few inline comments.
src/bin/bat/config.rs
Outdated
if config_file.exists() { | ||
println!("A config file already exists at: {}", config_file.to_string_lossy()); | ||
|
||
print!("Overwrite? (y/n): "); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make it clear that "no" is the default here:
Overwrite? (y/N):
src/bin/bat/config.rs
Outdated
println!("A config file already exists at: {}", config_file.to_string_lossy()); | ||
|
||
print!("Overwrite? (y/n): "); | ||
let _ = io::stdout().flush(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you change this function to return Result<()>
, you can use
io::stdout().flush()?;
here. Another alternative would be io::stdout().flush().ok()
.
src/bin/bat/config.rs
Outdated
print!("Overwrite? (y/n): "); | ||
let _ = io::stdout().flush(); | ||
let mut decision = String::new(); | ||
io::stdin().read_line(&mut decision).expect("Failed to read input"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this function returns a Result<…>
, you can simply use .read_line(&mut decision)?
instead of .expect(…)
which would kill the process in case of an error.
src/bin/bat/config.rs
Outdated
io::stdin().read_line(&mut decision).expect("Failed to read input"); | ||
|
||
if !decision.trim().eq_ignore_ascii_case("Y") { | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would need to be a return Ok(());
. Similarly, you need to use Ok(())
in the last line of this function.
src/bin/bat/config.rs
Outdated
return; | ||
} | ||
} else { | ||
let config_dir = config_file.parent().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.unwrap()
also kills the process if an error occurs. It's better to use .parent()?
.
src/bin/bat/config.rs
Outdated
} | ||
} else { | ||
let config_dir = config_file.parent().unwrap(); | ||
if !config_dir.exists() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing a check like this before creating a directory or file is generally discouraged because it can lead to race conditions. Even if that is unlikely to be a problem here, the alternative is much simpler: simply create the directory and ignore the fact that it could already exist.
Now fs::create_dir
would return an error in case the path already exists, but you can (and should) use fs::create_dir_all
anyway. create_dir
would fail if the parent directory would not exist (~/.config
on Unix systems).
src/bin/bat/config.rs
Outdated
} | ||
} | ||
|
||
let default_config = "# Specify desired theme (e.g. \"TwoDark\") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent having to escape all quotes in this default config, you can use a raw string:
let default_config = r#"…
…"#;
src/bin/bat/config.rs
Outdated
} | ||
|
||
let default_config = "# Specify desired theme (e.g. \"TwoDark\") | ||
#--theme=\"TwoDark\" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could maybe mention bat --list-themes
here in the comment.
src/bin/bat/config.rs
Outdated
#--map-syntax \".ignore:Git Ignore\" | ||
"; | ||
|
||
fs::write(&config_file, default_config).expect("Error writing config file!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar here, please replace .expect(…)
by a ?
.
Thanks @sharkdp! I'll be adapting accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much!
I took a shot at implementing the
--generate-config-file
feature request per #870.This is my 1st foray into Rust so I'm open to feedback/suggestions for improvement. Also... thoughts on if the default config file content is sufficient or should be modified?
Thanks!