Skip to content

Add option to generate a default config file, fixes #870#885

Merged
sharkdp merged 3 commits intosharkdp:masterfrom
jmick414:generate-config-file
Mar 26, 2020
Merged

Add option to generate a default config file, fixes #870#885
sharkdp merged 3 commits intosharkdp:masterfrom
jmick414:generate-config-file

Conversation

@jmick414
Copy link
Copy Markdown
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
Copy Markdown
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.

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

print!("Overwrite? (y/n): ");
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Thanks @sharkdp! I'll be adapting accordingly.

Copy link
Copy Markdown
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