Skip to content

Commit

Permalink
Accept input lines as raw bytes and decode in main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed May 14, 2020
1 parent 18c1d1a commit 7e1a66f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ansi_colours = "1.0.1"
ansi_term = "0.12.1"
atty = "0.2.14"
box_drawing = "0.1.2"
bytelines = "2.2.2"
console = "0.11.2"
dirs = "2.0"
lazy_static = "1.4"
Expand Down
9 changes: 5 additions & 4 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,22 @@ impl State {
// | HunkPlus | flush, emit | flush, emit | flush, emit | flush, emit | flush, push | push |

pub fn delta<I>(
lines: I,
mut lines: I,
config: &Config,
assets: &HighlightingAssets,
writer: &mut dyn Write,
) -> std::io::Result<()>
where
I: Iterator<Item = String>,
I: Iterator<Item = std::io::Result<Vec<u8>>>,
{
let mut painter = Painter::new(writer, config, assets);
let mut minus_file = "".to_string();
let mut plus_file;
let mut state = State::Unknown;
let mut source = Source::Unknown;

for raw_line in lines {
while let Some(Ok(raw_line_bytes)) = lines.next() {
let raw_line = String::from_utf8_lossy(&raw_line_bytes);
let line = strip_ansi_codes(&raw_line).to_string();
if source == Source::Unknown {
source = detect_source(&line);
Expand Down Expand Up @@ -633,7 +634,7 @@ mod tests {
let config = cli::process_command_line_arguments(&assets, &options);

delta(
input.split("\n").map(String::from),
input.split("\n").map(|s| Ok(s.as_bytes().to_vec())),
&config,
&assets,
&mut writer,
Expand Down
18 changes: 8 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ mod paint;
mod parse;
mod style;

use std::io::{self, BufRead, ErrorKind, Read, Write};
use std::io::{self, ErrorKind, Read, Write};
use std::process;

use ansi_term;
use atty;
use bytelines::ByteLinesReader;
use structopt::StructOpt;
use syntect::highlighting::{Color, FontStyle, Style};

Expand Down Expand Up @@ -62,10 +63,7 @@ fn main() -> std::io::Result<()> {
let mut writer = output_type.handle().unwrap();

if let Err(error) = delta(
io::stdin()
.lock()
.lines()
.map(|l| l.unwrap_or_else(|_| "<delta: invalid utf-8 data>".to_string())),
io::stdin().lock().byte_lines_iter(),
&config,
&assets,
&mut writer,
Expand Down Expand Up @@ -123,9 +121,9 @@ fn get_painted_rgb_string(color: Color, true_color: bool) -> String {

fn list_themes(assets: &HighlightingAssets) -> std::io::Result<()> {
let opt = cli::Opt::from_args();
let mut input = String::new();
let mut input = Vec::new();
if atty::is(atty::Stream::Stdin) {
input = "\
input = b"\
diff --git a/example.rs b/example.rs
index f38589a..0f1bb83 100644
--- a/example.rs
Expand All @@ -140,9 +138,9 @@ index f38589a..0f1bb83 100644
+ let result = f64::powf(num, 3.0);
+ println!(\"The cube of {:.2} is {:.2}.\", num, result);
}"
.to_string()
.to_vec()
} else {
io::stdin().read_to_string(&mut input)?;
io::stdin().read_to_end(&mut input)?;
}

let stdout = io::stdout();
Expand All @@ -166,7 +164,7 @@ index f38589a..0f1bb83 100644
let mut writer = output_type.handle().unwrap();

if let Err(error) = delta(
input.split('\n').map(String::from),
input.split(|&b| b == b'\n').map(|line| Ok(line.to_vec())),
&config,
&assets,
&mut writer,
Expand Down

0 comments on commit 7e1a66f

Please sign in to comment.