Add simple configuration file

This allows users to create simple configuration file
(`~/.config/bat/config` on Linux) that has the following format:

    ```bash
    --flag1
    --flag2
    --option1=value1
    # lines beginning with '#' are ignored
    --option2=value2
    # empty lines and trailing whitespace are also ignored

    --option3=value3
    ```
This commit is contained in:
sharkdp 2018-10-07 23:22:42 +02:00 committed by David Peter
parent 278bde5cee
commit 8275b0436d
6 changed files with 96 additions and 6 deletions

View File

@ -14,6 +14,7 @@ use console::Term;
use ansi_term; use ansi_term;
use assets::BAT_THEME_DEFAULT; use assets::BAT_THEME_DEFAULT;
use config::get_args_from_config_file;
use errors::*; use errors::*;
use inputfile::InputFile; use inputfile::InputFile;
use line_range::LineRange; use line_range::LineRange;
@ -97,7 +98,26 @@ impl App {
} }
fn matches(interactive_output: bool) -> ArgMatches<'static> { fn matches(interactive_output: bool) -> ArgMatches<'static> {
clap_app::build_app(interactive_output).get_matches_from(wild::args()) let args = if wild::args_os().nth(1) == Some("cache".into()) {
// Skip the arguments in bats config file
wild::args_os().collect::<Vec<_>>()
} else {
let mut cli_args = wild::args_os();
// Read arguments from bats config file
let mut args = get_args_from_config_file();
// Put the zero-th CLI argument (program name) first
args.insert(0, cli_args.next().unwrap());
// .. and the rest at the end
cli_args.for_each(|a| args.push(a));
args
};
clap_app::build_app(interactive_output).get_matches_from(args)
} }
pub fn config(&self) -> Result<Config> { pub fn config(&self) -> Result<Config> {

View File

@ -13,11 +13,6 @@ use directories::ProjectDirs;
use errors::*; use errors::*;
use inputfile::{InputFile, InputFileReader}; use inputfile::{InputFile, InputFileReader};
lazy_static! {
static ref PROJECT_DIRS: ProjectDirs =
ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
}
pub const BAT_THEME_DEFAULT: &str = "Monokai Extended"; pub const BAT_THEME_DEFAULT: &str = "Monokai Extended";
pub struct HighlightingAssets { pub struct HighlightingAssets {

View File

@ -84,6 +84,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.value_name("style-components") .value_name("style-components")
.use_delimiter(true) .use_delimiter(true)
.takes_value(true) .takes_value(true)
.overrides_with("style")
.possible_values(&[ .possible_values(&[
"auto", "full", "plain", "changes", "header", "grid", "numbers", "auto", "full", "plain", "changes", "header", "grid", "numbers",
]) ])
@ -207,6 +208,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
.arg( .arg(
Arg::with_name("tabs") Arg::with_name("tabs")
.long("tabs") .long("tabs")
.overrides_with("tabs")
.takes_value(true) .takes_value(true)
.value_name("T") .value_name("T")
.validator( .validator(

65
src/config.rs Normal file
View File

@ -0,0 +1,65 @@
use std::ffi::OsString;
use std::fs;
use dirs::PROJECT_DIRS;
pub fn get_args_from_config_file() -> Vec<OsString> {
let config_file = PROJECT_DIRS.config_dir().join("config");
fs::read_to_string(config_file)
.map(|content| get_args_from_str(&content))
.unwrap_or(vec![])
}
fn get_args_from_str<'a>(content: &'a str) -> Vec<OsString> {
content
.split('\n')
.map(|line| line.trim())
.filter(|line| !line.is_empty())
.filter(|line| !line.starts_with("#"))
.map(|line| line.into())
.collect()
}
#[test]
fn empty() {
let args = get_args_from_str("");
println!("{:?}", args);
assert!(args.is_empty());
}
#[test]
fn single() {
assert_eq!(vec!["--plain"], get_args_from_str("--plain"));
}
#[test]
fn multiple() {
let config = "
-p
--style numbers,changes
--color=always
";
assert_eq!(
vec!["-p", "--style numbers,changes", "--color=always"],
get_args_from_str(config)
);
}
#[test]
fn comments() {
let config = "
# plain style
-p
# show line numbers and Git modifications
--style numbers,changes
# Always show ANSI colors
--color=always
";
assert_eq!(
vec!["-p", "--style numbers,changes", "--color=always"],
get_args_from_str(config)
);
}

6
src/dirs.rs Normal file
View File

@ -0,0 +1,6 @@
use directories::ProjectDirs;
lazy_static! {
pub static ref PROJECT_DIRS: ProjectDirs =
ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
}

View File

@ -23,9 +23,11 @@ extern crate wild;
mod app; mod app;
mod assets; mod assets;
mod clap_app; mod clap_app;
mod config;
mod controller; mod controller;
mod decorations; mod decorations;
mod diff; mod diff;
mod dirs;
mod inputfile; mod inputfile;
mod line_range; mod line_range;
mod output; mod output;