Skip to content
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

Merged
merged 3 commits into from
Mar 26, 2020

Conversation

jmick414
Copy link
Contributor

@jmick414 jmick414 commented Mar 24, 2020

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!

Copy link
Owner

@sharkdp sharkdp left a 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 Show resolved Hide resolved
if config_file.exists() {
println!("A config file already exists at: {}", config_file.to_string_lossy());

print!("Overwrite? (y/n): ");
Copy link
Owner

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):

println!("A config file already exists at: {}", config_file.to_string_lossy());

print!("Overwrite? (y/n): ");
let _ = io::stdout().flush();
Copy link
Owner

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().

print!("Overwrite? (y/n): ");
let _ = io::stdout().flush();
let mut decision = String::new();
io::stdin().read_line(&mut decision).expect("Failed to read input");
Copy link
Owner

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.

io::stdin().read_line(&mut decision).expect("Failed to read input");

if !decision.trim().eq_ignore_ascii_case("Y") {
return;
Copy link
Owner

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.

return;
}
} else {
let config_dir = config_file.parent().unwrap();
Copy link
Owner

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()?.

}
} else {
let config_dir = config_file.parent().unwrap();
if !config_dir.exists() {
Copy link
Owner

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).

}
}

let default_config = "# Specify desired theme (e.g. \"TwoDark\")
Copy link
Owner

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#"…
…"#;

}

let default_config = "# Specify desired theme (e.g. \"TwoDark\")
#--theme=\"TwoDark\"
Copy link
Owner

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.

#--map-syntax \".ignore:Git Ignore\"
";

fs::write(&config_file, default_config).expect("Error writing config file!");
Copy link
Owner

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 ?.

@jmick414
Copy link
Contributor Author

Thanks @sharkdp! I'll be adapting accordingly.

Copy link
Owner

@sharkdp sharkdp left a 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants