mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-02 19:32:25 +01:00
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:
65
src/config.rs
Normal file
65
src/config.rs
Normal 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)
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user