mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-01 19:02:22 +01:00
Merge remote-tracking branch 'origin/master' into fix-1063
This commit is contained in:
@@ -11,6 +11,7 @@ use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
|
||||
use path_abs::PathAbs;
|
||||
|
||||
use crate::assets_metadata::AssetsMetadata;
|
||||
use crate::bat_warning;
|
||||
use crate::error::*;
|
||||
use crate::input::{InputReader, OpenedInput, OpenedInputKind};
|
||||
use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
|
||||
@@ -189,13 +190,12 @@ impl HighlightingAssets {
|
||||
match self.theme_set.themes.get(theme) {
|
||||
Some(theme) => theme,
|
||||
None => {
|
||||
if theme == "ansi-light" || theme == "ansi-dark" {
|
||||
bat_warning!("Theme '{}' is deprecated, using 'ansi' instead.", theme);
|
||||
return self.get_theme("ansi");
|
||||
}
|
||||
if theme != "" {
|
||||
use ansi_term::Colour::Yellow;
|
||||
eprintln!(
|
||||
"{}: Unknown theme '{}', using default.",
|
||||
Yellow.paint("[bat warning]"),
|
||||
theme
|
||||
);
|
||||
bat_warning!("Unknown theme '{}', using default.", theme)
|
||||
}
|
||||
&self.theme_set.themes[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ use console::Term;
|
||||
use crate::input::{new_file_input, new_stdin_input};
|
||||
use bat::{
|
||||
assets::HighlightingAssets,
|
||||
bat_warning,
|
||||
config::{Config, VisibleLines},
|
||||
error::*,
|
||||
input::Input,
|
||||
@@ -82,10 +83,9 @@ impl App {
|
||||
Some("always") => PagingMode::Always,
|
||||
Some("never") => PagingMode::Never,
|
||||
Some("auto") | None => {
|
||||
if self.matches.occurrences_of("plain") > 1 {
|
||||
// If we have -pp as an option when in auto mode, the pager should be disabled.
|
||||
PagingMode::Never
|
||||
} else if self.matches.is_present("no-paging") {
|
||||
// If we have -pp as an option when in auto mode, the pager should be disabled.
|
||||
let extra_plain = self.matches.occurrences_of("plain") > 1;
|
||||
if extra_plain || self.matches.is_present("no-paging") {
|
||||
PagingMode::Never
|
||||
} else if inputs.iter().any(Input::is_stdin) {
|
||||
// If we are reading from stdin, only enable paging if we write to an
|
||||
@@ -323,11 +323,7 @@ impl App {
|
||||
|
||||
// If `grid` is set, remove `rule` as it is a subset of `grid`, and print a warning.
|
||||
if styled_components.grid() && styled_components.0.remove(&StyleComponent::Rule) {
|
||||
use ansi_term::Colour::Yellow;
|
||||
eprintln!(
|
||||
"{}: Style 'rule' is a subset of style 'grid', 'rule' will not be visible.",
|
||||
Yellow.paint("[bat warning]"),
|
||||
);
|
||||
bat_warning!("Style 'rule' is a subset of style 'grid', 'rule' will not be visible.");
|
||||
}
|
||||
|
||||
Ok(styled_components)
|
||||
|
@@ -36,6 +36,7 @@ use bat::{
|
||||
input::Input,
|
||||
style::{StyleComponent, StyleComponents},
|
||||
MappingTarget,
|
||||
PagingMode,
|
||||
};
|
||||
|
||||
const THEME_PREVIEW_DATA: &[u8] = include_bytes!("../../../assets/theme_preview.rs");
|
||||
@@ -78,7 +79,9 @@ fn get_syntax_mapping_to_paths<'a>(
|
||||
map
|
||||
}
|
||||
|
||||
pub fn list_languages(config: &Config) -> Result<()> {
|
||||
pub fn get_languages(config: &Config) -> Result<String> {
|
||||
let mut result: String = String::new();
|
||||
|
||||
let assets = assets_from_cache_or_binary()?;
|
||||
let mut languages = assets
|
||||
.syntaxes()
|
||||
@@ -119,12 +122,9 @@ pub fn list_languages(config: &Config) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
let stdout = io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
|
||||
if config.loop_through {
|
||||
for lang in languages {
|
||||
writeln!(stdout, "{}:{}", lang.name, lang.file_extensions.join(","))?;
|
||||
result += &format!("{}:{}\n", lang.name, lang.file_extensions.join(","));
|
||||
}
|
||||
} else {
|
||||
let longest = languages
|
||||
@@ -145,7 +145,7 @@ pub fn list_languages(config: &Config) -> Result<()> {
|
||||
};
|
||||
|
||||
for lang in languages {
|
||||
write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?;
|
||||
result += &format!("{:width$}{}", lang.name, separator, width = longest);
|
||||
|
||||
// Number of characters on this line so far, wrap before `desired_width`
|
||||
let mut num_chars = 0;
|
||||
@@ -156,20 +156,20 @@ pub fn list_languages(config: &Config) -> Result<()> {
|
||||
let new_chars = word.len() + comma_separator.len();
|
||||
if num_chars + new_chars >= desired_width {
|
||||
num_chars = 0;
|
||||
write!(stdout, "\n{:width$}{}", "", separator, width = longest)?;
|
||||
result += &format!("\n{:width$}{}", "", separator, width = longest);
|
||||
}
|
||||
|
||||
num_chars += new_chars;
|
||||
write!(stdout, "{}", style.paint(&word[..]))?;
|
||||
result += &format!("{}", style.paint(&word[..]));
|
||||
if extension.peek().is_some() {
|
||||
write!(stdout, "{}", comma_separator)?;
|
||||
result += comma_separator;
|
||||
}
|
||||
}
|
||||
writeln!(stdout)?;
|
||||
result += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn theme_preview_file<'a>() -> Input<'a> {
|
||||
@@ -200,19 +200,19 @@ pub fn list_themes(cfg: &Config) -> Result<()> {
|
||||
.ok();
|
||||
writeln!(stdout)?;
|
||||
}
|
||||
writeln!(
|
||||
stdout,
|
||||
"Further themes can be installed to '{}', \
|
||||
and are added to the cache with `bat cache --build`. \
|
||||
For more information, see:\n\n \
|
||||
https://github.com/sharkdp/bat#adding-new-themes",
|
||||
config_file().join("themes").to_string_lossy()
|
||||
)?;
|
||||
} else {
|
||||
for theme in assets.themes() {
|
||||
writeln!(stdout, "{}", theme)?;
|
||||
}
|
||||
}
|
||||
writeln!(
|
||||
stdout,
|
||||
"Further themes can be installed to '{}', \
|
||||
and are added to the cache with `bat cache --build`. \
|
||||
For more information, see:\n\n \
|
||||
https://github.com/sharkdp/bat#adding-new-themes",
|
||||
config_file().join("themes").to_string_lossy()
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -248,8 +248,14 @@ fn run() -> Result<bool> {
|
||||
let config = app.config(&inputs)?;
|
||||
|
||||
if app.matches.is_present("list-languages") {
|
||||
list_languages(&config)?;
|
||||
Ok(true)
|
||||
let languages: String = get_languages(&config)?;
|
||||
let inputs: Vec<Input> = vec![Input::from_reader(Box::new(languages.as_bytes()))];
|
||||
let plain_config = Config {
|
||||
style_components: StyleComponents::new(StyleComponent::Plain.components(false)),
|
||||
paging_mode: PagingMode::QuitIfOneScreen,
|
||||
..Default::default()
|
||||
};
|
||||
run_controller(inputs, &plain_config)
|
||||
} else if app.matches.is_present("list-themes") {
|
||||
list_themes(&config)?;
|
||||
Ok(true)
|
||||
|
@@ -197,6 +197,8 @@ impl<'b> Controller<'b> {
|
||||
let mut first_range: bool = true;
|
||||
let mut mid_range: bool = false;
|
||||
|
||||
let style_snip = self.config.style_components.snip();
|
||||
|
||||
while reader.read_line(&mut line_buffer)? {
|
||||
match line_ranges.check(line_number) {
|
||||
RangeCheckResult::BeforeOrBetweenRanges => {
|
||||
@@ -207,7 +209,7 @@ impl<'b> Controller<'b> {
|
||||
}
|
||||
|
||||
RangeCheckResult::InRange => {
|
||||
if self.config.style_components.snip() {
|
||||
if style_snip {
|
||||
if first_range {
|
||||
first_range = false;
|
||||
mid_range = true;
|
||||
|
@@ -19,6 +19,8 @@
|
||||
//! .unwrap();
|
||||
//! ```
|
||||
|
||||
mod macros;
|
||||
|
||||
pub mod assets;
|
||||
pub mod assets_metadata;
|
||||
pub mod config;
|
||||
|
7
src/macros.rs
Normal file
7
src/macros.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
#[macro_export]
|
||||
macro_rules! bat_warning {
|
||||
($($arg:tt)*) => ({
|
||||
use ansi_term::Colour::Yellow;
|
||||
eprintln!("{}: {}", Yellow.paint("[bat warning]"), format!($($arg)*));
|
||||
})
|
||||
}
|
@@ -72,7 +72,7 @@ pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
|
||||
}
|
||||
}
|
||||
// line feed
|
||||
'\x0A' => output.push('␊'),
|
||||
'\x0A' => output.push_str("␊\x0A"),
|
||||
// carriage return
|
||||
'\x0D' => output.push('␍'),
|
||||
// null
|
||||
|
@@ -91,9 +91,6 @@ impl<'a> Printer for SimplePrinter<'a> {
|
||||
if self.config.show_nonprintable {
|
||||
let line = replace_nonprintable(line_buffer, self.config.tab_width);
|
||||
write!(handle, "{}", line)?;
|
||||
if line_buffer.last() == Some(&b'\n') {
|
||||
writeln!(handle)?;
|
||||
}
|
||||
} else {
|
||||
handle.write_all(line_buffer)?
|
||||
};
|
||||
@@ -451,7 +448,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
if text.len() != text_trimmed.len() {
|
||||
if let Some(background_color) = background_color {
|
||||
let mut ansi_style = Style::default();
|
||||
ansi_style.background = Some(to_ansi_color(background_color, true_color));
|
||||
ansi_style.background = to_ansi_color(background_color, true_color);
|
||||
let width = if cursor_total <= cursor_max {
|
||||
cursor_max - cursor_total + 1
|
||||
} else {
|
||||
@@ -463,7 +460,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
if line.bytes().next_back() != Some(b'\n') {
|
||||
if !self.config.style_components.plain() && line.bytes().next_back() != Some(b'\n') {
|
||||
writeln!(handle)?;
|
||||
}
|
||||
} else {
|
||||
@@ -592,8 +589,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
||||
|
||||
if let Some(background_color) = background_color {
|
||||
let mut ansi_style = Style::default();
|
||||
ansi_style.background =
|
||||
Some(to_ansi_color(background_color, self.config.true_color));
|
||||
ansi_style.background = to_ansi_color(background_color, self.config.true_color);
|
||||
|
||||
write!(
|
||||
handle,
|
||||
@@ -627,20 +623,27 @@ impl Colors {
|
||||
}
|
||||
|
||||
fn colored(theme: &Theme, true_color: bool) -> Self {
|
||||
let gutter_color = theme
|
||||
.settings
|
||||
.gutter_foreground
|
||||
.map(|c| to_ansi_color(c, true_color))
|
||||
.unwrap_or(Fixed(DEFAULT_GUTTER_COLOR));
|
||||
let gutter_style = Style {
|
||||
foreground: match theme.settings.gutter_foreground {
|
||||
// If the theme provides a gutter foreground color, use it.
|
||||
// Note: It might be the special value #00000001, in which case
|
||||
// to_ansi_color returns None and we use an empty Style
|
||||
// (resulting in the terminal's default foreground color).
|
||||
Some(c) => to_ansi_color(c, true_color),
|
||||
// Otherwise, use a specific fallback color.
|
||||
None => Some(Fixed(DEFAULT_GUTTER_COLOR)),
|
||||
},
|
||||
..Style::default()
|
||||
};
|
||||
|
||||
Colors {
|
||||
grid: gutter_color.normal(),
|
||||
rule: gutter_color.normal(),
|
||||
grid: gutter_style,
|
||||
rule: gutter_style,
|
||||
filename: Style::new().bold(),
|
||||
git_added: Green.normal(),
|
||||
git_removed: Red.normal(),
|
||||
git_modified: Yellow.normal(),
|
||||
line_number: gutter_color.normal(),
|
||||
line_number: gutter_style,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -3,13 +3,13 @@ use ansi_term::{self, Style};
|
||||
|
||||
use syntect::highlighting::{self, FontStyle};
|
||||
|
||||
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term::Color {
|
||||
pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> Option<ansi_term::Color> {
|
||||
if color.a == 0 {
|
||||
// Themes can specify one of the user-configurable terminal colors by
|
||||
// encoding them as #RRGGBBAA with AA set to 00 (transparent) and RR set
|
||||
// to the 8-bit color palette number. The built-in themes ansi-light,
|
||||
// ansi-dark, base16, and base16-256 use this.
|
||||
match color.r {
|
||||
// to the 8-bit color palette number. The built-in themes ansi, base16,
|
||||
// and base16-256 use this.
|
||||
Some(match color.r {
|
||||
// For the first 8 colors, use the Color enum to produce ANSI escape
|
||||
// sequences using codes 30-37 (foreground) and 40-47 (background).
|
||||
// For example, red foreground is \x1b[31m. This works on terminals
|
||||
@@ -31,11 +31,18 @@ pub fn to_ansi_color(color: highlighting::Color, true_color: bool) -> ansi_term:
|
||||
// 90-97 (foreground) and 100-107 (background), we should use those
|
||||
// for values 0x08 to 0x0f and only use Fixed for 0x10 to 0xff.
|
||||
n => Fixed(n),
|
||||
}
|
||||
})
|
||||
} else if color.a == 1 {
|
||||
// Themes can specify the terminal's default foreground/background color
|
||||
// (i.e. no escape sequence) using the encoding #RRGGBBAA with AA set to
|
||||
// 01. The built-in theme ansi uses this.
|
||||
None
|
||||
} else if true_color {
|
||||
RGB(color.r, color.g, color.b)
|
||||
Some(RGB(color.r, color.g, color.b))
|
||||
} else {
|
||||
Fixed(ansi_colours::ansi256_from_rgb((color.r, color.g, color.b)))
|
||||
Some(Fixed(ansi_colours::ansi256_from_rgb((
|
||||
color.r, color.g, color.b,
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +61,10 @@ pub fn as_terminal_escaped(
|
||||
let mut style = if !colored {
|
||||
Style::default()
|
||||
} else {
|
||||
let mut color = Style::from(to_ansi_color(style.foreground, true_color));
|
||||
let mut color = Style {
|
||||
foreground: to_ansi_color(style.foreground, true_color),
|
||||
..Style::default()
|
||||
};
|
||||
if style.font_style.contains(FontStyle::BOLD) {
|
||||
color = color.bold();
|
||||
}
|
||||
@@ -67,6 +77,6 @@ pub fn as_terminal_escaped(
|
||||
color
|
||||
};
|
||||
|
||||
style.background = background_color.map(|c| to_ansi_color(c, true_color));
|
||||
style.background = background_color.and_then(|c| to_ansi_color(c, true_color));
|
||||
style.paint(text).to_string()
|
||||
}
|
||||
|
Reference in New Issue
Block a user