mirror of
				https://github.com/sharkdp/bat.git
				synced 2025-10-31 23:22:03 +00:00 
			
		
		
		
	Support for line range plus syntax
This commit is contained in:
		
				
					committed by
					
						 David Peter
						David Peter
					
				
			
			
				
	
			
			
			
						parent
						
							b3247d9364
						
					
				
				
					commit
					0748783404
				
			| @@ -3,6 +3,7 @@ | |||||||
| ## Features | ## Features | ||||||
|  |  | ||||||
| - `$BAT_CONFIG_DIR` is now a recognized environment variable. It has precedence over `$XDG_CONFIG_HOME`, see #1727 (@billrisher) | - `$BAT_CONFIG_DIR` is now a recognized environment variable. It has precedence over `$XDG_CONFIG_HOME`, see #1727 (@billrisher) | ||||||
|  | - Support for `x:+x` syntax in line ranges (e.g. `20:+10`). See  #1810 (@bojan88) | ||||||
|  |  | ||||||
| ## Bugfixes | ## Bugfixes | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								assets/manual/bat.1.in
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								assets/manual/bat.1.in
									
									
									
									
										vendored
									
									
								
							| @@ -50,6 +50,8 @@ highlights lines 30 to 40 | |||||||
| highlights lines 1 to 40 | highlights lines 1 to 40 | ||||||
| .IP "\-\-highlight\-line 40:" | .IP "\-\-highlight\-line 40:" | ||||||
| highlights lines 40 to the end of the file | highlights lines 40 to the end of the file | ||||||
|  | .IP "\-\-highlight\-line 30:+10" | ||||||
|  | highlights lines 30 to 40 | ||||||
| .RE | .RE | ||||||
| .HP | .HP | ||||||
| \fB\-\-file\-name\fR <name>... | \fB\-\-file\-name\fR <name>... | ||||||
| @@ -154,6 +156,8 @@ prints lines 30 to 40 | |||||||
| prints lines 1 to 40 | prints lines 1 to 40 | ||||||
| .IP "\-\-line\-range 40:" | .IP "\-\-line\-range 40:" | ||||||
| prints lines 40 to the end of the file | prints lines 40 to the end of the file | ||||||
|  | .IP "\-\-line\-range 30:+10" | ||||||
|  | prints lines 30 to 40 | ||||||
| .RE | .RE | ||||||
| .HP | .HP | ||||||
| \fB\-L\fR, \fB\-\-list\-languages\fR | \fB\-L\fR, \fB\-\-list\-languages\fR | ||||||
|   | |||||||
| @@ -95,7 +95,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { | |||||||
|                      '--highlight-line 40' highlights line 40\n  \ |                      '--highlight-line 40' highlights line 40\n  \ | ||||||
|                      '--highlight-line 30:40' highlights lines 30 to 40\n  \ |                      '--highlight-line 30:40' highlights lines 30 to 40\n  \ | ||||||
|                      '--highlight-line :40' highlights lines 1 to 40\n  \ |                      '--highlight-line :40' highlights lines 1 to 40\n  \ | ||||||
|                      '--highlight-line 40:' highlights lines 40 to the end of the file", |                      '--highlight-line 40:' highlights lines 40 to the end of the file\n  \ | ||||||
|  |                      '--highlight-line 30:+10' highlights lines 30 to 40", | ||||||
|                 ), |                 ), | ||||||
|         ) |         ) | ||||||
|         .arg( |         .arg( | ||||||
| @@ -423,7 +424,8 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> { | |||||||
|                      '--line-range 30:40' prints lines 30 to 40\n  \ |                      '--line-range 30:40' prints lines 30 to 40\n  \ | ||||||
|                      '--line-range :40' prints lines 1 to 40\n  \ |                      '--line-range :40' prints lines 1 to 40\n  \ | ||||||
|                      '--line-range 40:' prints lines 40 to the end of the file\n  \ |                      '--line-range 40:' prints lines 40 to the end of the file\n  \ | ||||||
|                      '--line-range 40' only prints line 40", |                      '--line-range 40' only prints line 40\n  \ | ||||||
|  |                      '--line-range 30:+10' prints lines 30 to 40", | ||||||
|                 ), |                 ), | ||||||
|         ) |         ) | ||||||
|         .arg( |         .arg( | ||||||
|   | |||||||
| @@ -47,7 +47,16 @@ impl LineRange { | |||||||
|             } |             } | ||||||
|             2 => { |             2 => { | ||||||
|                 new_range.lower = line_numbers[0].parse()?; |                 new_range.lower = line_numbers[0].parse()?; | ||||||
|                 new_range.upper = line_numbers[1].parse()?; |  | ||||||
|  |                 new_range.upper = if line_numbers[1].bytes().next().unwrap() == b'+' { | ||||||
|  |                     let more_lines = &line_numbers[1][1..] | ||||||
|  |                         .parse() | ||||||
|  |                         .map_err(|_| "Invalid line number after +")?; | ||||||
|  |                     new_range.lower + more_lines | ||||||
|  |                 } else { | ||||||
|  |                     line_numbers[1].parse()? | ||||||
|  |                 }; | ||||||
|  |  | ||||||
|                 Ok(new_range) |                 Ok(new_range) | ||||||
|             } |             } | ||||||
|             _ => Err( |             _ => Err( | ||||||
| @@ -100,6 +109,23 @@ fn test_parse_fail() { | |||||||
|     assert!(range.is_err()); |     assert!(range.is_err()); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #[test] | ||||||
|  | fn test_parse_plus() { | ||||||
|  |     let range = LineRange::from("40:+10").expect("Shouldn't fail on test!"); | ||||||
|  |     assert_eq!(40, range.lower); | ||||||
|  |     assert_eq!(50, range.upper); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #[test] | ||||||
|  | fn test_parse_plus_fail() { | ||||||
|  |     let range = LineRange::from("40:+z"); | ||||||
|  |     assert!(range.is_err()); | ||||||
|  |     let range = LineRange::from("40:+-10"); | ||||||
|  |     assert!(range.is_err()); | ||||||
|  |     let range = LineRange::from("40:+"); | ||||||
|  |     assert!(range.is_err()); | ||||||
|  | } | ||||||
|  |  | ||||||
| #[derive(Copy, Clone, Debug, PartialEq)] | #[derive(Copy, Clone, Debug, PartialEq)] | ||||||
| pub enum RangeCheckResult { | pub enum RangeCheckResult { | ||||||
|     // Within one of the given ranges |     // Within one of the given ranges | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user