mirror of
https://github.com/sharkdp/bat
synced 2024-11-16 21:25:56 +00:00
update
This commit is contained in:
parent
3b1dcca5d0
commit
eaf0b99d25
37
src/app.rs
37
src/app.rs
@ -282,7 +282,7 @@ impl App {
|
|||||||
term_width: Term::stdout().size().1 as usize,
|
term_width: Term::stdout().size().1 as usize,
|
||||||
files,
|
files,
|
||||||
theme: self.matches.value_of("theme"),
|
theme: self.matches.value_of("theme"),
|
||||||
line_range: self.matches.value_of("line-range").map(LineRange::from),
|
line_range: transpose(self.matches.value_of("line-range").map(LineRange::from))?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,12 +351,12 @@ pub struct LineRange {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl LineRange {
|
impl LineRange {
|
||||||
pub fn from(range_raw: &str) -> LineRange {
|
pub fn from(range_raw: &str) -> Result<LineRange> {
|
||||||
LineRange::parse_range(range_raw).unwrap_or(LineRange::new())
|
LineRange::parse_range(range_raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> LineRange {
|
pub fn new() -> LineRange {
|
||||||
LineRange{
|
LineRange {
|
||||||
lower: usize::min_value(),
|
lower: usize::min_value(),
|
||||||
upper: usize::max_value(),
|
upper: usize::max_value(),
|
||||||
}
|
}
|
||||||
@ -365,10 +365,10 @@ impl LineRange {
|
|||||||
pub fn parse_range(range_raw: &str) -> Result<LineRange> {
|
pub fn parse_range(range_raw: &str) -> Result<LineRange> {
|
||||||
let mut new_range = LineRange::new();
|
let mut new_range = LineRange::new();
|
||||||
|
|
||||||
if range_raw.bytes().nth(0).ok_or("No first byte")? == b':' {
|
if range_raw.bytes().nth(0).ok_or("Empty line range")? == b':' {
|
||||||
new_range.upper = range_raw[1..].parse()?;
|
new_range.upper = range_raw[1..].parse()?;
|
||||||
return Ok(new_range);
|
return Ok(new_range);
|
||||||
} else if range_raw.bytes().last().ok_or("No last byte")? == b':' {
|
} else if range_raw.bytes().last().ok_or("Empty line range")? == b':' {
|
||||||
new_range.lower = range_raw[..range_raw.len() - 1].parse()?;
|
new_range.lower = range_raw[..range_raw.len() - 1].parse()?;
|
||||||
return Ok(new_range);
|
return Ok(new_range);
|
||||||
}
|
}
|
||||||
@ -377,28 +377,45 @@ impl LineRange {
|
|||||||
if line_numbers.len() == 2 {
|
if line_numbers.len() == 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 = line_numbers[1].parse()?;
|
||||||
|
return Ok(new_range);
|
||||||
}
|
}
|
||||||
Ok(new_range)
|
Err("expected single ':' character".into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_line_range_full() {
|
fn test_parse_line_range_full() {
|
||||||
let range = LineRange::from(Some("40:50")).expect("Shouldn't fail on test!");
|
let range = LineRange::from("40:50").expect("Shouldn't fail on test!");
|
||||||
assert_eq!(40, range.lower);
|
assert_eq!(40, range.lower);
|
||||||
assert_eq!(50, range.upper);
|
assert_eq!(50, range.upper);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_line_range_partial_min() {
|
fn test_parse_line_range_partial_min() {
|
||||||
let range = LineRange::from(Some(":50")).expect("Shouldn't fail on test!");
|
let range = LineRange::from(":50").expect("Shouldn't fail on test!");
|
||||||
assert_eq!(usize::min_value(), range.lower);
|
assert_eq!(usize::min_value(), range.lower);
|
||||||
assert_eq!(50, range.upper);
|
assert_eq!(50, range.upper);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_line_range_partial_max() {
|
fn test_parse_line_range_partial_max() {
|
||||||
let range = LineRange::from(Some("40:")).expect("Shouldn't fail on test!");
|
let range = LineRange::from("40:").expect("Shouldn't fail on test!");
|
||||||
assert_eq!(40, range.lower);
|
assert_eq!(40, range.lower);
|
||||||
assert_eq!(usize::max_value(), range.upper);
|
assert_eq!(usize::max_value(), range.upper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_line_range_fail() {
|
||||||
|
let range = LineRange::from("40:50:80");
|
||||||
|
assert!(range.is_err());
|
||||||
|
let range = LineRange::from("40::80");
|
||||||
|
assert!(range.is_err());
|
||||||
|
let range = LineRange::from(":40:");
|
||||||
|
assert!(range.is_err());
|
||||||
|
let range = LineRange::from("40");
|
||||||
|
assert!(range.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transpose<T>(opt: Option<Result<T>>) -> Result<Option<T>> {
|
||||||
|
opt.map_or(Ok(None), |res| res.map(Some))
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use ansi_term::Colour::Green;
|
use ansi_term::Colour::Green;
|
||||||
use app::{Config,LineRange};
|
use app::{Config, LineRange};
|
||||||
use assets::HighlightingAssets;
|
use assets::HighlightingAssets;
|
||||||
use diff::get_git_diff;
|
use diff::get_git_diff;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
@ -94,15 +94,7 @@ fn print_file(
|
|||||||
let highlighter = HighlightLines::new(syntax, theme);
|
let highlighter = HighlightLines::new(syntax, theme);
|
||||||
|
|
||||||
printer.print_header(filename)?;
|
printer.print_header(filename)?;
|
||||||
|
print_file_ranges(printer, reader, highlighter, &printer.config.line_range)?;
|
||||||
match printer.config.line_range.as_ref() {
|
|
||||||
Some(range) => {
|
|
||||||
print_file_ranges(printer, reader, highlighter, range)?;
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
print_file_no_ranges(printer, reader, highlighter)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printer.print_footer()?;
|
printer.print_footer()?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -112,8 +104,8 @@ fn print_file_ranges<'a>(
|
|||||||
printer: &mut Printer,
|
printer: &mut Printer,
|
||||||
mut reader: Box<BufRead + 'a>,
|
mut reader: Box<BufRead + 'a>,
|
||||||
mut highlighter: HighlightLines,
|
mut highlighter: HighlightLines,
|
||||||
ranges: &LineRange
|
line_ranges: &Option<LineRange>,
|
||||||
) -> Result<()>{
|
) -> Result<()> {
|
||||||
let mut buffer = Vec::new();
|
let mut buffer = Vec::new();
|
||||||
|
|
||||||
while reader.read_until(b'\n', &mut buffer)? > 0 {
|
while reader.read_until(b'\n', &mut buffer)? > 0 {
|
||||||
@ -121,36 +113,24 @@ fn print_file_ranges<'a>(
|
|||||||
let line = String::from_utf8_lossy(&buffer);
|
let line = String::from_utf8_lossy(&buffer);
|
||||||
let regions = highlighter.highlight(line.as_ref());
|
let regions = highlighter.highlight(line.as_ref());
|
||||||
|
|
||||||
if printer.line_number + 1 < ranges.lower {
|
match line_ranges {
|
||||||
// skip line
|
&Some(ref range) => {
|
||||||
printer.line_number += 1;
|
if printer.line_number + 1 < range.lower {
|
||||||
} else if printer.line_number >= ranges.upper {
|
// skip line
|
||||||
// no more lines in range
|
printer.line_number += 1;
|
||||||
break;
|
} else if printer.line_number >= range.upper {
|
||||||
} else {
|
// no more lines in range
|
||||||
printer.print_line(®ions)?;
|
break;
|
||||||
|
} else {
|
||||||
|
printer.print_line(®ions)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
&None => {
|
||||||
|
printer.print_line(®ions)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_file_no_ranges<'a>(
|
|
||||||
printer: &mut Printer,
|
|
||||||
mut reader: Box<BufRead + 'a>,
|
|
||||||
mut highlighter: HighlightLines
|
|
||||||
) -> Result<()>{
|
|
||||||
let mut buffer = Vec::new();
|
|
||||||
|
|
||||||
while reader.read_until(b'\n', &mut buffer)? > 0 {
|
|
||||||
{
|
|
||||||
let line = String::from_utf8_lossy(&buffer);
|
|
||||||
let regions = highlighter.highlight(line.as_ref());
|
|
||||||
printer.print_line(®ions)?;
|
|
||||||
}
|
|
||||||
buffer.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user