mirror of
				https://github.com/sharkdp/bat.git
				synced 2025-10-31 07:04:04 +00:00 
			
		
		
		
	Restructure and fix bug in line range module
This commit is contained in:
		| @@ -20,7 +20,7 @@ use bat::{ | |||||||
|     assets::BAT_THEME_DEFAULT, |     assets::BAT_THEME_DEFAULT, | ||||||
|     errors::*, |     errors::*, | ||||||
|     inputfile::InputFile, |     inputfile::InputFile, | ||||||
|     line_range::{LineRange, LineRanges}, |     line_range::{HighlightedLineRanges, LineRange, LineRanges}, | ||||||
|     style::{OutputComponent, OutputComponents, OutputWrap}, |     style::{OutputComponent, OutputComponents, OutputWrap}, | ||||||
|     syntax_mapping::SyntaxMapping, |     syntax_mapping::SyntaxMapping, | ||||||
|     Config, PagingMode, |     Config, PagingMode, | ||||||
| @@ -201,13 +201,14 @@ impl App { | |||||||
|                     } |                     } | ||||||
|                 }) |                 }) | ||||||
|                 .unwrap_or_else(|| String::from(BAT_THEME_DEFAULT)), |                 .unwrap_or_else(|| String::from(BAT_THEME_DEFAULT)), | ||||||
|             line_ranges: LineRanges::from( |             line_ranges: | ||||||
|                 self.matches |                 self.matches | ||||||
|                     .values_of("line-range") |                     .values_of("line-range") | ||||||
|                     .map(|vs| vs.map(LineRange::from).collect()) |                     .map(|vs| vs.map(LineRange::from).collect()) | ||||||
|                     .transpose()? |                     .transpose()? | ||||||
|                     .unwrap_or_else(|| vec![]), |                     .map(LineRanges::from) | ||||||
|             ), |                     .unwrap_or_default() | ||||||
|  |             , | ||||||
|             output_components, |             output_components, | ||||||
|             syntax_mapping, |             syntax_mapping, | ||||||
|             pager: self.matches.value_of("pager"), |             pager: self.matches.value_of("pager"), | ||||||
| @@ -215,13 +216,14 @@ impl App { | |||||||
|                 Some("always") => true, |                 Some("always") => true, | ||||||
|                 _ => false, |                 _ => false, | ||||||
|             }, |             }, | ||||||
|             highlight_lines: LineRanges::from( |             highlighted_lines: | ||||||
|                 self.matches |                 self.matches | ||||||
|                     .values_of("highlight-line") |                     .values_of("highlight-line") | ||||||
|                     .map(|ws| ws.map(LineRange::from).collect()) |                     .map(|ws| ws.map(LineRange::from).collect()) | ||||||
|                     .transpose()? |                     .transpose()? | ||||||
|                     .unwrap_or_else(|| vec![LineRange { lower: 0, upper: 0 }]), |                     .map(LineRanges::from) | ||||||
|             ), |                     .map(|lr| HighlightedLineRanges(lr)) | ||||||
|  |                     .unwrap_or_default() | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										20
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								src/lib.rs
									
									
									
									
									
								
							| @@ -68,7 +68,7 @@ impl Default for PagingMode { | |||||||
| } | } | ||||||
|  |  | ||||||
| use inputfile::InputFile; | use inputfile::InputFile; | ||||||
| use line_range::LineRanges; | use line_range::{LineRanges, HighlightedLineRanges}; | ||||||
| use style::{OutputComponents, OutputWrap}; | use style::{OutputComponents, OutputWrap}; | ||||||
| use syntax_mapping::SyntaxMapping; | use syntax_mapping::SyntaxMapping; | ||||||
|  |  | ||||||
| @@ -123,6 +123,20 @@ pub struct Config<'a> { | |||||||
|     /// Whether or not to use ANSI italics |     /// Whether or not to use ANSI italics | ||||||
|     pub use_italic_text: bool, |     pub use_italic_text: bool, | ||||||
|  |  | ||||||
|     /// Lines to highlight |     /// Ranges of lines which should be highlighted with a special background color | ||||||
|     pub highlight_lines: LineRanges, |     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); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -111,13 +111,15 @@ pub struct LineRanges { | |||||||
|     largest_upper_bound: usize, |     largest_upper_bound: usize, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl Default for LineRanges { | impl LineRanges { | ||||||
|     fn default() -> Self { |     pub fn none() -> LineRanges { | ||||||
|         LineRanges::from(vec![LineRange { lower: 0, upper: 0 }]) |         LineRanges::from(vec![]) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     pub fn all() -> LineRanges { | ||||||
|  |         LineRanges::from(vec![LineRange::default()]) | ||||||
|     } |     } | ||||||
|  |  | ||||||
| impl LineRanges { |  | ||||||
|     pub fn from(ranges: Vec<LineRange>) -> LineRanges { |     pub fn from(ranges: Vec<LineRange>) -> LineRanges { | ||||||
|         let largest_upper_bound = ranges |         let largest_upper_bound = ranges | ||||||
|             .iter() |             .iter() | ||||||
| @@ -131,7 +133,7 @@ impl LineRanges { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub fn check(&self, line: usize) -> RangeCheckResult { |     pub fn check(&self, line: usize) -> RangeCheckResult { | ||||||
|         if self.ranges.is_empty() || self.ranges.iter().any(|r| r.is_inside(line)) { |         if self.ranges.iter().any(|r| r.is_inside(line)) { | ||||||
|             RangeCheckResult::InRange |             RangeCheckResult::InRange | ||||||
|         } else if line < self.largest_upper_bound { |         } else if line < self.largest_upper_bound { | ||||||
|             RangeCheckResult::BeforeOrBetweenRanges |             RangeCheckResult::BeforeOrBetweenRanges | ||||||
| @@ -141,6 +143,23 @@ impl LineRanges { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | impl Default for LineRanges { | ||||||
|  |     fn default() -> Self { | ||||||
|  |         Self::all() | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /// Similar to LineRanges, but "empty" by default | ||||||
|  | #[derive(Debug, Clone)] | ||||||
|  | pub struct HighlightedLineRanges(pub LineRanges); | ||||||
|  |  | ||||||
|  | impl Default for HighlightedLineRanges { | ||||||
|  |     fn default() -> Self { | ||||||
|  |         HighlightedLineRanges(LineRanges::none()) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| fn ranges(rs: &[&str]) -> LineRanges { | fn ranges(rs: &[&str]) -> LineRanges { | ||||||
|     LineRanges::from(rs.iter().map(|r| LineRange::from(r).unwrap()).collect()) |     LineRanges::from(rs.iter().map(|r| LineRange::from(r).unwrap()).collect()) | ||||||
| @@ -189,8 +208,15 @@ fn test_ranges_open_high() { | |||||||
| } | } | ||||||
|  |  | ||||||
| #[test] | #[test] | ||||||
| fn test_ranges_empty() { | fn test_ranges_all() { | ||||||
|     let ranges = ranges(&[]); |     let ranges = LineRanges::all(); | ||||||
|  |  | ||||||
|     assert_eq!(RangeCheckResult::InRange, ranges.check(1)); |     assert_eq!(RangeCheckResult::InRange, ranges.check(1)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #[test] | ||||||
|  | fn test_ranges_none() { | ||||||
|  |     let ranges = LineRanges::none(); | ||||||
|  |  | ||||||
|  |     assert_ne!(RangeCheckResult::InRange, ranges.check(1)); | ||||||
|  | } | ||||||
|   | |||||||
| @@ -376,7 +376,7 @@ impl<'a> Printer for InteractivePrinter<'a> { | |||||||
|  |  | ||||||
|         // Line highlighting |         // Line highlighting | ||||||
|         let highlight_this_line = |         let highlight_this_line = | ||||||
|             self.config.highlight_lines.check(line_number) == RangeCheckResult::InRange; |             self.config.highlighted_lines.0.check(line_number) == RangeCheckResult::InRange; | ||||||
|  |  | ||||||
|         let background_color = self |         let background_color = self | ||||||
|             .background_color_highlight |             .background_color_highlight | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user