@@ -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+}