diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-03 21:29:16 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-03 22:17:03 -0400 |
commit | a02e82e76cc7485f7c19f13f91082b74a6346646 (patch) | |
tree | d809d4a73a37a5e9183b2c0851f54fbccff708c6 /src | |
parent | 76d42e2ac19fa94ce736ee5a62aa63a61b00ad29 (diff) |
delta acts as `diff -u minus_file plus_file | delta` when there is no stdin
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 9 | ||||
-rw-r--r-- | src/config.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 45 |
3 files changed, 59 insertions, 0 deletions
@@ -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<PathBuf>, + + /// Second file to be compared when delta is being used in diff mode. + #[structopt(parse(from_os_str))] + pub plus_file: Option<PathBuf>, + #[structopt(long = "minus-color")] /// Deprecated: use --minus-style='normal my_background_color'. pub deprecated_minus_background_color: Option<String>, 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<PathBuf>, + pub plus_file: Option<PathBuf>, } 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 \ |