on master help sign in
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 ```

commit8275b0436d9501614f29b831b164687b3bc281c0 authorsharkdp <davidpeter@web.de> dateSun 7 Oct 2018, 23:22 (8 years ago) parent278bde5
6 files changed+96−6
src/app.rs+21−1 src/assets.rs−5 src/clap_app.rs+2 src/config.rs+65 src/dirs.rs+6 src/main.rs+2
src/app.rs+21−1
@@ -14,6 +14,7 @@ use console::Term;
1414use ansi_term;
1515
1616use assets::BAT_THEME_DEFAULT;
17+use config::get_args_from_config_file;
1718use errors::*;
1819use inputfile::InputFile;
1920use line_range::LineRange;
@@ -97,7 +98,26 @@ impl App {
9798 }
9899
99100 fn matches(interactive_output: bool) -> ArgMatches<'static> {
100− clap_app::build_app(interactive_output).get_matches_from(wild::args())
101+ let args = if wild::args_os().nth(1) == Some("cache".into()) {
102+ // Skip the arguments in bats config file
103+
104+ wild::args_os().collect::<Vec<_>>()
105+ } else {
106+ let mut cli_args = wild::args_os();
107+
108+ // Read arguments from bats config file
109+ let mut args = get_args_from_config_file();
110+
111+ // Put the zero-th CLI argument (program name) first
112+ args.insert(0, cli_args.next().unwrap());
113+
114+ // .. and the rest at the end
115+ cli_args.for_each(|a| args.push(a));
116+
117+ args
118+ };
119+
120+ clap_app::build_app(interactive_output).get_matches_from(args)
101121 }
102122
103123 pub fn config(&self) -> Result<Config> {
src/assets.rs−5
@@ -13,11 +13,6 @@ use directories::ProjectDirs;
1313use errors::*;
1414use inputfile::{InputFile, InputFileReader};
1515
16−lazy_static! {
17− static ref PROJECT_DIRS: ProjectDirs =
18− ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
19−}
20−
2116pub const BAT_THEME_DEFAULT: &str = "Monokai Extended";
2217
2318pub struct HighlightingAssets {
src/clap_app.rs+2
@@ -84,6 +84,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
8484 .value_name("style-components")
8585 .use_delimiter(true)
8686 .takes_value(true)
87+ .overrides_with("style")
8788 .possible_values(&[
8889 "auto", "full", "plain", "changes", "header", "grid", "numbers",
8990 ])
@@ -207,6 +208,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
207208 .arg(
208209 Arg::with_name("tabs")
209210 .long("tabs")
211+ .overrides_with("tabs")
210212 .takes_value(true)
211213 .value_name("T")
212214 .validator(
src/config.rsnew file+65
@@ -0,0 +1,65 @@
1+use std::ffi::OsString;
2+use std::fs;
3+
4+use dirs::PROJECT_DIRS;
5+
6+pub fn get_args_from_config_file() -> Vec<OsString> {
7+ let config_file = PROJECT_DIRS.config_dir().join("config");
8+ fs::read_to_string(config_file)
9+ .map(|content| get_args_from_str(&content))
10+ .unwrap_or(vec![])
11+}
12+
13+fn get_args_from_str<'a>(content: &'a str) -> Vec<OsString> {
14+ content
15+ .split('\n')
16+ .map(|line| line.trim())
17+ .filter(|line| !line.is_empty())
18+ .filter(|line| !line.starts_with("#"))
19+ .map(|line| line.into())
20+ .collect()
21+}
22+
23+#[test]
24+fn empty() {
25+ let args = get_args_from_str("");
26+ println!("{:?}", args);
27+ assert!(args.is_empty());
28+}
29+
30+#[test]
31+fn single() {
32+ assert_eq!(vec!["--plain"], get_args_from_str("--plain"));
33+}
34+
35+#[test]
36+fn multiple() {
37+ let config = "
38+ -p
39+ --style numbers,changes
40+
41+ --color=always
42+ ";
43+ assert_eq!(
44+ vec!["-p", "--style numbers,changes", "--color=always"],
45+ get_args_from_str(config)
46+ );
47+}
48+
49+#[test]
50+fn comments() {
51+ let config = "
52+ # plain style
53+ -p
54+
55+ # show line numbers and Git modifications
56+ --style numbers,changes
57+
58+ # Always show ANSI colors
59+ --color=always
60+ ";
61+ assert_eq!(
62+ vec!["-p", "--style numbers,changes", "--color=always"],
63+ get_args_from_str(config)
64+ );
65+}
src/dirs.rsnew file+6
@@ -0,0 +1,6 @@
1+use directories::ProjectDirs;
2+
3+lazy_static! {
4+ pub static ref PROJECT_DIRS: ProjectDirs =
5+ ProjectDirs::from("", "", crate_name!()).expect("Could not get home directory");
6+}
src/main.rs+2
@@ -23,9 +23,11 @@ extern crate wild;
2323mod app;
2424mod assets;
2525mod clap_app;
26+mod config;
2627mod controller;
2728mod decorations;
2829mod diff;
30+mod dirs;
2931mod inputfile;
3032mod line_range;
3133mod output;