add line-range

This commit is contained in:
Taylor Skinner 2018-05-31 21:08:29 -06:00 committed by David Peter
parent 50209bfe21
commit 6691786d82
3 changed files with 55 additions and 3 deletions

View File

@ -141,6 +141,17 @@ impl App {
see all available themes",
),
)
.arg(
Arg::with_name("line-range")
.long("line-range")
.overrides_with("line-range")
.takes_value(true)
.help("Print range of lines")
.long_help(
"Print a specified range of lines from the files \
--line-range 30-40 or --line-range 30-",
),
)
.arg(
Arg::with_name("list-themes")
.long("list-themes")
@ -269,6 +280,7 @@ impl App {
term_width: Term::stdout().size().1 as usize,
files,
theme: self.matches.value_of("theme"),
line_range: get_ranges(self.matches.value_of("line-range")),
})
}
@ -322,6 +334,7 @@ pub struct Config<'a> {
pub term_width: usize,
pub files: Vec<Option<&'a str>>,
pub theme: Option<&'a str>,
pub line_range: Option<(usize, usize)>,
}
fn is_truecolor_terminal() -> bool {
@ -329,3 +342,29 @@ fn is_truecolor_terminal() -> bool {
.map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
.unwrap_or(false)
}
fn get_ranges(value: Option<&str>) -> Option<(usize, usize)> {
match value {
None => None,
Some(str_range) => {
let mut new_range = (usize::min_value(), usize::max_value());
if str_range.bytes().nth(0).expect("Something should be here!") == b':' {
new_range.1 = str_range[1..].parse().expect("This should be a number!");
return Some(new_range);
} else if str_range.bytes().last().expect("There should be a last!") == b':' {
new_range.0 = str_range[..str_range.len() - 1]
.parse()
.expect("This should be a number!");
return Some(new_range);
}
let line_numbers: Vec<&str> = str_range.split(':').collect();
if line_numbers.len() == 2 {
new_range.0 = line_numbers[0].parse().expect("Should be a number!");
new_range.1 = line_numbers[1].parse().expect("Should be a number!");
}
Some(new_range)
}
}
}

View File

@ -96,12 +96,25 @@ fn print_file(
printer.print_header(filename)?;
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(&regions)?;
if printer.config.line_range.is_some() {
if printer.line_number + 1 < printer.config.line_range.unwrap().0 {
// skip line
printer.line_number += 1;
} else if printer.line_number >= printer.config.line_range.unwrap().1 {
// no more lines in range
break;
} else {
printer.print_line(&regions)?;
}
} else {
printer.print_line(&regions)?;
}
}
buffer.clear();
}

View File

@ -15,10 +15,10 @@ use terminal::as_terminal_escaped;
pub struct Printer<'a> {
handle: &'a mut Write,
colors: Colors,
config: &'a Config<'a>,
pub config: &'a Config<'a>,
decorations: Vec<Box<Decoration>>,
panel_width: usize,
line_number: usize,
pub line_number: usize,
pub line_changes: Option<LineChanges>,
}