1use std::collections::HashSet;
5use atty::{self, Stream};
16use assets::BAT_THEME_DEFAULT;
18use inputfile::InputFile;
19use line_range::LineRange;
20use style::{OutputComponent, OutputComponents, OutputWrap};
22#[derive(Debug, Clone, Copy, PartialEq)]
30pub struct Config<'a> {
31 /// List of files to print
32 pub files: Vec<InputFile<'a>>,
34 /// The explicitly configured language, if any
35 pub language: Option<&'a str>,
37 /// The character width of the terminal
38 pub term_width: usize,
40 /// The width of tab characters.
41 /// Currently, a value of 0 will cause tabs to be passed through without expanding them.
44 /// Whether or not to simply loop through all input (`cat` mode)
45 pub loop_through: bool,
47 /// Whether or not the output should be colorized
48 pub colored_output: bool,
50 /// Whether or not the output terminal supports true color
53 /// Style elements (grid, line numbers, ...)
54 pub output_components: OutputComponents,
56 /// Text wrapping mode
57 pub output_wrap: OutputWrap,
60 pub paging_mode: PagingMode,
62 /// The range lines that should be printed, if specified
63 pub line_range: Option<LineRange>,
65 /// The syntax highlighting theme
68 /// A line to highlight
69 pub highlight_line: Option<usize>,
72fn is_truecolor_terminal() -> bool {
74 .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
78/// Helper function that might appear in Rust stable at some point
79/// (https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.transpose)
80fn transpose<T>(opt: Option<Result<T>>) -> Result<Option<T>> {
81 opt.map_or(Ok(None), |res| res.map(Some))
85 pub matches: ArgMatches<'static>,
86 interactive_output: bool,
90 pub fn new() -> Self {
92 let _ = ansi_term::enable_ansi_support();
94 let interactive_output = atty::is(Stream::Stdout);
97 matches: Self::matches(interactive_output),
102 fn matches(interactive_output: bool) -> ArgMatches<'static> {
103 clap_app::build_app(interactive_output).get_matches_from(wild::args())
106 pub fn config(&self) -> Result<Config> {
107 let files = self.files();
108 let output_components = self.output_components()?;
110 let paging_mode = match self.matches.value_of("paging") {
111 Some("always") => PagingMode::Always,
112 Some("never") => PagingMode::Never,
113 Some("auto") | _ => {
114 if files.contains(&InputFile::StdIn) {
115 // If we are reading from stdin, only enable paging if we write to an
116 // interactive terminal and if we do not *read* from an interactive
118 if self.interactive_output && !atty::is(Stream::Stdin) {
119 PagingMode::QuitIfOneScreen
124 if self.interactive_output {
125 PagingMode::QuitIfOneScreen
134 true_color: is_truecolor_terminal(),
135 language: self.matches.value_of("language"),
136 output_wrap: if !self.interactive_output {
137 // We don't have the tty width when piping to another program.
138 // There's no point in wrapping when this is the case.
141 match self.matches.value_of("wrap") {
142 Some("character") => OutputWrap::Character,
143 Some("never") => OutputWrap::None,
144 Some("auto") | _ => {
145 if output_components.plain() {
148 OutputWrap::Character
153 colored_output: match self.matches.value_of("color") {
154 Some("always") => true,
155 Some("never") => false,
156 Some("auto") | _ => self.interactive_output,
161 .value_of("terminal-width")
162 .and_then(|w| w.parse().ok())
163 .unwrap_or(Term::stdout().size().1 as usize),
164 loop_through: !(self.interactive_output
165 || self.matches.value_of("color") == Some("always")
166 || self.matches.value_of("decorations") == Some("always")),
172 .or_else(|| env::var("BAT_TABS").ok())
173 .and_then(|t| t.parse().ok())
175 if output_components.plain() && paging_mode == PagingMode::Never {
185 .or_else(|| env::var("BAT_THEME").ok())
186 .unwrap_or(String::from(BAT_THEME_DEFAULT)),
187 line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
188 highlight_line: self.matches.value_of("highlight-line").and_then(|w| w.parse().ok()),
193 fn files(&self) -> Vec<InputFile> {
202 InputFile::Ordinary(filename)
207 .unwrap_or_else(|| vec![InputFile::StdIn])
210 fn output_components(&self) -> Result<OutputComponents> {
211 let matches = &self.matches;
213 if matches.value_of("decorations") == Some("never") {
215 } else if matches.is_present("number") {
216 [OutputComponent::Numbers].iter().cloned().collect()
217 } else if matches.is_present("plain") {
218 [OutputComponent::Plain].iter().cloned().collect()
220 let env_style_components: Option<Vec<OutputComponent>> =
221 transpose(env::var("BAT_STYLE").ok().map(|style_str| {
224 .map(|x| OutputComponent::from_str(&x))
225 .collect::<Result<Vec<OutputComponent>>>()
228 values_t!(matches.values_of("style"), OutputComponent)
230 .or(env_style_components)
231 .unwrap_or(vec![OutputComponent::Full])
233 .map(|style| style.components(self.interactive_output))
234 .fold(HashSet::new(), |mut acc, components| {
235 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