on 9ad401b help sign in
bat/src/config.rs
111 lines 3.0 kB history raw
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;
8#[derive(Debug, Clone)]
9pub enum VisibleLines {
10 /// Show all lines which are included in the line ranges
11 Ranges(LineRanges),
13 #[cfg(feature = "git")]
14 /// Only show lines surrounding added/deleted/modified lines
15 DiffContext(usize),
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 }
28impl Default for VisibleLines {
29 fn default() -> Self {
30 VisibleLines::Ranges(LineRanges::default())
31 }
34#[derive(Debug, Clone, Default)]
35pub struct Config<'a> {
36 /// The explicitly configured language, if any
37 pub language: Option<&'a str>,
39 /// Whether or not to show/replace non-printable characters like space, tab and newline.
40 pub show_nonprintable: bool,
42 /// The character width of the terminal
43 pub term_width: usize,
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,
49 /// Whether or not to simply loop through all input (`cat` mode)
50 pub loop_through: bool,
52 /// Whether or not the output should be colorized
53 pub colored_output: bool,
55 /// Whether or not the output terminal supports true color
56 pub true_color: bool,
58 /// Style elements (grid, line numbers, ...)
59 pub style_components: StyleComponents,
61 /// If and how text should be wrapped
62 pub wrapping_mode: WrappingMode,
64 /// Pager or STDOUT
65 #[cfg(feature = "paging")]
66 pub paging_mode: PagingMode,
68 /// Specifies which lines should be printed
69 pub visible_lines: VisibleLines,
71 /// The syntax highlighting theme
72 pub theme: String,
74 /// File extension/name mappings
75 pub syntax_mapping: SyntaxMapping<'a>,
77 /// Command to start the pager
78 pub pager: Option<&'a str>,
80 /// Whether or not to use ANSI italics
81 pub use_italic_text: bool,
83 /// Ranges of lines which should be highlighted with a special background color
84 pub highlighted_lines: HighlightedLineRanges,
87#[cfg(all(feature = "application", feature = "paging"))]
88pub fn get_pager_executable(config_pager: Option<&str>) -> Option<String> {
89 if let Ok(Some(pager)) = crate::pager::get_pager(config_pager) {
90 Some(pager.bin)
91 } else {
92 None
93 }
96#[test]
97fn default_config_should_include_all_lines() {
98 use crate::line_range::RangeCheckResult;
100 assert_eq!(LineRanges::default().check(17), RangeCheckResult::InRange);
103#[test]
104fn default_config_should_highlight_no_lines() {
105 use crate::line_range::RangeCheckResult;
107 assert_ne!(
108 Config::default().highlighted_lines.0.check(17),
109 RangeCheckResult::InRange
110 );