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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ non-default location of the configuration file:
export BAT_CONFIG_PATH="/path/to/bat.conf"
```

A default configuration file can be created with the `--generate-config-file` option.
```bash
bat --generate-config-file
```

### Format

The configuration file is a simple list of command line arguments. Use `bat --help` to see a full list of possible options and values. In addition, you can add comments by prepending a line with the `#` character.
Expand Down
8 changes: 8 additions & 0 deletions src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
46 changes: 46 additions & 0 deletions src/bin/bat/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +16,51 @@ pub fn config_file() -> PathBuf {
.unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
}

pub fn generate_config_file() -> bat::errors::Result<()> {
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): ");
io::stdout().flush()?;
let mut decision = String::new();
io::stdin().read_line(&mut decision)?;

if !decision.trim().eq_ignore_ascii_case("Y") {
return Ok(());
}
} else {
let config_dir = config_file.parent();
match config_dir {
Some(path) => fs::create_dir_all(path)?,
None => return Ok(Err(format!("Unable to write config file to: {}", config_file.to_string_lossy()))?),
}
}

let default_config = r#"# bat config

# Specify desired theme (e.g. "TwoDark"). Issue `bat --list-themes` for a list of all available themes
#--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)?;
println!("Success! Config file written to {}", config_file.to_string_lossy());

return Ok(());
}

pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
Ok(fs::read_to_string(config_file())
.ok()
Expand Down
8 changes: 4 additions & 4 deletions src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -179,15 +179,15 @@ fn run() -> Result<bool> {

if app.matches.is_present("list-languages") {
list_languages(&config)?;

Ok(true)
} else if app.matches.is_present("list-themes") {
list_themes(&config)?;

Ok(true)
} 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())?;
Expand Down