2018-10-07 23:22:42 +02:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::fs;
|
|
|
|
|
2018-10-11 21:39:36 +02:00
|
|
|
use shell_words;
|
|
|
|
|
2018-10-07 23:22:42 +02:00
|
|
|
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("#"))
|
2018-10-11 21:39:36 +02:00
|
|
|
.flat_map(|line| shell_words::split(line).unwrap())
|
2018-10-07 23:22:42 +02:00
|
|
|
.map(|line| line.into())
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
let args = get_args_from_str("");
|
|
|
|
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!(
|
2018-10-11 21:39:36 +02:00
|
|
|
vec!["-p", "--style", "numbers,changes", "--color=always"],
|
2018-10-07 23:22:42 +02:00
|
|
|
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!(
|
2018-10-11 21:39:36 +02:00
|
|
|
vec!["-p", "--style", "numbers,changes", "--color=always"],
|
2018-10-07 23:22:42 +02:00
|
|
|
get_args_from_str(config)
|
|
|
|
);
|
|
|
|
}
|