mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-26 09:43:51 +01:00
Add option to generate a default config file, fixes #870
This commit is contained in:
parent
4aef8c180a
commit
376c556862
@ -370,6 +370,14 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
|
||||
.hidden(true)
|
||||
.help("Show path to the configuration file."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("generate-config-file")
|
||||
.long("generate-config-file")
|
||||
.conflicts_with("list-languages")
|
||||
.conflicts_with("list-themes")
|
||||
.hidden(true)
|
||||
.help("Generates a default configuration file."),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("config-dir")
|
||||
.long("config-dir")
|
||||
|
@ -1,6 +1,7 @@
|
||||
use std::env;
|
||||
use std::ffi::OsString;
|
||||
use std::fs;
|
||||
use std::io::{self, Write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use shell_words;
|
||||
@ -15,6 +16,46 @@ pub fn config_file() -> PathBuf {
|
||||
.unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
|
||||
}
|
||||
|
||||
pub fn generate_config_file() {
|
||||
let config_file = config_file();
|
||||
if config_file.exists() {
|
||||
println!("A config file already exists at: {}", config_file.to_string_lossy());
|
||||
|
||||
print!("Overwrite? (y/n): ");
|
||||
let _ = io::stdout().flush();
|
||||
let mut decision = String::new();
|
||||
io::stdin().read_line(&mut decision).expect("Failed to read input");
|
||||
|
||||
if !decision.trim().eq_ignore_ascii_case("Y") {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
let config_dir = config_file.parent().unwrap();
|
||||
if !config_dir.exists() {
|
||||
fs::create_dir(config_dir).expect("Unable to create config directory");
|
||||
}
|
||||
}
|
||||
|
||||
let default_config = "# Specify desired theme (e.g. \"TwoDark\")
|
||||
#--theme=\"TwoDark\"
|
||||
|
||||
# Enable this to use italic text on the terminal (not supported on all terminals):
|
||||
#--italic-text=always
|
||||
|
||||
# Uncomment the following line to disable automatic paging:
|
||||
#--paging=never
|
||||
|
||||
# Use C++ syntax for .ino files
|
||||
#--map-syntax \"*.ino:C++\"
|
||||
|
||||
# Use \".gitignore\"-style highlighting for \".ignore\" files
|
||||
#--map-syntax \".ignore:Git Ignore\"
|
||||
";
|
||||
|
||||
fs::write(&config_file, default_config).expect("Error writing config file!");
|
||||
println!("Success! Config file written to {}", config_file.to_string_lossy());
|
||||
}
|
||||
|
||||
pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
|
||||
Ok(fs::read_to_string(config_file())
|
||||
.ok()
|
||||
|
@ -22,7 +22,7 @@ use std::process;
|
||||
use ansi_term::Colour::Green;
|
||||
use ansi_term::Style;
|
||||
|
||||
use crate::{app::App, config::config_file};
|
||||
use crate::{app::App, config::{config_file, generate_config_file}};
|
||||
use assets::{assets_from_cache_or_binary, cache_dir, clear_assets, config_dir};
|
||||
use bat::Controller;
|
||||
use directories::PROJECT_DIRS;
|
||||
@ -188,6 +188,10 @@ fn run() -> Result<bool> {
|
||||
} else if app.matches.is_present("config-file") {
|
||||
println!("{}", config_file().to_string_lossy());
|
||||
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("generate-config-file") {
|
||||
generate_config_file();
|
||||
|
||||
Ok(true)
|
||||
} else if app.matches.is_present("config-dir") {
|
||||
writeln!(io::stdout(), "{}", config_dir())?;
|
||||
|
Loading…
Reference in New Issue
Block a user