From b56194b41504e7d074de135747fee13a51832624 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 14 May 2020 15:51:28 -0400 Subject: [PATCH] Use ByteLines instead of iterator --- src/delta.rs | 20 ++++++++++---------- src/main.rs | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/delta.rs b/src/delta.rs index 7d91c8a19..05fc0760b 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -1,6 +1,8 @@ use std::io::Write; +use bytelines::ByteLines; use console::strip_ansi_codes; +use std::io::BufRead; use unicode_segmentation::UnicodeSegmentation; use crate::bat::assets::HighlightingAssets; @@ -51,13 +53,13 @@ impl State { // | HunkPlus | flush, emit | flush, emit | flush, emit | flush, emit | flush, push | push | pub fn delta( - mut lines: I, + mut lines: ByteLines, config: &Config, assets: &HighlightingAssets, writer: &mut dyn Write, ) -> std::io::Result<()> where - I: Iterator>>, + I: BufRead, { let mut painter = Painter::new(writer, config, assets); let mut minus_file = "".to_string(); @@ -403,6 +405,7 @@ mod tests { use super::*; use console::strip_ansi_codes; use std::env; + use std::io::BufReader; use syntect::highlighting::StyleModifier; use crate::paint; @@ -634,7 +637,7 @@ mod tests { let config = cli::process_command_line_arguments(&assets, &options); delta( - input.split("\n").map(|s| Ok(s.as_bytes().to_vec())), + ByteLines::new(BufReader::new(input.as_bytes())), &config, &assets, &mut writer, @@ -735,11 +738,8 @@ mod tests { let mut options = get_command_line_options(); options.color_only = true; let output = run_delta(input, &options); - assert_eq!( - strip_ansi_codes(&output).to_string(), - input.to_owned() + "\n" - ); - assert_ne!(output, input.to_owned() + "\n"); + assert_eq!(strip_ansi_codes(&output).to_string(), input.to_owned()); + assert_ne!(output, input.to_owned()); } } @@ -749,7 +749,7 @@ mod tests { let output = run_delta(DIFF_WITH_MERGE_CONFLICT, &options); // TODO: The + in the first column is being removed. assert!(strip_ansi_codes(&output).contains("+>>>>>>> Stashed changes")); - assert_eq!(output.split('\n').count(), 47); + assert_eq!(output.split('\n').count(), 46); } #[test] @@ -759,7 +759,7 @@ mod tests { let output = run_delta(DIFF_WITH_MERGE_CONFLICT, &options); assert_eq!( strip_ansi_codes(&output).to_string(), - DIFF_WITH_MERGE_CONFLICT.to_owned() + "\n" + DIFF_WITH_MERGE_CONFLICT.to_owned() ); } diff --git a/src/main.rs b/src/main.rs index 9bd4819f5..049420a09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,7 @@ fn main() -> std::io::Result<()> { let mut writer = output_type.handle().unwrap(); if let Err(error) = delta( - io::stdin().lock().byte_lines_iter(), + io::stdin().lock().byte_lines(), &config, &assets, &mut writer, @@ -120,10 +120,15 @@ fn get_painted_rgb_string(color: Color, true_color: bool) -> String { } fn list_themes(assets: &HighlightingAssets) -> std::io::Result<()> { + use bytelines::ByteLines; + use std::io::BufReader; let opt = cli::Opt::from_args(); - let mut input = Vec::new(); - if atty::is(atty::Stream::Stdin) { - input = b"\ + let input = if !atty::is(atty::Stream::Stdin) { + let mut buf = Vec::new(); + io::stdin().lock().read_to_end(&mut buf)?; + buf + } else { + b"\ diff --git a/example.rs b/example.rs index f38589a..0f1bb83 100644 --- a/example.rs @@ -139,9 +144,7 @@ index f38589a..0f1bb83 100644 + println!(\"The cube of {:.2} is {:.2}.\", num, result); }" .to_vec() - } else { - io::stdin().read_to_end(&mut input)?; - } + }; let stdout = io::stdout(); let mut stdout = stdout.lock(); @@ -164,7 +167,7 @@ index f38589a..0f1bb83 100644 let mut writer = output_type.handle().unwrap(); if let Err(error) = delta( - input.split(|&b| b == b'\n').map(|line| Ok(line.to_vec())), + ByteLines::new(BufReader::new(&input[0..])), &config, &assets, &mut writer,