From a02e82e76cc7485f7c19f13f91082b74a6346646 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Wed, 3 Jun 2020 21:29:16 -0400 Subject: delta acts as `diff -u minus_file plus_file | delta` when there is no stdin --- src/cli.rs | 9 +++++++++ src/config.rs | 5 +++++ src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) (limited to 'src') diff --git a/src/cli.rs b/src/cli.rs index c22ebe24..b06743aa 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use std::process; use structopt::clap::AppSettings::{ColorAlways, ColoredHelp, DeriveDisplayOrder}; @@ -287,6 +288,14 @@ pub struct Opt { #[structopt(long = "paging", default_value = "auto")] pub paging_mode: String, + /// First file to be compared when delta is being used in diff mode. + #[structopt(parse(from_os_str))] + pub minus_file: Option, + + /// Second file to be compared when delta is being used in diff mode. + #[structopt(parse(from_os_str))] + pub plus_file: Option, + #[structopt(long = "minus-color")] /// Deprecated: use --minus-style='normal my_background_color'. pub deprecated_minus_background_color: Option, diff --git a/src/config.rs b/src/config.rs index e96dca41..1005a782 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use std::cmp::min; +use std::path::PathBuf; use std::process; use console::Term; @@ -51,6 +52,8 @@ pub struct Config<'a> { pub null_syntect_style: SyntectStyle, pub max_buffered_lines: usize, pub paging_mode: PagingMode, + pub minus_file: Option, + pub plus_file: Option, } impl<'a> Config<'a> { @@ -172,6 +175,8 @@ pub fn get_config<'a>( null_syntect_style: SyntectStyle::default(), max_buffered_lines: 32, paging_mode, + minus_file: opt.minus_file, + plus_file: opt.plus_file, } } diff --git a/src/main.rs b/src/main.rs index 2f92f498..c5944daa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,8 @@ mod tests; mod theme; use std::io::{self, ErrorKind, Read, Write}; +use std::os::unix::process::CommandExt; +use std::path::PathBuf; use std::process; use ansi_term::{self, Color}; @@ -61,6 +63,11 @@ fn main() -> std::io::Result<()> { let config = cli::process_command_line_arguments(opt, Some(arg_matches)); + if atty::is(atty::Stream::Stdin) { + delta_diff(config); + process::exit(0); + } + if show_background_colors_option { show_background_colors(&config); process::exit(0); @@ -78,6 +85,44 @@ fn main() -> std::io::Result<()> { Ok(()) } +fn delta_diff(config: config::Config) { + let mut output_type = OutputType::from_mode(config.paging_mode, None, &config).unwrap(); + let writer = output_type.handle().unwrap(); + let die = || { + eprintln!("Usage: delta minus_file plus_file"); + process::exit(1); + }; + let minus_file = config.minus_file.unwrap_or_else(die); + let plus_file = config.plus_file.unwrap_or_else(die); + + let diff_process = process::Command::new(PathBuf::from("diff")) + .arg("-u") + .args(&[minus_file, plus_file]) + .stdout(process::Stdio::piped()) + .spawn() + .unwrap(); + + if cfg!(unix) { + process::Command::new(PathBuf::from("delta")) + .stdin(diff_process.stdout.unwrap()) + .exec(); + } else { + // TODO: Send stdout of child delta process directly to less. + let mut buf = String::new(); + process::Command::new(PathBuf::from("delta")) + .stdin(diff_process.stdout.unwrap()) + .stdout(process::Stdio::piped()) + .spawn() + .unwrap() + .stdout + .unwrap() + .read_to_string(&mut buf) + .unwrap(); + write!(writer, "{}", buf).unwrap(); + process::exit(0); + } +} + fn show_background_colors(config: &config::Config) { println!( "delta \ -- cgit v1.2.3