mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-06 21:32:27 +01:00
Merge branch 'master' into read-from-tail
This commit is contained in:
@@ -13,6 +13,7 @@ use crate::error::*;
|
||||
use crate::input::{InputReader, OpenedInput};
|
||||
use crate::syntax_mapping::ignored_suffixes::IgnoredSuffixes;
|
||||
use crate::syntax_mapping::MappingTarget;
|
||||
use crate::theme::{default_theme, ColorScheme};
|
||||
use crate::{bat_warning, SyntaxMapping};
|
||||
|
||||
use lazy_theme_set::LazyThemeSet;
|
||||
@@ -69,57 +70,6 @@ impl HighlightingAssets {
|
||||
}
|
||||
}
|
||||
|
||||
/// The default theme.
|
||||
///
|
||||
/// ### Windows and Linux
|
||||
///
|
||||
/// Windows and most Linux distributions has a dark terminal theme by
|
||||
/// default. On these platforms, this function always returns a theme that
|
||||
/// looks good on a dark background.
|
||||
///
|
||||
/// ### macOS
|
||||
///
|
||||
/// On macOS the default terminal background is light, but it is common that
|
||||
/// Dark Mode is active, which makes the terminal background dark. On this
|
||||
/// platform, the default theme depends on
|
||||
/// ```bash
|
||||
/// defaults read -globalDomain AppleInterfaceStyle
|
||||
/// ```
|
||||
/// To avoid the overhead of the check on macOS, simply specify a theme
|
||||
/// explicitly via `--theme`, `BAT_THEME`, or `~/.config/bat`.
|
||||
///
|
||||
/// See <https://github.com/sharkdp/bat/issues/1746> and
|
||||
/// <https://github.com/sharkdp/bat/issues/1928> for more context.
|
||||
pub fn default_theme() -> &'static str {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
Self::default_dark_theme()
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
if macos_dark_mode_active() {
|
||||
Self::default_dark_theme()
|
||||
} else {
|
||||
Self::default_light_theme()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The default theme that looks good on a dark background.
|
||||
*/
|
||||
fn default_dark_theme() -> &'static str {
|
||||
"Monokai Extended"
|
||||
}
|
||||
|
||||
/**
|
||||
* The default theme that looks good on a light background.
|
||||
*/
|
||||
#[cfg(target_os = "macos")]
|
||||
fn default_light_theme() -> &'static str {
|
||||
"Monokai Extended Light"
|
||||
}
|
||||
|
||||
pub fn from_cache(cache_path: &Path) -> Result<Self> {
|
||||
Ok(HighlightingAssets::new(
|
||||
SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
|
||||
@@ -248,7 +198,10 @@ impl HighlightingAssets {
|
||||
bat_warning!("Unknown theme '{}', using default.", theme)
|
||||
}
|
||||
self.get_theme_set()
|
||||
.get(self.fallback_theme.unwrap_or_else(Self::default_theme))
|
||||
.get(
|
||||
self.fallback_theme
|
||||
.unwrap_or_else(|| default_theme(ColorScheme::Dark)),
|
||||
)
|
||||
.expect("something is very wrong if the default theme is missing")
|
||||
}
|
||||
}
|
||||
@@ -399,26 +352,6 @@ fn asset_from_cache<T: serde::de::DeserializeOwned>(
|
||||
.map_err(|_| format!("Could not parse cached {description}").into())
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn macos_dark_mode_active() -> bool {
|
||||
const PREFERENCES_FILE: &str = "Library/Preferences/.GlobalPreferences.plist";
|
||||
const STYLE_KEY: &str = "AppleInterfaceStyle";
|
||||
|
||||
let preferences_file = home::home_dir()
|
||||
.map(|home| home.join(PREFERENCES_FILE))
|
||||
.expect("Could not get home directory");
|
||||
|
||||
match plist::Value::from_file(preferences_file).map(|file| file.into_dictionary()) {
|
||||
Ok(Some(preferences)) => match preferences.get(STYLE_KEY).and_then(|val| val.as_string()) {
|
||||
Some(value) => value == "Dark",
|
||||
// If the key does not exist, then light theme is currently in use.
|
||||
None => false,
|
||||
},
|
||||
// Unreachable, in theory. All macOS users have a home directory and preferences file setup.
|
||||
Ok(None) | Err(_) => true,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -437,7 +370,7 @@ mod tests {
|
||||
pub temp_dir: TempDir,
|
||||
}
|
||||
|
||||
impl<'a> SyntaxDetectionTest<'a> {
|
||||
impl SyntaxDetectionTest<'_> {
|
||||
fn new() -> Self {
|
||||
SyntaxDetectionTest {
|
||||
assets: HighlightingAssets::from_binary(),
|
||||
|
@@ -60,7 +60,7 @@ fn to_path_and_stem(source_dir: &Path, entry: DirEntry) -> Option<PathAndStem> {
|
||||
fn handle_file(path_and_stem: &PathAndStem) -> Result<Option<String>> {
|
||||
if path_and_stem.stem == "NOTICE" {
|
||||
handle_notice(&path_and_stem.path)
|
||||
} else if path_and_stem.stem.to_ascii_uppercase() == "LICENSE" {
|
||||
} else if path_and_stem.stem.eq_ignore_ascii_case("LICENSE") {
|
||||
handle_license(&path_and_stem.path)
|
||||
} else {
|
||||
Ok(None)
|
||||
|
@@ -9,6 +9,8 @@ use crate::{
|
||||
config::{get_args_from_config_file, get_args_from_env_opts_var, get_args_from_env_vars},
|
||||
};
|
||||
use bat::style::StyleComponentList;
|
||||
use bat::theme::{theme, ThemeName, ThemeOptions, ThemePreference};
|
||||
use bat::BinaryBehavior;
|
||||
use bat::StripAnsiMode;
|
||||
use clap::ArgMatches;
|
||||
|
||||
@@ -16,7 +18,6 @@ use console::Term;
|
||||
|
||||
use crate::input::{new_file_input, new_stdin_input};
|
||||
use bat::{
|
||||
assets::HighlightingAssets,
|
||||
bat_warning,
|
||||
config::{Config, VisibleLines},
|
||||
error::*,
|
||||
@@ -97,15 +98,36 @@ impl App {
|
||||
pub fn config(&self, inputs: &[Input]) -> Result<Config> {
|
||||
let style_components = self.style_components()?;
|
||||
|
||||
let extra_plain = self.matches.get_count("plain") > 1;
|
||||
let plain_last_index = self
|
||||
.matches
|
||||
.indices_of("plain")
|
||||
.and_then(Iterator::max)
|
||||
.unwrap_or_default();
|
||||
let paging_last_index = self
|
||||
.matches
|
||||
.indices_of("paging")
|
||||
.and_then(Iterator::max)
|
||||
.unwrap_or_default();
|
||||
|
||||
let paging_mode = match self.matches.get_one::<String>("paging").map(|s| s.as_str()) {
|
||||
Some("always") => PagingMode::Always,
|
||||
Some("always") => {
|
||||
// Disable paging if the second -p (or -pp) is specified after --paging=always
|
||||
if extra_plain && plain_last_index > paging_last_index {
|
||||
PagingMode::Never
|
||||
} else {
|
||||
PagingMode::Always
|
||||
}
|
||||
}
|
||||
Some("never") => PagingMode::Never,
|
||||
Some("auto") | None => {
|
||||
// If we have -pp as an option when in auto mode, the pager should be disabled.
|
||||
let extra_plain = self.matches.get_count("plain") > 1;
|
||||
if extra_plain || self.matches.get_flag("no-paging") {
|
||||
PagingMode::Never
|
||||
} else if inputs.iter().any(Input::is_stdin) {
|
||||
} else if inputs.iter().any(Input::is_stdin)
|
||||
// ignore stdin when --list-themes is used because in that case no input will be read anyways
|
||||
&& !self.matches.get_flag("list-themes")
|
||||
{
|
||||
// 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.
|
||||
@@ -193,6 +215,11 @@ impl App {
|
||||
Some("caret") => NonprintableNotation::Caret,
|
||||
_ => unreachable!("other values for --nonprintable-notation are not allowed"),
|
||||
},
|
||||
binary: match self.matches.get_one::<String>("binary").map(|s| s.as_str()) {
|
||||
Some("as-text") => BinaryBehavior::AsText,
|
||||
Some("no-printing") => BinaryBehavior::NoPrinting,
|
||||
_ => unreachable!("other values for --binary are not allowed"),
|
||||
},
|
||||
wrapping_mode: if self.interactive_output || maybe_term_width.is_some() {
|
||||
if !self.matches.get_flag("chop-long-lines") {
|
||||
match self.matches.get_one::<String>("wrap").map(|s| s.as_str()) {
|
||||
@@ -254,18 +281,7 @@ impl App {
|
||||
Some("auto") => StripAnsiMode::Auto,
|
||||
_ => unreachable!("other values for --strip-ansi are not allowed"),
|
||||
},
|
||||
theme: self
|
||||
.matches
|
||||
.get_one::<String>("theme")
|
||||
.map(String::from)
|
||||
.map(|s| {
|
||||
if s == "default" {
|
||||
String::from(HighlightingAssets::default_theme())
|
||||
} else {
|
||||
s
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| String::from(HighlightingAssets::default_theme())),
|
||||
theme: theme(self.theme_options()).to_string(),
|
||||
visible_lines: match self.matches.try_contains_id("diff").unwrap_or_default()
|
||||
&& self.matches.get_flag("diff")
|
||||
{
|
||||
@@ -412,7 +428,7 @@ impl App {
|
||||
None => StyleComponents(HashSet::from_iter(
|
||||
StyleComponent::Default
|
||||
.components(self.interactive_output)
|
||||
.into_iter()
|
||||
.iter()
|
||||
.cloned(),
|
||||
)),
|
||||
};
|
||||
@@ -424,4 +440,25 @@ impl App {
|
||||
|
||||
Ok(styled_components)
|
||||
}
|
||||
|
||||
fn theme_options(&self) -> ThemeOptions {
|
||||
let theme = self
|
||||
.matches
|
||||
.get_one::<String>("theme")
|
||||
.map(|t| ThemePreference::from_str(t).unwrap())
|
||||
.unwrap_or_default();
|
||||
let theme_dark = self
|
||||
.matches
|
||||
.get_one::<String>("theme-dark")
|
||||
.map(|t| ThemeName::from_str(t).unwrap());
|
||||
let theme_light = self
|
||||
.matches
|
||||
.get_one::<String>("theme-light")
|
||||
.map(|t| ThemeName::from_str(t).unwrap());
|
||||
ThemeOptions {
|
||||
theme,
|
||||
theme_dark,
|
||||
theme_light,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -77,11 +77,26 @@ pub fn build_app(interactive_output: bool) -> Command {
|
||||
* caret (^G, ^J, ^@, ..)",
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("binary")
|
||||
.long("binary")
|
||||
.action(ArgAction::Set)
|
||||
.default_value("no-printing")
|
||||
.value_parser(["no-printing", "as-text"])
|
||||
.value_name("behavior")
|
||||
.hide_default_value(true)
|
||||
.help("How to treat binary content. (default: no-printing)")
|
||||
.long_help(
|
||||
"How to treat binary content. (default: no-printing)\n\n\
|
||||
Possible values:\n \
|
||||
* no-printing: do not print any binary content\n \
|
||||
* as-text: treat binary content as normal text",
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("plain")
|
||||
.overrides_with("plain")
|
||||
.overrides_with("number")
|
||||
.overrides_with("paging")
|
||||
.short('p')
|
||||
.long("plain")
|
||||
.action(ArgAction::Count)
|
||||
@@ -306,7 +321,6 @@ pub fn build_app(interactive_output: bool) -> Command {
|
||||
.long("paging")
|
||||
.overrides_with("paging")
|
||||
.overrides_with("no-paging")
|
||||
.overrides_with("plain")
|
||||
.value_name("when")
|
||||
.value_parser(["auto", "never", "always"])
|
||||
.default_value("auto")
|
||||
@@ -379,9 +393,40 @@ pub fn build_app(interactive_output: bool) -> Command {
|
||||
see all available themes. To set a default theme, add the \
|
||||
'--theme=\"...\"' option to the configuration file or export the \
|
||||
BAT_THEME environment variable (e.g.: export \
|
||||
BAT_THEME=\"...\").",
|
||||
BAT_THEME=\"...\").\n\n\
|
||||
Special values:\n\n \
|
||||
* auto: Picks a dark or light theme depending on the terminal's colors (default).\n \
|
||||
Use '--theme-light' and '--theme-dark' to customize the selected theme.\n \
|
||||
* auto:always: Detect the terminal's colors even when the output is redirected.\n \
|
||||
* auto:system: Detect the color scheme from the system-wide preference (macOS only).\n \
|
||||
* dark: Use the dark theme specified by '--theme-dark'.\n \
|
||||
* light: Use the light theme specified by '--theme-light'.",
|
||||
),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("theme-light")
|
||||
.long("theme-light")
|
||||
.overrides_with("theme-light")
|
||||
.value_name("theme")
|
||||
.help("Sets the color theme for syntax highlighting used for light backgrounds.")
|
||||
.long_help(
|
||||
"Sets the theme name for syntax highlighting used when the terminal uses a light background. \
|
||||
Use '--list-themes' to see all available themes. To set a default theme, add the \
|
||||
'--theme-light=\"...\" option to the configuration file or export the BAT_THEME_LIGHT \
|
||||
environment variable (e.g. export BAT_THEME_LIGHT=\"...\")."),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("theme-dark")
|
||||
.long("theme-dark")
|
||||
.overrides_with("theme-dark")
|
||||
.value_name("theme")
|
||||
.help("Sets the color theme for syntax highlighting used for dark backgrounds.")
|
||||
.long_help(
|
||||
"Sets the theme name for syntax highlighting used when the terminal uses a dark background. \
|
||||
Use '--list-themes' to see all available themes. To set a default theme, add the \
|
||||
'--theme-dark=\"...\" option to the configuration file or export the BAT_THEME_DARK \
|
||||
environment variable (e.g. export BAT_THEME_DARK=\"...\")."),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("list-themes")
|
||||
.long("list-themes")
|
||||
@@ -519,6 +564,17 @@ pub fn build_app(interactive_output: bool) -> Command {
|
||||
.help("Do not load custom assets"),
|
||||
);
|
||||
|
||||
#[cfg(feature = "application")]
|
||||
{
|
||||
app = app.arg(
|
||||
Arg::new("completion")
|
||||
.long("completion")
|
||||
.value_name("SHELL")
|
||||
.value_parser(["bash", "fish", "ps1", "zsh"])
|
||||
.help("Show shell completion for a certain shell. [possible values: bash, fish, zsh, ps1]"),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "lessopen")]
|
||||
{
|
||||
app = app
|
||||
|
6
src/bin/bat/completions.rs
Normal file
6
src/bin/bat/completions.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use std::env;
|
||||
|
||||
pub const BASH_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_BASH"));
|
||||
pub const FISH_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_FISH"));
|
||||
pub const PS1_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_PS1"));
|
||||
pub const ZSH_COMPLETION: &str = include_str!(env!("BAT_GENERATED_COMPLETION_ZSH"));
|
@@ -140,7 +140,9 @@ fn get_args_from_str(content: &str) -> Result<Vec<OsString>, shell_words::ParseE
|
||||
pub fn get_args_from_env_vars() -> Vec<OsString> {
|
||||
[
|
||||
("--tabs", "BAT_TABS"),
|
||||
("--theme", "BAT_THEME"),
|
||||
("--theme", bat::theme::env::BAT_THEME),
|
||||
("--theme-dark", bat::theme::env::BAT_THEME_DARK),
|
||||
("--theme-light", bat::theme::env::BAT_THEME_LIGHT),
|
||||
("--pager", "BAT_PAGER"),
|
||||
("--paging", "BAT_PAGING"),
|
||||
("--style", "BAT_STYLE"),
|
||||
|
@@ -3,6 +3,8 @@
|
||||
mod app;
|
||||
mod assets;
|
||||
mod clap_app;
|
||||
#[cfg(feature = "application")]
|
||||
mod completions;
|
||||
mod config;
|
||||
mod directories;
|
||||
mod input;
|
||||
@@ -14,6 +16,8 @@ use std::io::{BufReader, Write};
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
|
||||
use bat::output::{OutputHandle, OutputType};
|
||||
use bat::theme::DetectColorScheme;
|
||||
use nu_ansi_term::Color::Green;
|
||||
use nu_ansi_term::Style;
|
||||
|
||||
@@ -30,12 +34,12 @@ use directories::PROJECT_DIRS;
|
||||
use globset::GlobMatcher;
|
||||
|
||||
use bat::{
|
||||
assets::HighlightingAssets,
|
||||
config::Config,
|
||||
controller::Controller,
|
||||
error::*,
|
||||
input::Input,
|
||||
style::{StyleComponent, StyleComponents},
|
||||
theme::{color_scheme, default_theme, ColorScheme},
|
||||
MappingTarget, PagingMode,
|
||||
};
|
||||
|
||||
@@ -189,7 +193,12 @@ fn theme_preview_file<'a>() -> Input<'a> {
|
||||
Input::from_reader(Box::new(BufReader::new(THEME_PREVIEW_DATA)))
|
||||
}
|
||||
|
||||
pub fn list_themes(cfg: &Config, config_dir: &Path, cache_dir: &Path) -> Result<()> {
|
||||
pub fn list_themes(
|
||||
cfg: &Config,
|
||||
config_dir: &Path,
|
||||
cache_dir: &Path,
|
||||
detect_color_scheme: DetectColorScheme,
|
||||
) -> Result<()> {
|
||||
let assets = assets_from_cache_or_binary(cfg.use_custom_assets, cache_dir)?;
|
||||
let mut config = cfg.clone();
|
||||
let mut style = HashSet::new();
|
||||
@@ -197,36 +206,46 @@ pub fn list_themes(cfg: &Config, config_dir: &Path, cache_dir: &Path) -> Result<
|
||||
config.language = Some("Rust");
|
||||
config.style_components = StyleComponents(style);
|
||||
|
||||
let stdout = io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
let mut output_type =
|
||||
OutputType::from_mode(config.paging_mode, config.wrapping_mode, config.pager)?;
|
||||
let mut writer = output_type.handle()?;
|
||||
|
||||
let default_theme = HighlightingAssets::default_theme();
|
||||
let default_theme_name = default_theme(color_scheme(detect_color_scheme).unwrap_or_default());
|
||||
for theme in assets.themes() {
|
||||
let default_theme_info = if default_theme == theme {
|
||||
let default_theme_info = if default_theme_name == theme {
|
||||
" (default)"
|
||||
} else if default_theme(ColorScheme::Dark) == theme {
|
||||
" (default dark)"
|
||||
} else if default_theme(ColorScheme::Light) == theme {
|
||||
" (default light)"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
if config.colored_output {
|
||||
writeln!(
|
||||
stdout,
|
||||
writer,
|
||||
"Theme: {}{}\n",
|
||||
Style::new().bold().paint(theme.to_string()),
|
||||
default_theme_info
|
||||
)?;
|
||||
config.theme = theme.to_string();
|
||||
Controller::new(&config, &assets)
|
||||
.run(vec![theme_preview_file()], None)
|
||||
.run(
|
||||
vec![theme_preview_file()],
|
||||
Some(OutputHandle::IoWrite(&mut writer)),
|
||||
)
|
||||
.ok();
|
||||
writeln!(stdout)?;
|
||||
writeln!(writer)?;
|
||||
} else if config.loop_through {
|
||||
writeln!(writer, "{theme}")?;
|
||||
} else {
|
||||
writeln!(stdout, "{theme}{default_theme_info}")?;
|
||||
writeln!(writer, "{theme}{default_theme_info}")?;
|
||||
}
|
||||
}
|
||||
|
||||
if config.colored_output {
|
||||
writeln!(
|
||||
stdout,
|
||||
writer,
|
||||
"Further themes can be installed to '{}', \
|
||||
and are added to the cache with `bat cache --build`. \
|
||||
For more information, see:\n\n \
|
||||
@@ -337,6 +356,18 @@ fn run() -> Result<bool> {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
#[cfg(feature = "application")]
|
||||
if let Some(shell) = app.matches.get_one::<String>("completion") {
|
||||
match shell.as_str() {
|
||||
"bash" => println!("{}", completions::BASH_COMPLETION),
|
||||
"fish" => println!("{}", completions::FISH_COMPLETION),
|
||||
"ps1" => println!("{}", completions::PS1_COMPLETION),
|
||||
"zsh" => println!("{}", completions::ZSH_COMPLETION),
|
||||
_ => unreachable!("No completion for shell '{}' available.", shell),
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
match app.matches.subcommand() {
|
||||
Some(("cache", cache_matches)) => {
|
||||
// If there is a file named 'cache' in the current working directory,
|
||||
@@ -371,7 +402,7 @@ fn run() -> Result<bool> {
|
||||
};
|
||||
run_controller(inputs, &plain_config, cache_dir)
|
||||
} else if app.matches.get_flag("list-themes") {
|
||||
list_themes(&config, config_dir, cache_dir)?;
|
||||
list_themes(&config, config_dir, cache_dir, DetectColorScheme::default())?;
|
||||
Ok(true)
|
||||
} else if app.matches.get_flag("config-file") {
|
||||
println!("{}", config_file().to_string_lossy());
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use crate::line_range::{HighlightedLineRanges, LineRanges};
|
||||
use crate::nonprintable_notation::NonprintableNotation;
|
||||
use crate::nonprintable_notation::{BinaryBehavior, NonprintableNotation};
|
||||
#[cfg(feature = "paging")]
|
||||
use crate::paging::PagingMode;
|
||||
use crate::style::StyleComponents;
|
||||
@@ -44,6 +44,9 @@ pub struct Config<'a> {
|
||||
/// The configured notation for non-printable characters
|
||||
pub nonprintable_notation: NonprintableNotation,
|
||||
|
||||
/// How to treat binary content
|
||||
pub binary: BinaryBehavior,
|
||||
|
||||
/// The character width of the terminal
|
||||
pub term_width: usize,
|
||||
|
||||
|
@@ -9,10 +9,10 @@ use crate::lessopen::LessOpenPreprocessor;
|
||||
#[cfg(feature = "git")]
|
||||
use crate::line_range::LineRange;
|
||||
use crate::line_range::{LineRanges, MaxBufferedLineNumber, RangeCheckResult};
|
||||
use crate::output::OutputType;
|
||||
use crate::output::{OutputHandle, OutputType};
|
||||
#[cfg(feature = "paging")]
|
||||
use crate::paging::PagingMode;
|
||||
use crate::printer::{InteractivePrinter, OutputHandle, Printer, SimplePrinter};
|
||||
use crate::printer::{InteractivePrinter, Printer, SimplePrinter};
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{self, BufRead, Write};
|
||||
use std::mem;
|
||||
@@ -26,7 +26,7 @@ pub struct Controller<'a> {
|
||||
preprocessor: Option<LessOpenPreprocessor>,
|
||||
}
|
||||
|
||||
impl<'b> Controller<'b> {
|
||||
impl Controller<'_> {
|
||||
pub fn new<'a>(config: &'a Config, assets: &'a HighlightingAssets) -> Controller<'a> {
|
||||
Controller {
|
||||
config,
|
||||
@@ -36,18 +36,14 @@ impl<'b> Controller<'b> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
&self,
|
||||
inputs: Vec<Input>,
|
||||
output_buffer: Option<&mut dyn std::fmt::Write>,
|
||||
) -> Result<bool> {
|
||||
self.run_with_error_handler(inputs, output_buffer, default_error_handler)
|
||||
pub fn run(&self, inputs: Vec<Input>, output_handle: Option<OutputHandle<'_>>) -> Result<bool> {
|
||||
self.run_with_error_handler(inputs, output_handle, default_error_handler)
|
||||
}
|
||||
|
||||
pub fn run_with_error_handler(
|
||||
&self,
|
||||
inputs: Vec<Input>,
|
||||
output_buffer: Option<&mut dyn std::fmt::Write>,
|
||||
output_handle: Option<OutputHandle<'_>>,
|
||||
mut handle_error: impl FnMut(&Error, &mut dyn Write),
|
||||
) -> Result<bool> {
|
||||
let mut output_type;
|
||||
@@ -89,8 +85,9 @@ impl<'b> Controller<'b> {
|
||||
clircle::Identifier::stdout()
|
||||
};
|
||||
|
||||
let mut writer = match output_buffer {
|
||||
Some(buf) => OutputHandle::FmtWrite(buf),
|
||||
let mut writer = match output_handle {
|
||||
Some(OutputHandle::FmtWrite(w)) => OutputHandle::FmtWrite(w),
|
||||
Some(OutputHandle::IoWrite(w)) => OutputHandle::IoWrite(w),
|
||||
None => OutputHandle::IoWrite(output_type.handle()?),
|
||||
};
|
||||
let mut no_errors: bool = true;
|
||||
|
@@ -75,7 +75,7 @@ pub(crate) enum InputKind<'a> {
|
||||
CustomReader(Box<dyn Read + 'a>),
|
||||
}
|
||||
|
||||
impl<'a> InputKind<'a> {
|
||||
impl InputKind<'_> {
|
||||
pub fn description(&self) -> InputDescription {
|
||||
match self {
|
||||
InputKind::OrdinaryFile(ref path) => InputDescription::new(path.to_string_lossy()),
|
||||
|
152
src/lessopen.rs
152
src/lessopen.rs
@@ -1,15 +1,12 @@
|
||||
#![cfg(feature = "lessopen")]
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Cursor, Read, Write};
|
||||
use std::io::{BufRead, BufReader, Cursor, Read};
|
||||
use std::path::PathBuf;
|
||||
use std::str;
|
||||
use std::process::{ExitStatus, Stdio};
|
||||
|
||||
use clircle::{Clircle, Identifier};
|
||||
use os_str_bytes::RawOsString;
|
||||
use run_script::{IoOptions, ScriptOptions};
|
||||
use execute::{shell, Execute};
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::{
|
||||
@@ -21,7 +18,6 @@ use crate::{
|
||||
pub(crate) struct LessOpenPreprocessor {
|
||||
lessopen: String,
|
||||
lessclose: Option<String>,
|
||||
command_options: ScriptOptions,
|
||||
kind: LessOpenKind,
|
||||
/// Whether or not data piped via stdin is to be preprocessed
|
||||
preprocess_stdin: bool,
|
||||
@@ -52,7 +48,7 @@ impl LessOpenPreprocessor {
|
||||
// Otherwise, if output is empty and exit code is nonzero, use original file contents
|
||||
let (kind, lessopen) = if lessopen.starts_with("||") {
|
||||
(LessOpenKind::Piped, lessopen.chars().skip(2).collect())
|
||||
// "|" means pipe, but ignore exit code, always using preprocessor output
|
||||
// "|" means pipe as above, but ignore exit code and always use preprocessor output even if empty
|
||||
} else if lessopen.starts_with('|') {
|
||||
(
|
||||
LessOpenKind::PipedIgnoreExitCode,
|
||||
@@ -70,16 +66,9 @@ impl LessOpenPreprocessor {
|
||||
(false, lessopen)
|
||||
};
|
||||
|
||||
let mut command_options = ScriptOptions::new();
|
||||
command_options.runner = env::var("SHELL").ok();
|
||||
command_options.input_redirection = IoOptions::Pipe;
|
||||
|
||||
Ok(Self {
|
||||
lessopen: lessopen.replacen("%s", "$1", 1),
|
||||
lessclose: env::var("LESSCLOSE")
|
||||
.ok()
|
||||
.map(|str| str.replacen("%s", "$1", 1).replacen("%s", "$2", 1)),
|
||||
command_options,
|
||||
lessopen,
|
||||
lessclose: env::var("LESSCLOSE").ok(),
|
||||
kind,
|
||||
preprocess_stdin: stdin,
|
||||
})
|
||||
@@ -98,21 +87,21 @@ impl LessOpenPreprocessor {
|
||||
None => return input.open(stdin, stdout_identifier),
|
||||
};
|
||||
|
||||
let (exit_code, lessopen_stdout, _) = match run_script::run(
|
||||
&self.lessopen,
|
||||
&vec![path_str.to_string()],
|
||||
&self.command_options,
|
||||
) {
|
||||
let mut lessopen_command = shell(self.lessopen.replacen("%s", path_str, 1));
|
||||
lessopen_command.stdout(Stdio::piped());
|
||||
|
||||
let lessopen_output = match lessopen_command.execute_output() {
|
||||
Ok(output) => output,
|
||||
Err(_) => return input.open(stdin, stdout_identifier),
|
||||
};
|
||||
|
||||
if self.fall_back_to_original_file(&lessopen_stdout, exit_code) {
|
||||
if self.fall_back_to_original_file(&lessopen_output.stdout, lessopen_output.status)
|
||||
{
|
||||
return input.open(stdin, stdout_identifier);
|
||||
}
|
||||
|
||||
(
|
||||
RawOsString::from_string(lessopen_stdout),
|
||||
lessopen_output.stdout,
|
||||
path_str.to_string(),
|
||||
OpenedInputKind::OrdinaryFile(path.to_path_buf()),
|
||||
)
|
||||
@@ -127,47 +116,31 @@ impl LessOpenPreprocessor {
|
||||
}
|
||||
}
|
||||
|
||||
// stdin isn't Clone, so copy it to a cloneable buffer
|
||||
// stdin isn't Clone or AsRef<[u8]>, so move it into a cloneable buffer
|
||||
// so the data can be used multiple times if necessary
|
||||
// NOTE: stdin will be empty from this point onwards
|
||||
let mut stdin_buffer = Vec::new();
|
||||
stdin.read_to_end(&mut stdin_buffer).unwrap();
|
||||
stdin.read_to_end(&mut stdin_buffer)?;
|
||||
|
||||
let mut lessopen_handle = match run_script::spawn(
|
||||
&self.lessopen,
|
||||
&vec!["-".to_string()],
|
||||
&self.command_options,
|
||||
) {
|
||||
Ok(handle) => handle,
|
||||
Err(_) => {
|
||||
return input.open(stdin, stdout_identifier);
|
||||
}
|
||||
};
|
||||
let mut lessopen_command = shell(self.lessopen.replacen("%s", "-", 1));
|
||||
lessopen_command.stdout(Stdio::piped());
|
||||
|
||||
if lessopen_handle
|
||||
.stdin
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.write_all(&stdin_buffer.clone())
|
||||
.is_err()
|
||||
let lessopen_output = match lessopen_command.execute_input_output(&stdin_buffer)
|
||||
{
|
||||
return input.open(stdin, stdout_identifier);
|
||||
}
|
||||
|
||||
let lessopen_output = match lessopen_handle.wait_with_output() {
|
||||
Ok(output) => output,
|
||||
Err(_) => {
|
||||
return input.open(Cursor::new(stdin_buffer), stdout_identifier);
|
||||
}
|
||||
};
|
||||
|
||||
if lessopen_output.stdout.is_empty()
|
||||
&& (!lessopen_output.status.success()
|
||||
|| matches!(self.kind, LessOpenKind::PipedIgnoreExitCode))
|
||||
if self
|
||||
.fall_back_to_original_file(&lessopen_output.stdout, lessopen_output.status)
|
||||
{
|
||||
return input.open(Cursor::new(stdin_buffer), stdout_identifier);
|
||||
}
|
||||
|
||||
(
|
||||
RawOsString::assert_from_raw_vec(lessopen_output.stdout),
|
||||
lessopen_output.stdout,
|
||||
"-".to_string(),
|
||||
OpenedInputKind::StdIn,
|
||||
)
|
||||
@@ -184,13 +157,17 @@ impl LessOpenPreprocessor {
|
||||
kind,
|
||||
reader: InputReader::new(BufReader::new(
|
||||
if matches!(self.kind, LessOpenKind::TempFile) {
|
||||
// Remove newline at end of temporary file path returned by $LESSOPEN
|
||||
let stdout = match lessopen_stdout.strip_suffix("\n") {
|
||||
Some(stripped) => stripped.to_owned(),
|
||||
None => lessopen_stdout,
|
||||
let lessopen_string = match String::from_utf8(lessopen_stdout) {
|
||||
Ok(string) => string,
|
||||
Err(_) => {
|
||||
return input.open(stdin, stdout_identifier);
|
||||
}
|
||||
};
|
||||
// Remove newline at end of temporary file path returned by $LESSOPEN
|
||||
let stdout = match lessopen_string.strip_suffix("\n") {
|
||||
Some(stripped) => stripped.to_owned(),
|
||||
None => lessopen_string,
|
||||
};
|
||||
|
||||
let stdout = stdout.into_os_string();
|
||||
|
||||
let file = match File::open(PathBuf::from(&stdout)) {
|
||||
Ok(file) => file,
|
||||
@@ -201,16 +178,18 @@ impl LessOpenPreprocessor {
|
||||
|
||||
Preprocessed {
|
||||
kind: PreprocessedKind::TempFile(file),
|
||||
lessclose: self.lessclose.clone(),
|
||||
command_args: vec![path_str, stdout.to_str().unwrap().to_string()],
|
||||
command_options: self.command_options.clone(),
|
||||
lessclose: self
|
||||
.lessclose
|
||||
.as_ref()
|
||||
.map(|s| s.replacen("%s", &path_str, 1).replacen("%s", &stdout, 1)),
|
||||
}
|
||||
} else {
|
||||
Preprocessed {
|
||||
kind: PreprocessedKind::Piped(Cursor::new(lessopen_stdout.into_raw_vec())),
|
||||
lessclose: self.lessclose.clone(),
|
||||
command_args: vec![path_str, "-".to_string()],
|
||||
command_options: self.command_options.clone(),
|
||||
kind: PreprocessedKind::Piped(Cursor::new(lessopen_stdout)),
|
||||
lessclose: self
|
||||
.lessclose
|
||||
.as_ref()
|
||||
.map(|s| s.replacen("%s", &path_str, 1).replacen("%s", "-", 1)),
|
||||
}
|
||||
},
|
||||
)),
|
||||
@@ -219,9 +198,9 @@ impl LessOpenPreprocessor {
|
||||
})
|
||||
}
|
||||
|
||||
fn fall_back_to_original_file(&self, lessopen_output: &str, exit_code: i32) -> bool {
|
||||
lessopen_output.is_empty()
|
||||
&& (exit_code != 0 || matches!(self.kind, LessOpenKind::PipedIgnoreExitCode))
|
||||
fn fall_back_to_original_file(&self, lessopen_stdout: &[u8], exit_code: ExitStatus) -> bool {
|
||||
lessopen_stdout.is_empty()
|
||||
&& (!exit_code.success() || matches!(self.kind, LessOpenKind::PipedIgnoreExitCode))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -261,8 +240,6 @@ impl Read for PreprocessedKind {
|
||||
pub struct Preprocessed {
|
||||
kind: PreprocessedKind,
|
||||
lessclose: Option<String>,
|
||||
command_args: Vec<String>,
|
||||
command_options: ScriptOptions,
|
||||
}
|
||||
|
||||
impl Read for Preprocessed {
|
||||
@@ -273,11 +250,20 @@ impl Read for Preprocessed {
|
||||
|
||||
impl Drop for Preprocessed {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ref command) = self.lessclose {
|
||||
self.command_options.output_redirection = IoOptions::Inherit;
|
||||
if let Some(lessclose) = self.lessclose.clone() {
|
||||
let mut lessclose_command = shell(lessclose);
|
||||
|
||||
run_script::run(command, &self.command_args, &self.command_options)
|
||||
.expect("failed to run $LESSCLOSE to clean up file");
|
||||
let lessclose_output = match lessclose_command.execute_output() {
|
||||
Ok(output) => output,
|
||||
Err(_) => {
|
||||
bat_warning!("failed to run $LESSCLOSE to clean up temporary file");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if lessclose_output.status.success() {
|
||||
bat_warning!("$LESSCLOSE exited with nonzero exit code",)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -301,7 +287,7 @@ mod tests {
|
||||
fn test_just_lessopen() -> Result<()> {
|
||||
let preprocessor = LessOpenPreprocessor::mock_new(Some("|batpipe %s"), None)?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "batpipe $1");
|
||||
assert_eq!(preprocessor.lessopen, "batpipe %s");
|
||||
assert!(preprocessor.lessclose.is_none());
|
||||
|
||||
reset_env_vars();
|
||||
@@ -327,8 +313,8 @@ mod tests {
|
||||
let preprocessor =
|
||||
LessOpenPreprocessor::mock_new(Some("lessopen.sh %s"), Some("lessclose.sh %s %s"))?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "lessopen.sh $1");
|
||||
assert_eq!(preprocessor.lessclose.unwrap(), "lessclose.sh $1 $2");
|
||||
assert_eq!(preprocessor.lessopen, "lessopen.sh %s");
|
||||
assert_eq!(preprocessor.lessclose.unwrap(), "lessclose.sh %s %s");
|
||||
|
||||
reset_env_vars();
|
||||
|
||||
@@ -340,13 +326,13 @@ mod tests {
|
||||
fn test_lessopen_prefixes() -> Result<()> {
|
||||
let preprocessor = LessOpenPreprocessor::mock_new(Some("batpipe %s"), None)?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "batpipe $1");
|
||||
assert_eq!(preprocessor.lessopen, "batpipe %s");
|
||||
assert!(matches!(preprocessor.kind, LessOpenKind::TempFile));
|
||||
assert!(!preprocessor.preprocess_stdin);
|
||||
|
||||
let preprocessor = LessOpenPreprocessor::mock_new(Some("|batpipe %s"), None)?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "batpipe $1");
|
||||
assert_eq!(preprocessor.lessopen, "batpipe %s");
|
||||
assert!(matches!(
|
||||
preprocessor.kind,
|
||||
LessOpenKind::PipedIgnoreExitCode
|
||||
@@ -355,19 +341,19 @@ mod tests {
|
||||
|
||||
let preprocessor = LessOpenPreprocessor::mock_new(Some("||batpipe %s"), None)?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "batpipe $1");
|
||||
assert_eq!(preprocessor.lessopen, "batpipe %s");
|
||||
assert!(matches!(preprocessor.kind, LessOpenKind::Piped));
|
||||
assert!(!preprocessor.preprocess_stdin);
|
||||
|
||||
let preprocessor = LessOpenPreprocessor::mock_new(Some("-batpipe %s"), None)?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "batpipe $1");
|
||||
assert_eq!(preprocessor.lessopen, "batpipe %s");
|
||||
assert!(matches!(preprocessor.kind, LessOpenKind::TempFile));
|
||||
assert!(preprocessor.preprocess_stdin);
|
||||
|
||||
let preprocessor = LessOpenPreprocessor::mock_new(Some("|-batpipe %s"), None)?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "batpipe $1");
|
||||
assert_eq!(preprocessor.lessopen, "batpipe %s");
|
||||
assert!(matches!(
|
||||
preprocessor.kind,
|
||||
LessOpenKind::PipedIgnoreExitCode
|
||||
@@ -376,7 +362,7 @@ mod tests {
|
||||
|
||||
let preprocessor = LessOpenPreprocessor::mock_new(Some("||-batpipe %s"), None)?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "batpipe $1");
|
||||
assert_eq!(preprocessor.lessopen, "batpipe %s");
|
||||
assert!(matches!(preprocessor.kind, LessOpenKind::Piped));
|
||||
assert!(preprocessor.preprocess_stdin);
|
||||
|
||||
@@ -391,8 +377,8 @@ mod tests {
|
||||
let preprocessor =
|
||||
LessOpenPreprocessor::mock_new(Some("|echo File:%s"), Some("echo File:%s Temp:%s"))?;
|
||||
|
||||
assert_eq!(preprocessor.lessopen, "echo File:$1");
|
||||
assert_eq!(preprocessor.lessclose.unwrap(), "echo File:$1 Temp:$2");
|
||||
assert_eq!(preprocessor.lessopen, "echo File:%s");
|
||||
assert_eq!(preprocessor.lessclose.unwrap(), "echo File:%s Temp:%s");
|
||||
|
||||
reset_env_vars();
|
||||
|
||||
|
@@ -38,7 +38,7 @@ mod less;
|
||||
mod lessopen;
|
||||
pub mod line_range;
|
||||
pub(crate) mod nonprintable_notation;
|
||||
mod output;
|
||||
pub mod output;
|
||||
#[cfg(feature = "paging")]
|
||||
mod pager;
|
||||
#[cfg(feature = "paging")]
|
||||
@@ -49,10 +49,11 @@ pub(crate) mod printer;
|
||||
pub mod style;
|
||||
pub(crate) mod syntax_mapping;
|
||||
mod terminal;
|
||||
pub mod theme;
|
||||
mod vscreen;
|
||||
pub(crate) mod wrapping;
|
||||
|
||||
pub use nonprintable_notation::NonprintableNotation;
|
||||
pub use nonprintable_notation::{BinaryBehavior, NonprintableNotation};
|
||||
pub use preprocessor::StripAnsiMode;
|
||||
pub use pretty_printer::{Input, PrettyPrinter, Syntax};
|
||||
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
|
||||
|
@@ -10,3 +10,15 @@ pub enum NonprintableNotation {
|
||||
#[default]
|
||||
Unicode,
|
||||
}
|
||||
|
||||
/// How to treat binary content
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum BinaryBehavior {
|
||||
/// Do not print any binary content
|
||||
#[default]
|
||||
NoPrinting,
|
||||
|
||||
/// Treat binary content as normal text
|
||||
AsText,
|
||||
}
|
||||
|
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::io::{self, Write};
|
||||
#[cfg(feature = "paging")]
|
||||
use std::process::Child;
|
||||
@@ -162,3 +163,17 @@ impl Drop for OutputType {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum OutputHandle<'a> {
|
||||
IoWrite(&'a mut dyn io::Write),
|
||||
FmtWrite(&'a mut dyn fmt::Write),
|
||||
}
|
||||
|
||||
impl OutputHandle<'_> {
|
||||
pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result<()> {
|
||||
match self {
|
||||
Self::IoWrite(handle) => handle.write_fmt(args).map_err(Into::into),
|
||||
Self::FmtWrite(handle) => handle.write_fmt(args).map_err(Into::into),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ use crate::{
|
||||
error::Result,
|
||||
input,
|
||||
line_range::{HighlightedLineRanges, LineRange, LineRanges},
|
||||
output::OutputHandle,
|
||||
style::StyleComponent,
|
||||
StripAnsiMode, SyntaxMapping, WrappingMode,
|
||||
};
|
||||
@@ -245,7 +246,9 @@ impl<'a> PrettyPrinter<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Specify the highlighting theme
|
||||
/// Specify the highlighting theme.
|
||||
/// You can use [`crate::theme::theme`] to pick a theme based on user preferences
|
||||
/// and the terminal's background color.
|
||||
pub fn theme(&mut self, theme: impl AsRef<str>) -> &mut Self {
|
||||
self.config.theme = theme.as_ref().to_owned();
|
||||
self
|
||||
@@ -279,6 +282,11 @@ impl<'a> PrettyPrinter<'a> {
|
||||
/// If you want to call 'print' multiple times, you have to call the appropriate
|
||||
/// input_* methods again.
|
||||
pub fn print(&mut self) -> Result<bool> {
|
||||
self.print_with_writer(None::<&mut dyn std::fmt::Write>)
|
||||
}
|
||||
|
||||
/// Pretty-print all specified inputs to a specified writer.
|
||||
pub fn print_with_writer<W: std::fmt::Write>(&mut self, writer: Option<W>) -> Result<bool> {
|
||||
let highlight_lines = std::mem::take(&mut self.highlighted_lines);
|
||||
self.config.highlighted_lines = HighlightedLineRanges(LineRanges::from(highlight_lines));
|
||||
self.config.term_width = self
|
||||
@@ -315,7 +323,16 @@ impl<'a> PrettyPrinter<'a> {
|
||||
|
||||
// Run the controller
|
||||
let controller = Controller::new(&self.config, &self.assets);
|
||||
controller.run(inputs.into_iter().map(|i| i.into()).collect(), None)
|
||||
|
||||
// If writer is provided, pass it to the controller, otherwise pass None
|
||||
if let Some(mut w) = writer {
|
||||
controller.run(
|
||||
inputs.into_iter().map(|i| i.into()).collect(),
|
||||
Some(OutputHandle::FmtWrite(&mut w)),
|
||||
)
|
||||
} else {
|
||||
controller.run(inputs.into_iter().map(|i| i.into()).collect(), None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,3 @@
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::vec::Vec;
|
||||
|
||||
use nu_ansi_term::Color::{Fixed, Green, Red, Yellow};
|
||||
@@ -17,6 +15,7 @@ use content_inspector::ContentType;
|
||||
|
||||
use encoding_rs::{UTF_16BE, UTF_16LE};
|
||||
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthChar;
|
||||
|
||||
use crate::assets::{HighlightingAssets, SyntaxReferenceInSet};
|
||||
@@ -29,12 +28,14 @@ use crate::diff::LineChanges;
|
||||
use crate::error::*;
|
||||
use crate::input::OpenedInput;
|
||||
use crate::line_range::{MaxBufferedLineNumber, RangeCheckResult};
|
||||
use crate::output::OutputHandle;
|
||||
use crate::preprocessor::strip_ansi;
|
||||
use crate::preprocessor::{expand_tabs, replace_nonprintable};
|
||||
use crate::style::StyleComponent;
|
||||
use crate::terminal::{as_terminal_escaped, to_ansi_color};
|
||||
use crate::vscreen::{AnsiStyle, EscapeSequence, EscapeSequenceIterator};
|
||||
use crate::wrapping::WrappingMode;
|
||||
use crate::BinaryBehavior;
|
||||
use crate::StripAnsiMode;
|
||||
|
||||
const ANSI_UNDERLINE_ENABLE: EscapeSequence = EscapeSequence::CSI {
|
||||
@@ -67,20 +68,6 @@ const EMPTY_SYNTECT_STYLE: syntect::highlighting::Style = syntect::highlighting:
|
||||
font_style: FontStyle::empty(),
|
||||
};
|
||||
|
||||
pub enum OutputHandle<'a> {
|
||||
IoWrite(&'a mut dyn io::Write),
|
||||
FmtWrite(&'a mut dyn fmt::Write),
|
||||
}
|
||||
|
||||
impl<'a> OutputHandle<'a> {
|
||||
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result<()> {
|
||||
match self {
|
||||
Self::IoWrite(handle) => handle.write_fmt(args).map_err(Into::into),
|
||||
Self::FmtWrite(handle) => handle.write_fmt(args).map_err(Into::into),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait Printer {
|
||||
fn print_header(
|
||||
&mut self,
|
||||
@@ -116,7 +103,7 @@ impl<'a> SimplePrinter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Printer for SimplePrinter<'a> {
|
||||
impl Printer for SimplePrinter<'_> {
|
||||
fn print_header(
|
||||
&mut self,
|
||||
_handle: &mut OutputHandle,
|
||||
@@ -145,7 +132,7 @@ impl<'a> Printer for SimplePrinter<'a> {
|
||||
// Skip squeezed lines.
|
||||
if let Some(squeeze_limit) = self.config.squeeze_lines {
|
||||
if String::from_utf8_lossy(line_buffer)
|
||||
.trim_end_matches(|c| c == '\r' || c == '\n')
|
||||
.trim_end_matches(['\r', '\n'])
|
||||
.is_empty()
|
||||
{
|
||||
self.consecutive_empty_lines += 1;
|
||||
@@ -268,9 +255,10 @@ impl<'a> InteractivePrinter<'a> {
|
||||
let is_printing_binary = input
|
||||
.reader
|
||||
.content_type
|
||||
.map_or(false, |c| c.is_binary() && !config.show_nonprintable);
|
||||
.is_some_and(|c| c.is_binary() && !config.show_nonprintable);
|
||||
|
||||
let needs_to_match_syntax = !is_printing_binary
|
||||
let needs_to_match_syntax = (!is_printing_binary
|
||||
|| matches!(config.binary, BinaryBehavior::AsText))
|
||||
&& (config.colored_output || config.strip_ansi == StripAnsiMode::Auto);
|
||||
|
||||
let (is_plain_text, highlighter_from_set) = if needs_to_match_syntax {
|
||||
@@ -403,14 +391,18 @@ impl<'a> InteractivePrinter<'a> {
|
||||
handle: &mut OutputHandle,
|
||||
content: &str,
|
||||
) -> Result<()> {
|
||||
let mut content = content;
|
||||
let content_width = self.config.term_width - self.get_header_component_indent_length();
|
||||
while content.len() > content_width {
|
||||
let (content_line, remaining) = content.split_at(content_width);
|
||||
self.print_header_component_with_indent(handle, content_line)?;
|
||||
content = remaining;
|
||||
if content.chars().count() <= content_width {
|
||||
return self.print_header_component_with_indent(handle, content);
|
||||
}
|
||||
self.print_header_component_with_indent(handle, content)
|
||||
|
||||
let mut content_graphemes: Vec<&str> = content.graphemes(true).collect();
|
||||
while content_graphemes.len() > content_width {
|
||||
let (content_line, remaining) = content_graphemes.split_at(content_width);
|
||||
self.print_header_component_with_indent(handle, content_line.join("").as_str())?;
|
||||
content_graphemes = remaining.iter().cloned().collect();
|
||||
}
|
||||
self.print_header_component_with_indent(handle, content_graphemes.join("").as_str())
|
||||
}
|
||||
|
||||
fn highlight_regions_for_line<'b>(
|
||||
@@ -432,7 +424,7 @@ impl<'a> InteractivePrinter<'a> {
|
||||
.highlight_line(for_highlighting, highlighter_from_set.syntax_set)?;
|
||||
|
||||
if too_long {
|
||||
highlighted_line[0].1 = &line;
|
||||
highlighted_line[0].1 = line;
|
||||
}
|
||||
|
||||
Ok(highlighted_line)
|
||||
@@ -448,7 +440,7 @@ impl<'a> InteractivePrinter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Printer for InteractivePrinter<'a> {
|
||||
impl Printer for InteractivePrinter<'_> {
|
||||
fn print_header(
|
||||
&mut self,
|
||||
handle: &mut OutputHandle,
|
||||
@@ -460,7 +452,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
}
|
||||
|
||||
if !self.config.style_components.header() {
|
||||
if Some(ContentType::BINARY) == self.content_type && !self.config.show_nonprintable {
|
||||
if Some(ContentType::BINARY) == self.content_type
|
||||
&& !self.config.show_nonprintable
|
||||
&& !matches!(self.config.binary, BinaryBehavior::AsText)
|
||||
{
|
||||
writeln!(
|
||||
handle,
|
||||
"{}: Binary content from {} will not be printed to the terminal \
|
||||
@@ -541,7 +536,10 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
})?;
|
||||
|
||||
if self.config.style_components.grid() {
|
||||
if self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable {
|
||||
if self.content_type.is_some_and(|c| c.is_text())
|
||||
|| self.config.show_nonprintable
|
||||
|| matches!(self.config.binary, BinaryBehavior::AsText)
|
||||
{
|
||||
self.print_horizontal_line(handle, '┼')?;
|
||||
} else {
|
||||
self.print_horizontal_line(handle, '┴')?;
|
||||
@@ -553,7 +551,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
|
||||
fn print_footer(&mut self, handle: &mut OutputHandle, _input: &OpenedInput) -> Result<()> {
|
||||
if self.config.style_components.grid()
|
||||
&& (self.content_type.map_or(false, |c| c.is_text()) || self.config.show_nonprintable)
|
||||
&& (self.content_type.is_some_and(|c| c.is_text())
|
||||
|| self.config.show_nonprintable
|
||||
|| matches!(self.config.binary, BinaryBehavior::AsText))
|
||||
{
|
||||
self.print_horizontal_line(handle, '┴')
|
||||
} else {
|
||||
@@ -602,7 +602,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
.into()
|
||||
} else {
|
||||
let mut line = match self.content_type {
|
||||
Some(ContentType::BINARY) | None => {
|
||||
Some(ContentType::BINARY) | None
|
||||
if !matches!(self.config.binary, BinaryBehavior::AsText) =>
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
Some(ContentType::UTF_16LE) => UTF_16LE.decode_with_bom_removal(line_buffer).0,
|
||||
@@ -635,7 +637,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
|
||||
// Skip squeezed lines.
|
||||
if let Some(squeeze_limit) = self.config.squeeze_lines {
|
||||
if line.trim_end_matches(|c| c == '\r' || c == '\n').is_empty() {
|
||||
if line.trim_end_matches(['\r', '\n']).is_empty() {
|
||||
self.consecutive_empty_lines += 1;
|
||||
if self.consecutive_empty_lines > squeeze_limit {
|
||||
return Ok(());
|
||||
@@ -692,7 +694,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
// Regular text.
|
||||
EscapeSequence::Text(text) => {
|
||||
let text = self.preprocess(text, &mut cursor_total);
|
||||
let text_trimmed = text.trim_end_matches(|c| c == '\r' || c == '\n');
|
||||
let text_trimmed = text.trim_end_matches(['\r', '\n']);
|
||||
|
||||
write!(
|
||||
handle,
|
||||
@@ -746,10 +748,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
match chunk {
|
||||
// Regular text.
|
||||
EscapeSequence::Text(text) => {
|
||||
let text = self.preprocess(
|
||||
text.trim_end_matches(|c| c == '\r' || c == '\n'),
|
||||
&mut cursor_total,
|
||||
);
|
||||
let text = self
|
||||
.preprocess(text.trim_end_matches(['\r', '\n']), &mut cursor_total);
|
||||
|
||||
let mut max_width = cursor_max - cursor;
|
||||
|
||||
|
@@ -225,7 +225,7 @@ impl FromStr for StyleComponentList {
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
Ok(StyleComponentList(
|
||||
s.split(",")
|
||||
.map(|s| ComponentAction::extract_from_str(s)) // If the component starts with "-", it's meant to be removed
|
||||
.map(ComponentAction::extract_from_str) // If the component starts with "-", it's meant to be removed
|
||||
.map(|(a, s)| Ok((a, StyleComponent::from_str(s)?)))
|
||||
.collect::<Result<Vec<(ComponentAction, StyleComponent)>>>()?,
|
||||
))
|
||||
|
@@ -61,7 +61,7 @@ pub struct SyntaxMapping<'a> {
|
||||
halt_glob_build: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl<'a> Drop for SyntaxMapping<'a> {
|
||||
impl Drop for SyntaxMapping<'_> {
|
||||
fn drop(&mut self) {
|
||||
// signal the offload thread to halt early
|
||||
self.halt_glob_build.store(true, Ordering::Relaxed);
|
||||
@@ -153,7 +153,7 @@ impl<'a> SyntaxMapping<'a> {
|
||||
if glob.is_match_candidate(&candidate)
|
||||
|| candidate_filename
|
||||
.as_ref()
|
||||
.map_or(false, |filename| glob.is_match_candidate(filename))
|
||||
.is_some_and(|filename| glob.is_match_candidate(filename))
|
||||
{
|
||||
return Some(*syntax);
|
||||
}
|
||||
|
2
src/syntax_mapping/builtins/common/50-citation.toml
Normal file
2
src/syntax_mapping/builtins/common/50-citation.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[mappings]
|
||||
"YAML" = ["CITATION.cff"]
|
3
src/syntax_mapping/builtins/common/50-diff.toml
Normal file
3
src/syntax_mapping/builtins/common/50-diff.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
# .debdiff is the extension used for diffs in Debian packaging
|
||||
[mappings]
|
||||
"Diff" = ["*.debdiff"]
|
2
src/syntax_mapping/builtins/common/50-dotnet-xml.toml
Normal file
2
src/syntax_mapping/builtins/common/50-dotnet-xml.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[mappings]
|
||||
"XML" = ["*.csproj", "*.vbproj", "*.props", "*.targets"]
|
@@ -1,3 +1,3 @@
|
||||
# JSON Lines is a simple variation of JSON #2535
|
||||
[mappings]
|
||||
"JSON" = ["*.jsonl", "*.jsonc", "*.jsonld"]
|
||||
"JSON" = ["*.jsonl", "*.jsonc", "*.jsonld", "*.geojson", "*.ndjson"]
|
||||
|
2
src/syntax_mapping/builtins/common/50-markdown.toml
Normal file
2
src/syntax_mapping/builtins/common/50-markdown.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[mappings]
|
||||
"Markdown" = ["*.mkd"]
|
2
src/syntax_mapping/builtins/common/50-nix.toml
Normal file
2
src/syntax_mapping/builtins/common/50-nix.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[mappings]
|
||||
"JSON" = ["flake.lock"]
|
2
src/syntax_mapping/builtins/linux/50-kubernetes.toml
Normal file
2
src/syntax_mapping/builtins/linux/50-kubernetes.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[mappings]
|
||||
"YAML" = ["/etc/kubernetes/*.conf"]
|
@@ -1,3 +1,8 @@
|
||||
[mappings]
|
||||
# pacman hooks
|
||||
"INI" = ["/usr/share/libalpm/hooks/*.hook", "/etc/pacman.d/hooks/*.hook"]
|
||||
"INI" = [
|
||||
# config
|
||||
"/etc/pacman.conf",
|
||||
# hooks
|
||||
"/usr/share/libalpm/hooks/*.hook",
|
||||
"/etc/pacman.d/hooks/*.hook",
|
||||
]
|
||||
|
6
src/syntax_mapping/builtins/linux/50-paru.toml
Normal file
6
src/syntax_mapping/builtins/linux/50-paru.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
# See https://github.com/Morganamilo/paru/blob/master/man/paru.conf.5
|
||||
[mappings]
|
||||
"INI" = [
|
||||
"${PARU_CONF}",
|
||||
"paru.conf",
|
||||
]
|
@@ -1,2 +1,2 @@
|
||||
[mappings]
|
||||
"Apache Conf" = ["/etc/apache2/**/*.conf", "/etc/apache2/sites-*/**/*"]
|
||||
"Apache Conf" = ["/etc/apache2/**/*.conf", "/etc/apache2/sites-*/**/*", "/etc/httpd/conf/**/*.conf"]
|
||||
|
@@ -2,4 +2,24 @@
|
||||
"Bourne Again Shell (bash)" = [
|
||||
# used by lots of shells
|
||||
"/etc/profile",
|
||||
|
||||
"bashrc",
|
||||
"*.bashrc",
|
||||
"bash_profile",
|
||||
"*.bash_profile",
|
||||
"bash_login",
|
||||
"*.bash_login",
|
||||
"bash_logout",
|
||||
"*.bash_logout",
|
||||
|
||||
"zshrc",
|
||||
"*.zshrc",
|
||||
"zprofile",
|
||||
"*.zprofile",
|
||||
"zlogin",
|
||||
"*.zlogin",
|
||||
"zlogout",
|
||||
"*.zlogout",
|
||||
"zshenv",
|
||||
"*.zshenv"
|
||||
]
|
||||
|
570
src/theme.rs
Normal file
570
src/theme.rs
Normal file
@@ -0,0 +1,570 @@
|
||||
//! Utilities for choosing an appropriate theme for syntax highlighting.
|
||||
|
||||
use std::convert::Infallible;
|
||||
use std::fmt;
|
||||
use std::io::IsTerminal as _;
|
||||
use std::str::FromStr;
|
||||
|
||||
/// Environment variable names.
|
||||
pub mod env {
|
||||
/// See [`crate::theme::ThemeOptions::theme`].
|
||||
pub const BAT_THEME: &str = "BAT_THEME";
|
||||
/// See [`crate::theme::ThemeOptions::theme_dark`].
|
||||
pub const BAT_THEME_DARK: &str = "BAT_THEME_DARK";
|
||||
/// See [`crate::theme::ThemeOptions::theme_light`].
|
||||
pub const BAT_THEME_LIGHT: &str = "BAT_THEME_LIGHT";
|
||||
}
|
||||
|
||||
/// Chooses an appropriate theme or falls back to a default theme
|
||||
/// based on the user-provided options and the color scheme of the terminal.
|
||||
///
|
||||
/// Intentionally returns a [`ThemeResult`] instead of a simple string so
|
||||
/// that downstream consumers such as `delta` can easily apply their own
|
||||
/// default theme and can use the detected color scheme elsewhere.
|
||||
pub fn theme(options: ThemeOptions) -> ThemeResult {
|
||||
theme_impl(options, &TerminalColorSchemeDetector)
|
||||
}
|
||||
|
||||
/// The default theme, suitable for the given color scheme.
|
||||
/// Use [`theme`] if you want to automatically detect the color scheme from the terminal.
|
||||
pub const fn default_theme(color_scheme: ColorScheme) -> &'static str {
|
||||
match color_scheme {
|
||||
ColorScheme::Dark => "Monokai Extended",
|
||||
ColorScheme::Light => "Monokai Extended Light",
|
||||
}
|
||||
}
|
||||
|
||||
/// Detects the color scheme from the terminal.
|
||||
pub fn color_scheme(when: DetectColorScheme) -> Option<ColorScheme> {
|
||||
color_scheme_impl(when, &TerminalColorSchemeDetector)
|
||||
}
|
||||
|
||||
/// Options for configuring the theme used for syntax highlighting.
|
||||
/// Used together with [`theme`].
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct ThemeOptions {
|
||||
/// Configures how the theme is chosen. If set to a [`ThemePreference::Fixed`] value,
|
||||
/// then the given theme is used regardless of the terminal's background color.
|
||||
/// This corresponds with the `BAT_THEME` environment variable and the `--theme` option.
|
||||
pub theme: ThemePreference,
|
||||
/// The theme to use in case the terminal uses a dark background with light text.
|
||||
/// This corresponds with the `BAT_THEME_DARK` environment variable and the `--theme-dark` option.
|
||||
pub theme_dark: Option<ThemeName>,
|
||||
/// The theme to use in case the terminal uses a light background with dark text.
|
||||
/// This corresponds with the `BAT_THEME_LIGHT` environment variable and the `--theme-light` option.
|
||||
pub theme_light: Option<ThemeName>,
|
||||
}
|
||||
|
||||
/// What theme should `bat` use?
|
||||
///
|
||||
/// The easiest way to construct this is from a string:
|
||||
/// ```
|
||||
/// # use bat::theme::{ThemePreference, DetectColorScheme};
|
||||
/// let preference = ThemePreference::new("auto:system");
|
||||
/// assert_eq!(ThemePreference::Auto(DetectColorScheme::System), preference);
|
||||
/// ```
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum ThemePreference {
|
||||
/// Choose between [`ThemeOptions::theme_dark`] and [`ThemeOptions::theme_light`]
|
||||
/// based on the terminal's color scheme.
|
||||
Auto(DetectColorScheme),
|
||||
/// Always use the same theme regardless of the terminal's color scheme.
|
||||
Fixed(ThemeName),
|
||||
/// Use a dark theme.
|
||||
Dark,
|
||||
/// Use a light theme.
|
||||
Light,
|
||||
}
|
||||
|
||||
impl Default for ThemePreference {
|
||||
fn default() -> Self {
|
||||
ThemePreference::Auto(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl ThemePreference {
|
||||
/// Creates a theme preference from a string.
|
||||
pub fn new(s: impl Into<String>) -> Self {
|
||||
use ThemePreference::*;
|
||||
let s = s.into();
|
||||
match s.as_str() {
|
||||
"auto" => Auto(Default::default()),
|
||||
"auto:always" => Auto(DetectColorScheme::Always),
|
||||
"auto:system" => Auto(DetectColorScheme::System),
|
||||
"dark" => Dark,
|
||||
"light" => Light,
|
||||
_ => Fixed(ThemeName::new(s)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for ThemePreference {
|
||||
type Err = Infallible;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(ThemePreference::new(s))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ThemePreference {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
use ThemePreference::*;
|
||||
match self {
|
||||
Auto(DetectColorScheme::Auto) => f.write_str("auto"),
|
||||
Auto(DetectColorScheme::Always) => f.write_str("auto:always"),
|
||||
Auto(DetectColorScheme::System) => f.write_str("auto:system"),
|
||||
Fixed(theme) => theme.fmt(f),
|
||||
Dark => f.write_str("dark"),
|
||||
Light => f.write_str("light"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of a theme or the default theme.
|
||||
///
|
||||
/// ```
|
||||
/// # use bat::theme::ThemeName;
|
||||
/// assert_eq!(ThemeName::Default, ThemeName::new("default"));
|
||||
/// assert_eq!(ThemeName::Named("example".to_string()), ThemeName::new("example"));
|
||||
/// ```
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum ThemeName {
|
||||
Named(String),
|
||||
Default,
|
||||
}
|
||||
|
||||
impl ThemeName {
|
||||
/// Creates a theme name from a string.
|
||||
pub fn new(s: impl Into<String>) -> Self {
|
||||
let s = s.into();
|
||||
if s == "default" {
|
||||
ThemeName::Default
|
||||
} else {
|
||||
ThemeName::Named(s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for ThemeName {
|
||||
type Err = Infallible;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(ThemeName::new(s))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ThemeName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ThemeName::Named(t) => f.write_str(t),
|
||||
ThemeName::Default => f.write_str("default"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum DetectColorScheme {
|
||||
/// Only query the terminal for its colors when appropriate (i.e. when the output is not redirected).
|
||||
#[default]
|
||||
Auto,
|
||||
/// Always query the terminal for its colors.
|
||||
Always,
|
||||
/// Detect the system-wide dark/light preference (macOS only).
|
||||
System,
|
||||
}
|
||||
|
||||
/// The color scheme used to pick a fitting theme. Defaults to [`ColorScheme::Dark`].
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum ColorScheme {
|
||||
#[default]
|
||||
Dark,
|
||||
Light,
|
||||
}
|
||||
|
||||
/// The resolved theme and the color scheme as determined from
|
||||
/// the terminal, OS or fallback.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ThemeResult {
|
||||
/// The theme selected according to the [`ThemeOptions`].
|
||||
pub theme: ThemeName,
|
||||
/// Either the user's chosen color scheme, the terminal's color scheme, the OS's
|
||||
/// color scheme or `None` if the color scheme was not detected because the user chose a fixed theme.
|
||||
pub color_scheme: Option<ColorScheme>,
|
||||
}
|
||||
|
||||
impl fmt::Display for ThemeResult {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match &self.theme {
|
||||
ThemeName::Named(name) => f.write_str(name),
|
||||
ThemeName::Default => f.write_str(default_theme(self.color_scheme.unwrap_or_default())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn theme_impl(options: ThemeOptions, detector: &dyn ColorSchemeDetector) -> ThemeResult {
|
||||
// Implementation note: This function is mostly pure (i.e. it has no side effects) for the sake of testing.
|
||||
// All the side effects (e.g. querying the terminal for its colors) are performed in the detector.
|
||||
match options.theme {
|
||||
ThemePreference::Fixed(theme) => ThemeResult {
|
||||
theme,
|
||||
color_scheme: None,
|
||||
},
|
||||
ThemePreference::Dark => choose_theme_opt(Some(ColorScheme::Dark), options),
|
||||
ThemePreference::Light => choose_theme_opt(Some(ColorScheme::Light), options),
|
||||
ThemePreference::Auto(when) => choose_theme_opt(color_scheme_impl(when, detector), options),
|
||||
}
|
||||
}
|
||||
|
||||
fn choose_theme_opt(color_scheme: Option<ColorScheme>, options: ThemeOptions) -> ThemeResult {
|
||||
ThemeResult {
|
||||
color_scheme,
|
||||
theme: color_scheme
|
||||
.and_then(|c| choose_theme(options, c))
|
||||
.unwrap_or(ThemeName::Default),
|
||||
}
|
||||
}
|
||||
|
||||
fn choose_theme(options: ThemeOptions, color_scheme: ColorScheme) -> Option<ThemeName> {
|
||||
match color_scheme {
|
||||
ColorScheme::Dark => options.theme_dark,
|
||||
ColorScheme::Light => options.theme_light,
|
||||
}
|
||||
}
|
||||
|
||||
fn color_scheme_impl(
|
||||
when: DetectColorScheme,
|
||||
detector: &dyn ColorSchemeDetector,
|
||||
) -> Option<ColorScheme> {
|
||||
let should_detect = match when {
|
||||
DetectColorScheme::Auto => detector.should_detect(),
|
||||
DetectColorScheme::Always => true,
|
||||
DetectColorScheme::System => return color_scheme_from_system(),
|
||||
};
|
||||
should_detect.then(|| detector.detect()).flatten()
|
||||
}
|
||||
|
||||
trait ColorSchemeDetector {
|
||||
fn should_detect(&self) -> bool;
|
||||
|
||||
fn detect(&self) -> Option<ColorScheme>;
|
||||
}
|
||||
|
||||
struct TerminalColorSchemeDetector;
|
||||
|
||||
impl ColorSchemeDetector for TerminalColorSchemeDetector {
|
||||
fn should_detect(&self) -> bool {
|
||||
// Querying the terminal for its colors via OSC 10 / OSC 11 requires "exclusive" access
|
||||
// since we read/write from the terminal and enable/disable raw mode.
|
||||
// This causes race conditions with pagers such as less when they are attached to the
|
||||
// same terminal as us.
|
||||
//
|
||||
// This is usually only an issue when the output is manually piped to a pager.
|
||||
// For example: `bat Cargo.toml | less`.
|
||||
// Otherwise, if we start the pager ourselves, then there's no race condition
|
||||
// since the pager is started *after* the color is detected.
|
||||
std::io::stdout().is_terminal()
|
||||
}
|
||||
|
||||
fn detect(&self) -> Option<ColorScheme> {
|
||||
use terminal_colorsaurus::{color_scheme, ColorScheme as ColorsaurusScheme, QueryOptions};
|
||||
match color_scheme(QueryOptions::default()).ok()? {
|
||||
ColorsaurusScheme::Dark => Some(ColorScheme::Dark),
|
||||
ColorsaurusScheme::Light => Some(ColorScheme::Light),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
fn color_scheme_from_system() -> Option<ColorScheme> {
|
||||
crate::bat_warning!(
|
||||
"Theme 'auto:system' is only supported on macOS, \
|
||||
using default."
|
||||
);
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn color_scheme_from_system() -> Option<ColorScheme> {
|
||||
const PREFERENCES_FILE: &str = "Library/Preferences/.GlobalPreferences.plist";
|
||||
const STYLE_KEY: &str = "AppleInterfaceStyle";
|
||||
|
||||
let preferences_file = home::home_dir()
|
||||
.map(|home| home.join(PREFERENCES_FILE))
|
||||
.expect("Could not get home directory");
|
||||
|
||||
match plist::Value::from_file(preferences_file).map(|file| file.into_dictionary()) {
|
||||
Ok(Some(preferences)) => match preferences.get(STYLE_KEY).and_then(|val| val.as_string()) {
|
||||
Some("Dark") => Some(ColorScheme::Dark),
|
||||
// If the key does not exist, then light theme is currently in use.
|
||||
Some(_) | None => Some(ColorScheme::Light),
|
||||
},
|
||||
// Unreachable, in theory. All macOS users have a home directory and preferences file setup.
|
||||
Ok(None) | Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl ColorSchemeDetector for Option<ColorScheme> {
|
||||
fn should_detect(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn detect(&self) -> Option<ColorScheme> {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ColorScheme::*;
|
||||
use super::*;
|
||||
use std::cell::Cell;
|
||||
use std::iter;
|
||||
|
||||
mod color_scheme_detection {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn not_called_for_dark_or_light() {
|
||||
for theme in [ThemePreference::Dark, ThemePreference::Light] {
|
||||
let detector = DetectorStub::should_detect(Some(Dark));
|
||||
let options = ThemeOptions {
|
||||
theme,
|
||||
..Default::default()
|
||||
};
|
||||
_ = theme_impl(options, &detector);
|
||||
assert!(!detector.was_called.get());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn called_for_always() {
|
||||
let detectors = [
|
||||
DetectorStub::should_detect(Some(Dark)),
|
||||
DetectorStub::should_not_detect(),
|
||||
];
|
||||
for detector in detectors {
|
||||
let options = ThemeOptions {
|
||||
theme: ThemePreference::Auto(DetectColorScheme::Always),
|
||||
..Default::default()
|
||||
};
|
||||
_ = theme_impl(options, &detector);
|
||||
assert!(detector.was_called.get());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn called_for_auto_if_should_detect() {
|
||||
let detector = DetectorStub::should_detect(Some(Dark));
|
||||
_ = theme_impl(ThemeOptions::default(), &detector);
|
||||
assert!(detector.was_called.get());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_called_for_auto_if_not_should_detect() {
|
||||
let detector = DetectorStub::should_not_detect();
|
||||
_ = theme_impl(ThemeOptions::default(), &detector);
|
||||
assert!(!detector.was_called.get());
|
||||
}
|
||||
}
|
||||
|
||||
mod precedence {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn theme_is_preferred_over_light_or_dark_themes() {
|
||||
for color_scheme in optional(color_schemes()) {
|
||||
for options in [
|
||||
ThemeOptions {
|
||||
theme: ThemePreference::Fixed(ThemeName::Named("Theme".to_string())),
|
||||
..Default::default()
|
||||
},
|
||||
ThemeOptions {
|
||||
theme: ThemePreference::Fixed(ThemeName::Named("Theme".to_string())),
|
||||
theme_dark: Some(ThemeName::Named("Dark Theme".to_string())),
|
||||
theme_light: Some(ThemeName::Named("Light Theme".to_string())),
|
||||
},
|
||||
] {
|
||||
let detector = ConstantDetector(color_scheme);
|
||||
assert_eq!("Theme", theme_impl(options, &detector).to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detector_is_not_called_if_theme_is_present() {
|
||||
let options = ThemeOptions {
|
||||
theme: ThemePreference::Fixed(ThemeName::Named("Theme".to_string())),
|
||||
..Default::default()
|
||||
};
|
||||
let detector = DetectorStub::should_detect(Some(Dark));
|
||||
_ = theme_impl(options, &detector);
|
||||
assert!(!detector.was_called.get());
|
||||
}
|
||||
}
|
||||
|
||||
mod default_theme {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn default_dark_if_unable_to_detect_color_scheme() {
|
||||
let detector = ConstantDetector(None);
|
||||
assert_eq!(
|
||||
default_theme(ColorScheme::Dark),
|
||||
theme_impl(ThemeOptions::default(), &detector).to_string()
|
||||
);
|
||||
}
|
||||
|
||||
// For backwards compatibility, if the default theme is requested
|
||||
// explicitly through BAT_THEME, we always pick the default dark theme.
|
||||
#[test]
|
||||
fn default_dark_if_requested_explicitly_through_theme() {
|
||||
for color_scheme in optional(color_schemes()) {
|
||||
let options = ThemeOptions {
|
||||
theme: ThemePreference::Fixed(ThemeName::Default),
|
||||
..Default::default()
|
||||
};
|
||||
let detector = ConstantDetector(color_scheme);
|
||||
assert_eq!(
|
||||
default_theme(ColorScheme::Dark),
|
||||
theme_impl(options, &detector).to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn varies_depending_on_color_scheme() {
|
||||
for color_scheme in color_schemes() {
|
||||
for options in [
|
||||
ThemeOptions::default(),
|
||||
ThemeOptions {
|
||||
theme_dark: Some(ThemeName::Default),
|
||||
theme_light: Some(ThemeName::Default),
|
||||
..Default::default()
|
||||
},
|
||||
] {
|
||||
let detector = ConstantDetector(Some(color_scheme));
|
||||
assert_eq!(
|
||||
default_theme(color_scheme),
|
||||
theme_impl(options, &detector).to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod choosing {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn chooses_default_theme_if_unknown() {
|
||||
let options = ThemeOptions {
|
||||
theme_dark: Some(ThemeName::Named("Dark".to_string())),
|
||||
theme_light: Some(ThemeName::Named("Light".to_string())),
|
||||
..Default::default()
|
||||
};
|
||||
let detector = ConstantDetector(None);
|
||||
assert_eq!(
|
||||
default_theme(ColorScheme::default()),
|
||||
theme_impl(options, &detector).to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chooses_dark_theme_if_dark_or_unknown() {
|
||||
let options = ThemeOptions {
|
||||
theme_dark: Some(ThemeName::Named("Dark".to_string())),
|
||||
theme_light: Some(ThemeName::Named("Light".to_string())),
|
||||
..Default::default()
|
||||
};
|
||||
let detector = ConstantDetector(Some(ColorScheme::Dark));
|
||||
assert_eq!("Dark", theme_impl(options, &detector).to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chooses_light_theme_if_light() {
|
||||
let options = ThemeOptions {
|
||||
theme_dark: Some(ThemeName::Named("Dark".to_string())),
|
||||
theme_light: Some(ThemeName::Named("Light".to_string())),
|
||||
..Default::default()
|
||||
};
|
||||
let detector = ConstantDetector(Some(ColorScheme::Light));
|
||||
assert_eq!("Light", theme_impl(options, &detector).to_string());
|
||||
}
|
||||
}
|
||||
|
||||
mod theme_preference {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn values_roundtrip_via_display() {
|
||||
let prefs = [
|
||||
ThemePreference::Auto(DetectColorScheme::Auto),
|
||||
ThemePreference::Auto(DetectColorScheme::Always),
|
||||
ThemePreference::Auto(DetectColorScheme::System),
|
||||
ThemePreference::Fixed(ThemeName::Default),
|
||||
ThemePreference::Fixed(ThemeName::new("foo")),
|
||||
ThemePreference::Dark,
|
||||
ThemePreference::Light,
|
||||
];
|
||||
for pref in prefs {
|
||||
assert_eq!(pref, ThemePreference::new(pref.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DetectorStub {
|
||||
should_detect: bool,
|
||||
color_scheme: Option<ColorScheme>,
|
||||
was_called: Cell<bool>,
|
||||
}
|
||||
|
||||
impl DetectorStub {
|
||||
fn should_detect(color_scheme: Option<ColorScheme>) -> Self {
|
||||
DetectorStub {
|
||||
should_detect: true,
|
||||
color_scheme,
|
||||
was_called: Cell::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn should_not_detect() -> Self {
|
||||
DetectorStub {
|
||||
should_detect: false,
|
||||
color_scheme: None,
|
||||
was_called: Cell::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ColorSchemeDetector for DetectorStub {
|
||||
fn should_detect(&self) -> bool {
|
||||
self.should_detect
|
||||
}
|
||||
|
||||
fn detect(&self) -> Option<ColorScheme> {
|
||||
self.was_called.set(true);
|
||||
self.color_scheme
|
||||
}
|
||||
}
|
||||
|
||||
struct ConstantDetector(Option<ColorScheme>);
|
||||
|
||||
impl ColorSchemeDetector for ConstantDetector {
|
||||
fn should_detect(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn detect(&self) -> Option<ColorScheme> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
fn optional<T>(value: impl Iterator<Item = T>) -> impl Iterator<Item = Option<T>> {
|
||||
value.map(Some).chain(iter::once(None))
|
||||
}
|
||||
|
||||
fn color_schemes() -> impl Iterator<Item = ColorScheme> {
|
||||
[Dark, Light].into_iter()
|
||||
}
|
||||
}
|
@@ -360,10 +360,10 @@ pub struct EscapeSequenceOffsetsIterator<'a> {
|
||||
|
||||
impl<'a> EscapeSequenceOffsetsIterator<'a> {
|
||||
pub fn new(text: &'a str) -> EscapeSequenceOffsetsIterator<'a> {
|
||||
return EscapeSequenceOffsetsIterator {
|
||||
EscapeSequenceOffsetsIterator {
|
||||
text,
|
||||
chars: text.char_indices().peekable(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes values from the iterator while the predicate returns true.
|
||||
@@ -539,7 +539,7 @@ impl<'a> EscapeSequenceOffsetsIterator<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for EscapeSequenceOffsetsIterator<'a> {
|
||||
impl Iterator for EscapeSequenceOffsetsIterator<'_> {
|
||||
type Item = EscapeSequenceOffsets;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.chars.peek() {
|
||||
@@ -564,10 +564,10 @@ pub struct EscapeSequenceIterator<'a> {
|
||||
|
||||
impl<'a> EscapeSequenceIterator<'a> {
|
||||
pub fn new(text: &'a str) -> EscapeSequenceIterator<'a> {
|
||||
return EscapeSequenceIterator {
|
||||
EscapeSequenceIterator {
|
||||
text,
|
||||
offset_iter: EscapeSequenceOffsetsIterator::new(text),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user