Skip to content

Commit

Permalink
Use ByteLines instead of iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed May 14, 2020
1 parent 7e1a66f commit b56194b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/delta.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,13 +53,13 @@ impl State {
// | HunkPlus | flush, emit | flush, emit | flush, emit | flush, emit | flush, push | push |

pub fn delta<I>(
mut lines: I,
mut lines: ByteLines<I>,
config: &Config,
assets: &HighlightingAssets,
writer: &mut dyn Write,
) -> std::io::Result<()>
where
I: Iterator<Item = std::io::Result<Vec<u8>>>,
I: BufRead,
{
let mut painter = Painter::new(writer, config, assets);
let mut minus_file = "".to_string();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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]
Expand All @@ -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()
);
}

Expand Down
19 changes: 11 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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,
Expand Down

0 comments on commit b56194b

Please sign in to comment.