Introduce bat_warning! helper macro

This macro is intended to be package-internal and is not to be
considered part of the public lib API.

Use it in three places to reduce code duplication. However, main reason
for this refactoring is to allow us to fix #1063 without duplicating the
code yet another time.

The macro can also be used for the "Binary content from {} will not be
printed to the terminal" message if that message starts to use eprintln!
instead (if ever).

To trigger/verify the changed code, the following commands can be used:

    cargo run -- --theme=ansi-light tests/examples/single-line.txt
    cargo run -- --theme=does-not-exist tests/examples/single-line.txt
    cargo run -- --style=grid,rule tests/examples/single-line.txt
This commit is contained in:
Martin Nordholts 2020-12-27 22:51:24 +01:00 committed by David Peter
parent b149ea91dd
commit 47bb4a9c0f
4 changed files with 14 additions and 17 deletions

View File

@ -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};
@ -190,21 +191,11 @@ impl HighlightingAssets {
Some(theme) => theme,
None => {
if theme == "ansi-light" || theme == "ansi-dark" {
use ansi_term::Colour::Yellow;
eprintln!(
"{}: Theme '{}' is deprecated, using 'ansi' instead.",
Yellow.paint("[bat warning]"),
theme
);
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())]
}

View File

@ -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,
@ -322,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)

View File

@ -19,6 +19,8 @@
//! .unwrap();
//! ```
mod macros;
pub mod assets;
pub mod assets_metadata;
pub mod config;

7
src/macros.rs Normal file
View 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)*));
})
}