on master help sign in
add line-range
commit6691786d828cb44fdd5e7150d9f17d75d7322d37 authorTaylor Skinner <tskinn12@gmail.com> dateThu 31 May 2018, 21:08 (8 years ago) parent50209bf
3 files changed+55−3
src/app.rs+39 src/features.rs+14−1 src/printer.rs+2−2
src/app.rs+39
@@ -141,6 +141,17 @@ impl App {
141141 see all available themes",
142142 ),
143143 )
144+ .arg(
145+ Arg::with_name("line-range")
146+ .long("line-range")
147+ .overrides_with("line-range")
148+ .takes_value(true)
149+ .help("Print range of lines")
150+ .long_help(
151+ "Print a specified range of lines from the files \
152+ --line-range 30-40 or --line-range 30-",
153+ ),
154+ )
144155 .arg(
145156 Arg::with_name("list-themes")
146157 .long("list-themes")
@@ -269,6 +280,7 @@ impl App {
269280 term_width: Term::stdout().size().1 as usize,
270281 files,
271282 theme: self.matches.value_of("theme"),
283+ line_range: get_ranges(self.matches.value_of("line-range")),
272284 })
273285 }
274286
@@ -322,6 +334,7 @@ pub struct Config<'a> {
322334 pub term_width: usize,
323335 pub files: Vec<Option<&'a str>>,
324336 pub theme: Option<&'a str>,
337+ pub line_range: Option<(usize, usize)>,
325338}
326339
327340fn is_truecolor_terminal() -> bool {
@@ -329,3 +342,29 @@ fn is_truecolor_terminal() -> bool {
329342 .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
330343 .unwrap_or(false)
331344}
345+
346+fn get_ranges(value: Option<&str>) -> Option<(usize, usize)> {
347+ match value {
348+ None => None,
349+ Some(str_range) => {
350+ let mut new_range = (usize::min_value(), usize::max_value());
351+ if str_range.bytes().nth(0).expect("Something should be here!") == b':' {
352+ new_range.1 = str_range[1..].parse().expect("This should be a number!");
353+ return Some(new_range);
354+ } else if str_range.bytes().last().expect("There should be a last!") == b':' {
355+ new_range.0 = str_range[..str_range.len() - 1]
356+ .parse()
357+ .expect("This should be a number!");
358+ return Some(new_range);
359+ }
360+
361+ let line_numbers: Vec<&str> = str_range.split(':').collect();
362+ if line_numbers.len() == 2 {
363+ new_range.0 = line_numbers[0].parse().expect("Should be a number!");
364+ new_range.1 = line_numbers[1].parse().expect("Should be a number!");
365+ }
366+
367+ Some(new_range)
368+ }
369+ }
370+}
src/features.rs+14−1
@@ -96,12 +96,25 @@ fn print_file(
9696 printer.print_header(filename)?;
9797
9898 let mut buffer = Vec::new();
99+
99100 while reader.read_until(b'\n', &mut buffer)? > 0 {
100101 {
101102 let line = String::from_utf8_lossy(&buffer);
102103 let regions = highlighter.highlight(line.as_ref());
103104
104− printer.print_line(&regions)?;
105+ if printer.config.line_range.is_some() {
106+ if printer.line_number + 1 < printer.config.line_range.unwrap().0 {
107+ // skip line
108+ printer.line_number += 1;
109+ } else if printer.line_number >= printer.config.line_range.unwrap().1 {
110+ // no more lines in range
111+ break;
112+ } else {
113+ printer.print_line(&regions)?;
114+ }
115+ } else {
116+ printer.print_line(&regions)?;
117+ }
105118 }
106119 buffer.clear();
107120 }
src/printer.rs+2−2
@@ -15,10 +15,10 @@ use terminal::as_terminal_escaped;
1515pub struct Printer<'a> {
1616 handle: &'a mut Write,
1717 colors: Colors,
18− config: &'a Config<'a>,
18+ pub config: &'a Config<'a>,
1919 decorations: Vec<Box<Decoration>>,
2020 panel_width: usize,
21− line_number: usize,
21+ pub line_number: usize,
2222 pub line_changes: Option<LineChanges>,
2323}
2424