1
0
mirror of https://github.com/sharkdp/bat synced 2026-07-24 17:13:19 +00:00

Move config.rs into src/bin/bat/

This commit is contained in:
Fahmi Akbar Wildana
2019-10-06 08:44:14 +07:00
committed by David Peter
parent d3243903c0
commit a2ee753b25
4 changed files with 7 additions and 6 deletions
+4 -2
View File
@@ -4,7 +4,10 @@ use std::str::FromStr;
use atty::{self, Stream};
use crate::clap_app;
use crate::{
clap_app,
config::{get_args_from_config_file, get_args_from_env_var},
};
use clap::ArgMatches;
use wild;
@@ -15,7 +18,6 @@ use ansi_term;
use bat::{
assets::BAT_THEME_DEFAULT,
config::{get_args_from_config_file, get_args_from_env_var},
errors::*,
inputfile::InputFile,
line_range::{LineRange, LineRanges},
+104
View File
@@ -0,0 +1,104 @@
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::PathBuf;
use shell_words;
use bat::{dirs::PROJECT_DIRS, util::transpose};
pub fn config_file() -> PathBuf {
env::var("BAT_CONFIG_PATH")
.ok()
.map(PathBuf::from)
.filter(|config_path| config_path.is_file())
.unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
}
pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
Ok(transpose(
fs::read_to_string(config_file())
.ok()
.map(|content| get_args_from_str(&content)),
)?
.unwrap_or_else(|| vec![]))
}
pub fn get_args_from_env_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {
env::var("BAT_OPTS").ok().map(|s| get_args_from_str(&s))
}
fn get_args_from_str(content: &str) -> Result<Vec<OsString>, shell_words::ParseError> {
let args_per_line = content
.split('\n')
.map(|line| line.trim())
.filter(|line| !line.is_empty())
.filter(|line| !line.starts_with('#'))
.map(|line| shell_words::split(line))
.collect::<Result<Vec<_>, _>>()?;
Ok(args_per_line
.iter()
.flatten()
.map(|line| line.into())
.collect())
}
#[test]
fn empty() {
let args = get_args_from_str("").unwrap();
assert!(args.is_empty());
}
#[test]
fn single() {
assert_eq!(vec!["--plain"], get_args_from_str("--plain").unwrap());
}
#[test]
fn multiple() {
assert_eq!(
vec!["--plain", "--language=cpp"],
get_args_from_str("--plain --language=cpp").unwrap()
);
}
#[test]
fn quotes() {
assert_eq!(
vec!["--theme", "Sublime Snazzy"],
get_args_from_str("--theme \"Sublime Snazzy\"").unwrap()
);
}
#[test]
fn multi_line() {
let config = "
-p
--style numbers,changes
--color=always
";
assert_eq!(
vec!["-p", "--style", "numbers,changes", "--color=always"],
get_args_from_str(config).unwrap()
);
}
#[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).unwrap()
);
}
+2 -1
View File
@@ -6,6 +6,7 @@ extern crate clap;
mod app;
mod clap_app;
mod config;
mod controller;
mod decorations;
mod output;
@@ -22,12 +23,12 @@ use ansi_term::Style;
use crate::{
app::{App, Config},
config::config_file,
controller::Controller,
};
use bat::{
assets::{cache_dir, clear_assets, config_dir, HighlightingAssets},
config::config_file,
errors::*,
inputfile::InputFile,
style::{OutputComponent, OutputComponents},