on master help sign in
Add option to generate a default config file, fixes #870
commit376c5568628c6d95a8b0b16ddf99055c8ea93e89 authorJosh Mickley <josh.mickley@tuta.io> dateMon 23 Mar 2020, 22:08 (7 years ago) parent4aef8c1
3 files changed+54−1
src/bin/bat/clap_app.rs+8 src/bin/bat/config.rs+41 src/bin/bat/main.rs+5−1
src/bin/bat/clap_app.rs+8
@@ -370,6 +370,14 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
370370 .hidden(true)
371371 .help("Show path to the configuration file."),
372372 )
373+ .arg(
374+ Arg::with_name("generate-config-file")
375+ .long("generate-config-file")
376+ .conflicts_with("list-languages")
377+ .conflicts_with("list-themes")
378+ .hidden(true)
379+ .help("Generates a default configuration file."),
380+ )
373381 .arg(
374382 Arg::with_name("config-dir")
375383 .long("config-dir")
src/bin/bat/config.rs+41
@@ -1,6 +1,7 @@
11use std::env;
22use std::ffi::OsString;
33use std::fs;
4+use std::io::{self, Write};
45use std::path::PathBuf;
56
67use shell_words;
@@ -15,6 +16,46 @@ pub fn config_file() -> PathBuf {
1516 .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
1617}
1718
19+pub fn generate_config_file() {
20+ let config_file = config_file();
21+ if config_file.exists() {
22+ println!("A config file already exists at: {}", config_file.to_string_lossy());
23+
24+ print!("Overwrite? (y/n): ");
25+ let _ = io::stdout().flush();
26+ let mut decision = String::new();
27+ io::stdin().read_line(&mut decision).expect("Failed to read input");
28+
29+ if !decision.trim().eq_ignore_ascii_case("Y") {
30+ return;
31+ }
32+ } else {
33+ let config_dir = config_file.parent().unwrap();
34+ if !config_dir.exists() {
35+ fs::create_dir(config_dir).expect("Unable to create config directory");
36+ }
37+ }
38+
39+ let default_config = "# Specify desired theme (e.g. \"TwoDark\")
40+#--theme=\"TwoDark\"
41+
42+# Enable this to use italic text on the terminal (not supported on all terminals):
43+#--italic-text=always
44+
45+# Uncomment the following line to disable automatic paging:
46+#--paging=never
47+
48+# Use C++ syntax for .ino files
49+#--map-syntax \"*.ino:C++\"
50+
51+# Use \".gitignore\"-style highlighting for \".ignore\" files
52+#--map-syntax \".ignore:Git Ignore\"
53+";
54+
55+ fs::write(&config_file, default_config).expect("Error writing config file!");
56+ println!("Success! Config file written to {}", config_file.to_string_lossy());
57+}
58+
1859pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
1960 Ok(fs::read_to_string(config_file())
2061 .ok()
src/bin/bat/main.rs+5−1
@@ -22,7 +22,7 @@ use std::process;
2222use ansi_term::Colour::Green;
2323use ansi_term::Style;
2424
25−use crate::{app::App, config::config_file};
25+use crate::{app::App, config::{config_file, generate_config_file}};
2626use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
2727use bat::Controller;
2828use directories::PROJECT_DIRS;
@@ -188,6 +188,10 @@ fn run() -> Result<bool> {
188188 } else if app.matches.is_present("config-file") {
189189 println!("{}", config_file().to_string_lossy());
190190
191+ Ok(true)
192+ } else if app.matches.is_present("generate-config-file") {
193+ generate_config_file();
194+
191195 Ok(true)
192196 } else if app.matches.is_present("config-dir") {
193197 writeln!(io::stdout(), "{}", config_dir())?;