From 47bb4a9c0f3e8cf5af92ce5741f9f0c3d810cf45 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Sun, 27 Dec 2020 22:51:24 +0100 Subject: [PATCH] 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 --- src/assets.rs | 15 +++------------ src/bin/bat/app.rs | 7 ++----- src/lib.rs | 2 ++ src/macros.rs | 7 +++++++ 4 files changed, 14 insertions(+), 17 deletions(-) create mode 100644 src/macros.rs diff --git a/src/assets.rs b/src/assets.rs index 734c6d20..5e0c0644 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -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())] } diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index c7b9d81d..3d0df0ec 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -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) diff --git a/src/lib.rs b/src/lib.rs index 6d6dd206..d0d1fe0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,8 @@ //! .unwrap(); //! ``` +mod macros; + pub mod assets; pub mod assets_metadata; pub mod config; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 00000000..beb63ea7 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,7 @@ +#[macro_export] +macro_rules! bat_warning { + ($($arg:tt)*) => ({ + use ansi_term::Colour::Yellow; + eprintln!("{}: {}", Yellow.paint("[bat warning]"), format!($($arg)*)); + }) +}