on master help sign in
Add option to generate a default config file, fixes #870
376c556Josh Mickley
src/bin/bat/clap_app.rs+8
@@ -370,6 +370,14 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
370 .hidden(true)
370 .hidden(true)
371 .help("Show path to the configuration file."),
371 .help("Show path to the configuration file."),
372 )
372 )
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+ )
373 .arg(
381 .arg(
374 Arg::with_name("config-dir")
382 Arg::with_name("config-dir")
375 .long("config-dir")
383 .long("config-dir")
src/bin/bat/config.rs+41
@@ -1,6 +1,7 @@
1use std::env;
1use std::env;
2use std::ffi::OsString;
2use std::ffi::OsString;
3use std::fs;
3use std::fs;
4+use std::io::{self, Write};
4use std::path::PathBuf;
5use std::path::PathBuf;
5
6
6use shell_words;
7use shell_words;
@@ -15,6 +16,46 @@ pub fn config_file() -> PathBuf {
15 .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
16 .unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
16}
17}
17
18
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+
18pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
59pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
19 Ok(fs::read_to_string(config_file())
60 Ok(fs::read_to_string(config_file())
20 .ok()
61 .ok()
src/bin/bat/main.rs+5−1
@@ -22,7 +22,7 @@ use std::process;
22use ansi_term::Colour::Green;
22use ansi_term::Colour::Green;
23use ansi_term::Style;
23use ansi_term::Style;
24
24
25−use crate::{app::App, config::config_file};
25+use crate::{app::App, config::{config_file, generate_config_file}};
26use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
26use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
27use bat::Controller;
27use bat::Controller;
28use directories::PROJECT_DIRS;
28use directories::PROJECT_DIRS;
@@ -188,6 +188,10 @@ fn run() -> Result<bool> {
188 } else if app.matches.is_present("config-file") {
188 } else if app.matches.is_present("config-file") {
189 println!("{}", config_file().to_string_lossy());
189 println!("{}", config_file().to_string_lossy());
190
190
191+ Ok(true)
192+ } else if app.matches.is_present("generate-config-file") {
193+ generate_config_file();
194+
191 Ok(true)
195 Ok(true)
192 } else if app.matches.is_present("config-dir") {
196 } else if app.matches.is_present("config-dir") {
193 writeln!(io::stdout(), "{}", config_dir())?;
197 writeln!(io::stdout(), "{}", config_dir())?;