1use std::collections::HashSet;
5use atty::{self, Stream};
16use assets::BAT_THEME_DEFAULT;
17use config::{get_args_from_config_file, get_args_from_env_var};
19use inputfile::InputFile;
20use line_range::LineRange;
21use style::{OutputComponent, OutputComponents, OutputWrap};
22use syntax_mapping::SyntaxMapping;
25#[derive(Debug, Clone, Copy, PartialEq)]
33pub struct Config<'a> {
34 /// List of files to print
35 pub files: Vec<InputFile<'a>>,
37 /// The explicitly configured language, if any
38 pub language: Option<&'a str>,
40 /// The character width of the terminal
41 pub term_width: usize,
43 /// The width of tab characters.
44 /// Currently, a value of 0 will cause tabs to be passed through without expanding them.
47 /// Whether or not to simply loop through all input (`cat` mode)
48 pub loop_through: bool,
50 /// Whether or not the output should be colorized
51 pub colored_output: bool,
53 /// Whether or not the output terminal supports true color
56 /// Style elements (grid, line numbers, ...)
57 pub output_components: OutputComponents,
59 /// Text wrapping mode
60 pub output_wrap: OutputWrap,
63 pub paging_mode: PagingMode,
65 /// The range lines that should be printed, if specified
66 pub line_range: Option<LineRange>,
68 /// The syntax highlighting theme
71 /// File extension/name mappings
72 pub syntax_mapping: SyntaxMapping,
75fn is_truecolor_terminal() -> bool {
77 .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
82 pub matches: ArgMatches<'static>,
83 interactive_output: bool,
87 pub fn new() -> Result<Self> {
89 let _ = ansi_term::enable_ansi_support();
91 let interactive_output = atty::is(Stream::Stdout);
94 matches: Self::matches(interactive_output)?,
99 fn matches(interactive_output: bool) -> Result<ArgMatches<'static>> {
100 let args = if wild::args_os().nth(1) == Some("cache".into())
101 || wild::args_os().any(|arg| arg == "--no-config")
103 // Skip the arguments in bats config file
105 wild::args_os().collect::<Vec<_>>()
107 let mut cli_args = wild::args_os();
109 // Read arguments from bats config file
110 let mut args = get_args_from_env_var()
111 .unwrap_or_else(|| get_args_from_config_file())
112 .chain_err(|| "Could not parse configuration file")?;
114 // Put the zero-th CLI argument (program name) first
115 args.insert(0, cli_args.next().unwrap());
117 // .. and the rest at the end
118 cli_args.for_each(|a| args.push(a));
123 Ok(clap_app::build_app(interactive_output).get_matches_from(args))
126 pub fn config(&self) -> Result<Config> {
127 let files = self.files();
128 let output_components = self.output_components()?;
130 let paging_mode = match self.matches.value_of("paging") {
131 Some("always") => PagingMode::Always,
132 Some("never") => PagingMode::Never,
133 Some("auto") | _ => {
134 if files.contains(&InputFile::StdIn) {
135 // If we are reading from stdin, only enable paging if we write to an
136 // interactive terminal and if we do not *read* from an interactive
138 if self.interactive_output && !atty::is(Stream::Stdin) {
139 PagingMode::QuitIfOneScreen
144 if self.interactive_output {
145 PagingMode::QuitIfOneScreen
153 let mut syntax_mapping = SyntaxMapping::new();
155 if let Some(values) = self.matches.values_of("map-syntax") {
156 for from_to in values {
157 let parts: Vec<_> = from_to.split(":").collect();
159 if parts.len() != 2 {
160 return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into());
163 syntax_mapping.insert(parts[0].into(), parts[1].into());
168 true_color: is_truecolor_terminal(),
169 language: self.matches.value_of("language"),
170 output_wrap: if !self.interactive_output {
171 // We don't have the tty width when piping to another program.
172 // There's no point in wrapping when this is the case.
175 match self.matches.value_of("wrap") {
176 Some("character") => OutputWrap::Character,
177 Some("never") => OutputWrap::None,
178 Some("auto") | _ => {
179 if output_components.plain() {
182 OutputWrap::Character
187 colored_output: match self.matches.value_of("color") {
188 Some("always") => true,
189 Some("never") => false,
190 Some("auto") | _ => self.interactive_output,
195 .value_of("terminal-width")
196 .and_then(|w| w.parse().ok())
197 .unwrap_or(Term::stdout().size().1 as usize),
198 loop_through: !(self.interactive_output
199 || self.matches.value_of("color") == Some("always")
200 || self.matches.value_of("decorations") == Some("always")),
206 .or_else(|| env::var("BAT_TABS").ok())
207 .and_then(|t| t.parse().ok())
209 if output_components.plain() && paging_mode == PagingMode::Never {
219 .or_else(|| env::var("BAT_THEME").ok())
220 .unwrap_or(String::from(BAT_THEME_DEFAULT)),
221 line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
227 fn files(&self) -> Vec<InputFile> {
236 InputFile::Ordinary(filename)
241 .unwrap_or_else(|| vec![InputFile::StdIn])
244 fn output_components(&self) -> Result<OutputComponents> {
245 let matches = &self.matches;
247 if matches.value_of("decorations") == Some("never") {
249 } else if matches.is_present("number") {
250 [OutputComponent::Numbers].iter().cloned().collect()
251 } else if matches.is_present("plain") {
252 [OutputComponent::Plain].iter().cloned().collect()
254 let env_style_components: Option<Vec<OutputComponent>> =
255 transpose(env::var("BAT_STYLE").ok().map(|style_str| {
258 .map(|x| OutputComponent::from_str(&x))
259 .collect::<Result<Vec<OutputComponent>>>()
262 values_t!(matches.values_of("style"), OutputComponent)
264 .or(env_style_components)
265 .unwrap_or(vec![OutputComponent::Full])
267 .map(|style| style.components(self.interactive_output))
268 .fold(HashSet::new(), |mut acc, components| {
269 acc.extend(components.iter().cloned());
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list