bat_impl/
config.rs

1use crate::line_range::{HighlightedLineRanges, LineRanges};
2#[cfg(feature = "paging")]
3use crate::paging::PagingMode;
4use crate::style::StyleComponents;
5use crate::syntax_mapping::SyntaxMapping;
6use crate::wrapping::WrappingMode;
7
8#[derive(Debug, Clone)]
9pub enum VisibleLines {
10    /// Show all lines which are included in the line ranges
11    Ranges(LineRanges),
12
13    #[cfg(feature = "git")]
14    /// Only show lines surrounding added/deleted/modified lines
15    DiffContext(usize),
16}
17
18impl VisibleLines {
19    pub fn diff_mode(&self) -> bool {
20        match self {
21            Self::Ranges(_) => false,
22            #[cfg(feature = "git")]
23            Self::DiffContext(_) => true,
24        }
25    }
26}
27
28impl Default for VisibleLines {
29    fn default() -> Self {
30        VisibleLines::Ranges(LineRanges::default())
31    }
32}
33
34#[derive(Debug, Clone, Default)]
35pub struct Config<'a> {
36    /// The explicitly configured language, if any
37    pub language: Option<&'a str>,
38
39    /// Whether or not to show/replace non-printable characters like space, tab and newline.
40    pub show_nonprintable: bool,
41
42    /// The character width of the terminal
43    pub term_width: usize,
44
45    /// The width of tab characters.
46    /// Currently, a value of 0 will cause tabs to be passed through without expanding them.
47    pub tab_width: usize,
48
49    /// Whether or not to simply loop through all input (`cat` mode)
50    pub loop_through: bool,
51
52    /// Whether or not the output should be colorized
53    pub colored_output: bool,
54
55    /// Whether or not the output terminal supports true color
56    pub true_color: bool,
57
58    /// Style elements (grid, line numbers, ...)
59    pub style_components: StyleComponents,
60
61    /// If and how text should be wrapped
62    pub wrapping_mode: WrappingMode,
63
64    /// Pager or STDOUT
65    #[cfg(feature = "paging")]
66    pub paging_mode: PagingMode,
67
68    /// Specifies which lines should be printed
69    pub visible_lines: VisibleLines,
70
71    /// The syntax highlighting theme
72    pub theme: String,
73
74    /// File extension/name mappings
75    pub syntax_mapping: SyntaxMapping<'a>,
76
77    /// Command to start the pager
78    pub pager: Option<&'a str>,
79
80    /// Whether or not to use ANSI italics
81    pub use_italic_text: bool,
82
83    /// Ranges of lines which should be highlighted with a special background color
84    pub highlighted_lines: HighlightedLineRanges,
85
86    /// Whether or not to allow custom assets. If this is false or if custom assets (a.k.a.
87    /// cached assets) are not available, assets from the binary will be used instead.
88    pub use_custom_assets: bool,
89}
90
91#[cfg(feature = "paging")]
92pub fn get_pager_executable(config_pager: Option<&str>) -> Option<String> {
93    crate::pager::get_pager(config_pager)
94        .ok()
95        .flatten()
96        .map(|pager| pager.bin)
97}
98
99#[test]
100fn default_config_should_include_all_lines() {
101    use crate::line_range::RangeCheckResult;
102
103    assert_eq!(LineRanges::default().check(17), RangeCheckResult::InRange);
104}
105
106#[test]
107fn default_config_should_highlight_no_lines() {
108    use crate::line_range::RangeCheckResult;
109
110    assert_ne!(
111        Config::default().highlighted_lines.0.check(17),
112        RangeCheckResult::InRange
113    );
114}