From 9b8ddb24d1de32f82c8da1ff04967ee47a544533 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sat, 21 Mar 2020 19:40:13 +0100 Subject: [PATCH] move Config struct to separate file --- examples/simple.rs | 2 +- src/bin/bat/app.rs | 2 +- src/bin/bat/main.rs | 2 +- src/config.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++ src/controller.rs | 2 +- src/lib.rs | 94 +-------------------------------------------- src/output.rs | 2 +- src/printer.rs | 2 +- 8 files changed, 99 insertions(+), 99 deletions(-) create mode 100644 src/config.rs diff --git a/examples/simple.rs b/examples/simple.rs index c3fcd0b9..9b26efa3 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -3,7 +3,7 @@ use bat::{ controller::Controller, inputfile::InputFile, style::{OutputComponent, OutputComponents}, - Config, + config::Config, }; use console::Term; use std::process; diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index aedc9d58..4c92d7a9 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -23,7 +23,7 @@ use bat::{ line_range::{HighlightedLineRanges, LineRange, LineRanges}, style::{OutputComponent, OutputComponents, OutputWrap}, syntax_mapping::SyntaxMapping, - Config, PagingMode, + config::{Config, PagingMode}, }; fn is_truecolor_terminal() -> bool { diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 9fe95c78..66c3a411 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -32,7 +32,7 @@ use bat::{ errors::*, inputfile::InputFile, style::{OutputComponent, OutputComponents}, - Config, + config::Config, }; fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> { diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 00000000..de2a2619 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,92 @@ +use crate::inputfile::InputFile; +use crate::line_range::{HighlightedLineRanges, LineRanges}; +use crate::style::{OutputComponents, OutputWrap}; +use crate::syntax_mapping::SyntaxMapping; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum PagingMode { + Always, + QuitIfOneScreen, + Never, +} + +impl Default for PagingMode { + fn default() -> Self { + PagingMode::Never + } +} + +#[derive(Debug, Clone, Default)] +pub struct Config<'a> { + /// List of files to print + pub files: Vec>, + + /// The explicitly configured language, if any + pub language: Option<&'a str>, + + /// Whether or not to show/replace non-printable characters like space, tab and newline. + pub show_nonprintable: bool, + + /// The character width of the terminal + pub term_width: usize, + + /// The width of tab characters. + /// Currently, a value of 0 will cause tabs to be passed through without expanding them. + pub tab_width: usize, + + /// Whether or not to simply loop through all input (`cat` mode) + pub loop_through: bool, + + /// Whether or not the output should be colorized + pub colored_output: bool, + + /// Whether or not the output terminal supports true color + pub true_color: bool, + + /// Style elements (grid, line numbers, ...) + pub output_components: OutputComponents, + + /// Text wrapping mode + pub output_wrap: OutputWrap, + + /// Pager or STDOUT + pub paging_mode: PagingMode, + + /// Specifies the lines that should be printed + pub line_ranges: LineRanges, + + /// The syntax highlighting theme + pub theme: String, + + /// File extension/name mappings + pub syntax_mapping: SyntaxMapping, + + /// Command to start the pager + pub pager: Option<&'a str>, + + /// Whether or not to use ANSI italics + pub use_italic_text: bool, + + /// Ranges of lines which should be highlighted with a special background color + pub highlighted_lines: HighlightedLineRanges, +} + +#[test] +fn default_config_should_include_all_lines() { + use crate::line_range::RangeCheckResult; + + assert_eq!( + Config::default().line_ranges.check(17), + RangeCheckResult::InRange + ); +} + +#[test] +fn default_config_should_highlight_no_lines() { + use crate::line_range::RangeCheckResult; + + assert_ne!( + Config::default().highlighted_lines.0.check(17), + RangeCheckResult::InRange + ); +} diff --git a/src/controller.rs b/src/controller.rs index 0c2e244b..5b418a98 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -7,7 +7,7 @@ use crate::inputfile::{InputFile, InputFileReader}; use crate::line_range::{LineRanges, RangeCheckResult}; use crate::output::OutputType; use crate::printer::{InteractivePrinter, Printer, SimplePrinter}; -use crate::{Config, PagingMode}; +use crate::config::{Config, PagingMode}; pub struct Controller<'a> { config: &'a Config<'a>, diff --git a/src/lib.rs b/src/lib.rs index e9bc309a..52856576 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ extern crate syntect; extern crate wild; pub mod assets; +pub mod config; pub mod controller; mod decorations; mod diff; @@ -26,96 +27,3 @@ mod printer; pub mod style; pub mod syntax_mapping; mod terminal; - -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum PagingMode { - Always, - QuitIfOneScreen, - Never, -} - -impl Default for PagingMode { - fn default() -> Self { - PagingMode::Never - } -} - -use inputfile::InputFile; -use line_range::{HighlightedLineRanges, LineRanges}; -use style::{OutputComponents, OutputWrap}; -use syntax_mapping::SyntaxMapping; - -#[derive(Debug, Clone, Default)] -pub struct Config<'a> { - /// List of files to print - pub files: Vec>, - - /// The explicitly configured language, if any - pub language: Option<&'a str>, - - /// Whether or not to show/replace non-printable characters like space, tab and newline. - pub show_nonprintable: bool, - - /// The character width of the terminal - pub term_width: usize, - - /// The width of tab characters. - /// Currently, a value of 0 will cause tabs to be passed through without expanding them. - pub tab_width: usize, - - /// Whether or not to simply loop through all input (`cat` mode) - pub loop_through: bool, - - /// Whether or not the output should be colorized - pub colored_output: bool, - - /// Whether or not the output terminal supports true color - pub true_color: bool, - - /// Style elements (grid, line numbers, ...) - pub output_components: OutputComponents, - - /// Text wrapping mode - pub output_wrap: OutputWrap, - - /// Pager or STDOUT - pub paging_mode: PagingMode, - - /// Specifies the lines that should be printed - pub line_ranges: LineRanges, - - /// The syntax highlighting theme - pub theme: String, - - /// File extension/name mappings - pub syntax_mapping: SyntaxMapping, - - /// Command to start the pager - pub pager: Option<&'a str>, - - /// Whether or not to use ANSI italics - pub use_italic_text: bool, - - /// Ranges of lines which should be highlighted with a special background color - pub highlighted_lines: HighlightedLineRanges, -} - -#[test] -fn default_config_should_include_all_lines() { - use line_range::RangeCheckResult; - - assert_eq!( - Config::default().line_ranges.check(17), - RangeCheckResult::InRange - ); -} - -#[test] -fn default_config_should_highlight_no_lines() { - use line_range::RangeCheckResult; - - assert_ne!( - Config::default().highlighted_lines.0.check(17), - RangeCheckResult::InRange - ); -} diff --git a/src/output.rs b/src/output.rs index 081cb160..3e82c494 100644 --- a/src/output.rs +++ b/src/output.rs @@ -8,7 +8,7 @@ use shell_words; use crate::errors::*; use crate::less::retrieve_less_version; -use crate::PagingMode; +use crate::config::PagingMode; #[derive(Debug)] pub enum OutputType { diff --git a/src/printer.rs b/src/printer.rs index dc07974b..9e36459f 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -31,7 +31,7 @@ use crate::line_range::RangeCheckResult; use crate::preprocessor::{expand_tabs, replace_nonprintable}; use crate::style::OutputWrap; use crate::terminal::{as_terminal_escaped, to_ansi_color}; -use crate::Config; +use crate::config::Config; pub trait Printer { fn print_header(&mut self, handle: &mut dyn Write, file: InputFile) -> Result<()>;