2018-08-22 22:29:12 +02:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::env;
|
2018-09-09 18:28:04 -06:00
|
|
|
use std::str::FromStr;
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
use atty::{self, Stream};
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-10-03 09:39:30 +02:00
|
|
|
use clap::ArgMatches;
|
2019-03-08 10:48:22 +00:00
|
|
|
use crate::clap_app;
|
2018-10-03 22:59:11 +02:00
|
|
|
use wild;
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
use console::Term;
|
|
|
|
|
2018-05-13 21:19:26 +02:00
|
|
|
#[cfg(windows)]
|
|
|
|
use ansi_term;
|
|
|
|
|
2019-03-08 10:48:22 +00:00
|
|
|
use crate::assets::BAT_THEME_DEFAULT;
|
|
|
|
use crate::config::{get_args_from_config_file, get_args_from_env_var};
|
|
|
|
use crate::errors::*;
|
|
|
|
use crate::inputfile::InputFile;
|
|
|
|
use crate::line_range::{LineRange, LineRanges};
|
|
|
|
use crate::style::{OutputComponent, OutputComponents, OutputWrap};
|
|
|
|
use crate::syntax_mapping::SyntaxMapping;
|
|
|
|
use crate::util::transpose;
|
2018-08-22 22:29:12 +02:00
|
|
|
|
2018-09-10 20:12:13 -07:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2018-08-22 22:29:12 +02:00
|
|
|
pub enum PagingMode {
|
|
|
|
Always,
|
|
|
|
QuitIfOneScreen,
|
|
|
|
Never,
|
|
|
|
}
|
|
|
|
|
2018-08-27 13:43:22 -06:00
|
|
|
#[derive(Clone)]
|
2018-08-22 22:29:12 +02:00
|
|
|
pub struct Config<'a> {
|
2018-08-22 22:36:37 +02:00
|
|
|
/// List of files to print
|
2018-08-28 20:12:45 +02:00
|
|
|
pub files: Vec<InputFile<'a>>,
|
2018-08-22 22:36:37 +02:00
|
|
|
|
|
|
|
/// The explicitly configured language, if any
|
2018-08-22 22:29:12 +02:00
|
|
|
pub language: Option<&'a str>,
|
2018-08-22 22:36:37 +02:00
|
|
|
|
2018-11-01 13:02:29 +01:00
|
|
|
/// Whether or not to show/replace non-printable characters like space, tab and newline.
|
|
|
|
pub show_nonprintable: bool,
|
|
|
|
|
2018-08-22 22:36:37 +02:00
|
|
|
/// The character width of the terminal
|
|
|
|
pub term_width: usize,
|
|
|
|
|
2018-09-10 18:11:59 -07:00
|
|
|
/// The width of tab characters.
|
2018-09-12 10:08:58 -07:00
|
|
|
/// Currently, a value of 0 will cause tabs to be passed through without expanding them.
|
2018-09-10 18:11:59 -07:00
|
|
|
pub tab_width: usize,
|
|
|
|
|
2018-08-23 19:43:10 +02:00
|
|
|
/// Whether or not to simply loop through all input (`cat` mode)
|
|
|
|
pub loop_through: bool,
|
2018-08-22 22:36:59 +02:00
|
|
|
|
2018-08-22 22:36:37 +02:00
|
|
|
/// Whether or not the output should be colorized
|
2018-08-22 22:29:12 +02:00
|
|
|
pub colored_output: bool,
|
2018-08-22 22:36:37 +02:00
|
|
|
|
|
|
|
/// Whether or not the output terminal supports true color
|
|
|
|
pub true_color: bool,
|
|
|
|
|
|
|
|
/// Style elements (grid, line numbers, ...)
|
|
|
|
pub output_components: OutputComponents,
|
|
|
|
|
|
|
|
/// Text wrapping mode
|
|
|
|
pub output_wrap: OutputWrap,
|
|
|
|
|
|
|
|
/// Pager or STDOUT
|
2018-08-22 22:29:12 +02:00
|
|
|
pub paging_mode: PagingMode,
|
2018-08-22 22:36:37 +02:00
|
|
|
|
2018-10-20 00:10:10 +02:00
|
|
|
/// Specifies the lines that should be printed
|
|
|
|
pub line_ranges: LineRanges,
|
2018-08-22 22:36:37 +02:00
|
|
|
|
|
|
|
/// The syntax highlighting theme
|
|
|
|
pub theme: String,
|
2018-10-17 22:30:09 +02:00
|
|
|
|
|
|
|
/// File extension/name mappings
|
|
|
|
pub syntax_mapping: SyntaxMapping,
|
2018-10-21 19:52:29 +09:00
|
|
|
|
2018-10-21 22:09:40 +02:00
|
|
|
/// Command to start the pager
|
2018-10-21 19:52:29 +09:00
|
|
|
pub pager: Option<&'a str>,
|
2018-11-01 00:57:31 +05:30
|
|
|
|
2018-12-16 20:43:36 +01:00
|
|
|
/// Whether or not to use ANSI italics
|
2018-11-04 22:38:33 +05:30
|
|
|
pub use_italic_text: bool,
|
2018-12-16 20:43:36 +01:00
|
|
|
|
2018-12-16 21:00:18 +01:00
|
|
|
/// Lines to highlight
|
|
|
|
pub highlight_lines: Vec<usize>,
|
2018-08-22 22:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_truecolor_terminal() -> bool {
|
|
|
|
env::var("COLORTERM")
|
|
|
|
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
pub struct App {
|
|
|
|
pub matches: ArgMatches<'static>,
|
|
|
|
interactive_output: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
2018-10-11 22:50:37 +02:00
|
|
|
pub fn new() -> Result<Self> {
|
2018-05-13 09:37:54 +02:00
|
|
|
#[cfg(windows)]
|
2018-09-03 20:47:15 +02:00
|
|
|
let _ = ansi_term::enable_ansi_support();
|
|
|
|
|
|
|
|
let interactive_output = atty::is(Stream::Stdout);
|
2018-05-13 09:37:54 +02:00
|
|
|
|
2018-10-11 22:50:37 +02:00
|
|
|
Ok(App {
|
|
|
|
matches: Self::matches(interactive_output)?,
|
2018-05-10 23:39:13 +02:00
|
|
|
interactive_output,
|
2018-10-11 22:50:37 +02:00
|
|
|
})
|
2018-05-10 23:39:13 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 22:50:37 +02:00
|
|
|
fn matches(interactive_output: bool) -> Result<ArgMatches<'static>> {
|
2018-10-11 21:40:14 +02:00
|
|
|
let args = if wild::args_os().nth(1) == Some("cache".into())
|
2018-10-11 23:02:57 +02:00
|
|
|
|| wild::args_os().any(|arg| arg == "--no-config")
|
2018-10-11 21:40:14 +02:00
|
|
|
{
|
2018-10-07 23:22:42 +02:00
|
|
|
// Skip the arguments in bats config file
|
|
|
|
|
|
|
|
wild::args_os().collect::<Vec<_>>()
|
|
|
|
} else {
|
|
|
|
let mut cli_args = wild::args_os();
|
|
|
|
|
|
|
|
// Read arguments from bats config file
|
2018-10-11 23:01:19 +02:00
|
|
|
let mut args = get_args_from_env_var()
|
|
|
|
.unwrap_or_else(|| get_args_from_config_file())
|
|
|
|
.chain_err(|| "Could not parse configuration file")?;
|
2018-10-07 23:22:42 +02:00
|
|
|
|
|
|
|
// Put the zero-th CLI argument (program name) first
|
|
|
|
args.insert(0, cli_args.next().unwrap());
|
|
|
|
|
|
|
|
// .. and the rest at the end
|
|
|
|
cli_args.for_each(|a| args.push(a));
|
|
|
|
|
|
|
|
args
|
|
|
|
};
|
|
|
|
|
2018-10-11 22:50:37 +02:00
|
|
|
Ok(clap_app::build_app(interactive_output).get_matches_from(args))
|
2018-05-10 23:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn config(&self) -> Result<Config> {
|
|
|
|
let files = self.files();
|
2018-09-07 10:51:47 -07:00
|
|
|
let output_components = self.output_components()?;
|
2018-05-10 23:39:13 +02:00
|
|
|
|
2018-09-10 20:12:13 -07:00
|
|
|
let paging_mode = match self.matches.value_of("paging") {
|
|
|
|
Some("always") => PagingMode::Always,
|
|
|
|
Some("never") => PagingMode::Never,
|
2018-09-26 19:16:03 +02:00
|
|
|
Some("auto") | _ => {
|
|
|
|
if files.contains(&InputFile::StdIn) {
|
|
|
|
// If we are reading from stdin, only enable paging if we write to an
|
|
|
|
// interactive terminal and if we do not *read* from an interactive
|
|
|
|
// terminal.
|
|
|
|
if self.interactive_output && !atty::is(Stream::Stdin) {
|
|
|
|
PagingMode::QuitIfOneScreen
|
|
|
|
} else {
|
|
|
|
PagingMode::Never
|
|
|
|
}
|
2018-09-10 20:12:13 -07:00
|
|
|
} else {
|
2018-09-26 19:16:03 +02:00
|
|
|
if self.interactive_output {
|
|
|
|
PagingMode::QuitIfOneScreen
|
|
|
|
} else {
|
|
|
|
PagingMode::Never
|
|
|
|
}
|
2018-09-10 20:12:13 -07:00
|
|
|
}
|
2018-09-26 19:16:03 +02:00
|
|
|
}
|
2018-09-10 20:12:13 -07:00
|
|
|
};
|
|
|
|
|
2018-10-17 22:30:09 +02:00
|
|
|
let mut syntax_mapping = SyntaxMapping::new();
|
|
|
|
|
|
|
|
if let Some(values) = self.matches.values_of("map-syntax") {
|
|
|
|
for from_to in values {
|
|
|
|
let parts: Vec<_> = from_to.split(":").collect();
|
|
|
|
|
|
|
|
if parts.len() != 2 {
|
|
|
|
return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into());
|
|
|
|
}
|
|
|
|
|
2018-11-27 04:40:54 +01:00
|
|
|
syntax_mapping.insert(parts[0], parts[1]);
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:39:13 +02:00
|
|
|
Ok(Config {
|
|
|
|
true_color: is_truecolor_terminal(),
|
2018-11-01 13:02:29 +01:00
|
|
|
language: self.matches.value_of("language").or_else(|| {
|
|
|
|
if self.matches.is_present("show-all") {
|
|
|
|
Some("show-nonprintable")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
show_nonprintable: self.matches.is_present("show-all"),
|
2018-05-13 03:26:23 -07:00
|
|
|
output_wrap: if !self.interactive_output {
|
2018-05-12 13:44:10 -07:00
|
|
|
// We don't have the tty width when piping to another program.
|
|
|
|
// There's no point in wrapping when this is the case.
|
|
|
|
OutputWrap::None
|
|
|
|
} else {
|
|
|
|
match self.matches.value_of("wrap") {
|
|
|
|
Some("character") => OutputWrap::Character,
|
2018-09-07 10:51:47 -07:00
|
|
|
Some("never") => OutputWrap::None,
|
2018-09-26 19:16:03 +02:00
|
|
|
Some("auto") | _ => {
|
|
|
|
if output_components.plain() {
|
|
|
|
OutputWrap::None
|
|
|
|
} else {
|
|
|
|
OutputWrap::Character
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 13:44:10 -07:00
|
|
|
}
|
|
|
|
},
|
2018-09-03 20:47:15 +02:00
|
|
|
colored_output: match self.matches.value_of("color") {
|
|
|
|
Some("always") => true,
|
|
|
|
Some("never") => false,
|
|
|
|
Some("auto") | _ => self.interactive_output,
|
|
|
|
},
|
2018-09-10 20:12:13 -07:00
|
|
|
paging_mode,
|
2018-09-07 11:13:03 -07:00
|
|
|
term_width: self
|
|
|
|
.matches
|
2018-08-31 22:52:22 +02:00
|
|
|
.value_of("terminal-width")
|
2018-10-31 21:34:34 +01:00
|
|
|
.and_then(|w| {
|
|
|
|
if w.starts_with("+") || w.starts_with("-") {
|
|
|
|
// Treat argument as a delta to the current terminal width
|
|
|
|
w.parse().ok().map(|delta: i16| {
|
|
|
|
let old_width: u16 = Term::stdout().size().1;
|
|
|
|
let new_width: i32 = old_width as i32 + delta as i32;
|
|
|
|
|
|
|
|
if new_width <= 0 {
|
|
|
|
old_width as usize
|
|
|
|
} else {
|
|
|
|
new_width as usize
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
w.parse().ok()
|
|
|
|
}
|
|
|
|
})
|
2018-08-31 22:52:22 +02:00
|
|
|
.unwrap_or(Term::stdout().size().1 as usize),
|
2018-08-23 23:13:24 +02:00
|
|
|
loop_through: !(self.interactive_output
|
|
|
|
|| self.matches.value_of("color") == Some("always")
|
|
|
|
|| self.matches.value_of("decorations") == Some("always")),
|
2018-05-10 23:39:13 +02:00
|
|
|
files,
|
2018-09-10 18:11:59 -07:00
|
|
|
tab_width: self
|
|
|
|
.matches
|
|
|
|
.value_of("tabs")
|
2018-09-12 10:08:58 -07:00
|
|
|
.map(String::from)
|
|
|
|
.or_else(|| env::var("BAT_TABS").ok())
|
|
|
|
.and_then(|t| t.parse().ok())
|
2018-09-10 20:12:13 -07:00
|
|
|
.unwrap_or(
|
|
|
|
if output_components.plain() && paging_mode == PagingMode::Never {
|
|
|
|
0
|
|
|
|
} else {
|
2018-11-01 13:34:14 +01:00
|
|
|
4
|
2018-09-10 20:12:13 -07:00
|
|
|
},
|
|
|
|
),
|
2018-09-07 11:13:03 -07:00
|
|
|
theme: self
|
|
|
|
.matches
|
2018-07-20 23:26:24 -07:00
|
|
|
.value_of("theme")
|
2018-07-23 21:19:47 +02:00
|
|
|
.map(String::from)
|
2018-07-23 21:38:45 +02:00
|
|
|
.or_else(|| env::var("BAT_THEME").ok())
|
2018-08-20 21:39:21 +02:00
|
|
|
.unwrap_or(String::from(BAT_THEME_DEFAULT)),
|
2018-10-20 00:10:10 +02:00
|
|
|
line_ranges: LineRanges::from(
|
|
|
|
transpose(
|
|
|
|
self.matches
|
|
|
|
.values_of("line-range")
|
|
|
|
.map(|vs| vs.map(LineRange::from).collect()),
|
|
|
|
)?
|
|
|
|
.unwrap_or(vec![]),
|
|
|
|
),
|
2018-09-07 10:51:47 -07:00
|
|
|
output_components,
|
2018-10-17 22:30:09 +02:00
|
|
|
syntax_mapping,
|
2018-10-21 19:52:29 +09:00
|
|
|
pager: self.matches.value_of("pager"),
|
2018-11-04 22:38:33 +05:30
|
|
|
use_italic_text: match self.matches.value_of("italic-text") {
|
2018-11-01 00:57:31 +05:30
|
|
|
Some("always") => true,
|
2018-11-02 16:36:49 +05:30
|
|
|
_ => false,
|
2018-11-01 00:57:31 +05:30
|
|
|
},
|
2018-12-16 21:00:18 +01:00
|
|
|
highlight_lines: self
|
2018-10-10 20:45:10 +02:00
|
|
|
.matches
|
2018-12-16 21:00:18 +01:00
|
|
|
.values_of("highlight-line")
|
|
|
|
.and_then(|ws| ws.map(|w| w.parse().ok()).collect())
|
|
|
|
.unwrap_or_default(),
|
2018-05-10 23:39:13 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-28 20:12:45 +02:00
|
|
|
fn files(&self) -> Vec<InputFile> {
|
2018-05-10 23:39:13 +02:00
|
|
|
self.matches
|
|
|
|
.values_of("FILE")
|
|
|
|
.map(|values| {
|
|
|
|
values
|
|
|
|
.map(|filename| {
|
|
|
|
if filename == "-" {
|
2018-08-28 20:12:45 +02:00
|
|
|
InputFile::StdIn
|
2018-05-10 23:39:13 +02:00
|
|
|
} else {
|
2018-08-28 20:12:45 +02:00
|
|
|
InputFile::Ordinary(filename)
|
2018-05-10 23:39:13 +02:00
|
|
|
}
|
2018-09-26 19:16:03 +02:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| vec![InputFile::StdIn])
|
2018-05-10 23:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn output_components(&self) -> Result<OutputComponents> {
|
|
|
|
let matches = &self.matches;
|
2018-08-23 23:13:24 +02:00
|
|
|
Ok(OutputComponents(
|
|
|
|
if matches.value_of("decorations") == Some("never") {
|
|
|
|
HashSet::new()
|
|
|
|
} else if matches.is_present("number") {
|
|
|
|
[OutputComponent::Numbers].iter().cloned().collect()
|
2018-08-25 19:44:02 -06:00
|
|
|
} else if matches.is_present("plain") {
|
|
|
|
[OutputComponent::Plain].iter().cloned().collect()
|
2018-09-12 21:35:23 +02:00
|
|
|
} else {
|
|
|
|
let env_style_components: Option<Vec<OutputComponent>> =
|
|
|
|
transpose(env::var("BAT_STYLE").ok().map(|style_str| {
|
|
|
|
style_str
|
|
|
|
.split(",")
|
|
|
|
.map(|x| OutputComponent::from_str(&x))
|
|
|
|
.collect::<Result<Vec<OutputComponent>>>()
|
|
|
|
}))?;
|
|
|
|
|
2018-10-21 17:50:53 +02:00
|
|
|
matches
|
|
|
|
.value_of("style")
|
2018-10-21 15:27:07 +02:00
|
|
|
.map(|styles| {
|
|
|
|
styles
|
|
|
|
.split(",")
|
|
|
|
.map(|style| style.parse::<OutputComponent>())
|
|
|
|
.filter_map(|style| style.ok())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
2018-09-12 21:35:23 +02:00
|
|
|
.or(env_style_components)
|
|
|
|
.unwrap_or(vec![OutputComponent::Full])
|
2018-08-23 23:13:24 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|style| style.components(self.interactive_output))
|
|
|
|
.fold(HashSet::new(), |mut acc, components| {
|
|
|
|
acc.extend(components.iter().cloned());
|
|
|
|
acc
|
|
|
|
})
|
|
|
|
},
|
|
|
|
))
|
2018-05-10 23:39:13 +02:00
|
|
|
}
|
|
|
|
}
|