on master help sign in
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

commit47bb4a9c0f3e8cf5af92ce5741f9f0c3d810cf45 authorMartin Nordholts <enselic@gmail.com> dateSun 27 Dec 2020, 22:51 (6 years ago) parentb149ea9
4 files changed+14−17
src/assets.rs+3−12 src/bin/bat/app.rs+2−5 src/lib.rs+2 src/macros.rs+7
src/assets.rs+3−12
@@ -11,6 +11,7 @@ use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
1111use path_abs::PathAbs;
1212
1313use crate::assets_metadata::AssetsMetadata;
14+use crate::bat_warning;
1415use crate::error::*;
1516use crate::input::{InputReader, OpenedInput, OpenedInputKind};
1617use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
@@ -190,21 +191,11 @@ impl HighlightingAssets {
190191 Some(theme) => theme,
191192 None => {
192193 if theme == "ansi-light" || theme == "ansi-dark" {
193− use ansi_term::Colour::Yellow;
194− eprintln!(
195− "{}: Theme '{}' is deprecated, using 'ansi' instead.",
196− Yellow.paint("[bat warning]"),
197− theme
198− );
194+ bat_warning!("Theme '{}' is deprecated, using 'ansi' instead.", theme);
199195 return self.get_theme("ansi");
200196 }
201197 if theme != "" {
202− use ansi_term::Colour::Yellow;
203− eprintln!(
204− "{}: Unknown theme '{}', using default.",
205− Yellow.paint("[bat warning]"),
206− theme
207− );
198+ bat_warning!("Unknown theme '{}', using default.", theme)
208199 }
209200 &self.theme_set.themes[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
210201 }
src/bin/bat/app.rs+2−5
@@ -16,6 +16,7 @@ use console::Term;
1616use crate::input::{new_file_input, new_stdin_input};
1717use bat::{
1818 assets::HighlightingAssets,
19+ bat_warning,
1920 config::{Config, VisibleLines},
2021 error::*,
2122 input::Input,
@@ -322,11 +323,7 @@ impl App {
322323
323324 // If `grid` is set, remove `rule` as it is a subset of `grid`, and print a warning.
324325 if styled_components.grid() && styled_components.0.remove(&StyleComponent::Rule) {
325− use ansi_term::Colour::Yellow;
326− eprintln!(
327− "{}: Style 'rule' is a subset of style 'grid', 'rule' will not be visible.",
328− Yellow.paint("[bat warning]"),
329− );
326+ bat_warning!("Style 'rule' is a subset of style 'grid', 'rule' will not be visible.");
330327 }
331328
332329 Ok(styled_components)
src/lib.rs+2
@@ -19,6 +19,8 @@
1919//! .unwrap();
2020//! ```
2121
22+mod macros;
23+
2224pub mod assets;
2325pub mod assets_metadata;
2426pub mod config;
src/macros.rsnew file+7
@@ -0,0 +1,7 @@
1+#[macro_export]
2+macro_rules! bat_warning {
3+ ($($arg:tt)*) => ({
4+ use ansi_term::Colour::Yellow;
5+ eprintln!("{}: {}", Yellow.paint("[bat warning]"), format!($($arg)*));
6+ })
7+}