From 43f971512594d931b97a61313612601ea5af6d0c Mon Sep 17 00:00:00 2001 From: Catherine Noll Date: Sat, 20 Mar 2021 23:08:27 -0500 Subject: Add functionality to cycle through themes using a diff from stdin --- src/main.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index b03b95a3..d48a2961 100644 --- a/src/main.rs +++ b/src/main.rs @@ -315,31 +315,30 @@ const THEMES: [&'static str; 4] = [ fn show_themes() -> std::io::Result<()> { use bytelines::ByteLines; - use std::io::BufReader; use sample_diff::DIFF; - let input = &DIFF.to_vec(); + use std::io::BufReader; + let mut input = DIFF.to_vec(); + + if !atty::is(atty::Stream::Stdin) { + let mut buf = Vec::new(); + io::stdin().lock().read_to_end(&mut buf)?; + if !buf.is_empty() { + input = buf; + } + }; let mut git_config = git_config::GitConfig::try_create(); - let opt = cli::Opt::from_iter_and_git_config( - &["", "", "--navigate"], - &mut git_config, - ); + let opt = cli::Opt::from_iter_and_git_config(&["", "", "--navigate"], &mut git_config); - let mut output_type = OutputType::from_mode( - PagingMode::Always, - None, - &config::Config::from(opt), - ) - .unwrap(); + let mut output_type = + OutputType::from_mode(PagingMode::Always, None, &config::Config::from(opt)).unwrap(); let title_style = ansi_term::Style::new().bold(); let writer = output_type.handle().unwrap(); for theme in &THEMES { writeln!(writer, "\n\nTheme: {}\n", title_style.paint(*theme))?; - let opt = cli::Opt::from_iter_and_git_config( - &["", "", "--features", theme], - &mut git_config, - ); + let opt = + cli::Opt::from_iter_and_git_config(&["", "", "--features", theme], &mut git_config); let config = config::Config::from(opt); if let Err(error) = delta(ByteLines::new(BufReader::new(&input[0..])), writer, &config) { match error.kind() { -- cgit v1.2.3