summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Otto <78380144+th1000s@users.noreply.github.com>2021-04-23 15:57:57 +0200
committerGitHub <noreply@github.com>2021-04-23 09:57:57 -0400
commit535ba79da0456bb4c3c1e20bb8dfb6456407b12c (patch)
tree7eb91f18aa4bd7e50e39065c06510755bef80861
parentce5dd0c47aa71f08590b3b518dc9c7162b727c10 (diff)
Fix --show-syntax-themes exhausting stdin (#569)
If a diff is provided via stdin then the first `_show_syntax_themes` call reads from it directly and uses that, but the second one finds nothing is left to read and falls back to the default. Fixed by reading from stdin earlier.
-rw-r--r--src/main.rs36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs
index c99fb464..aa7927cb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -375,13 +375,25 @@ fn show_syntax_themes() -> std::io::Result<()> {
let mut writer = output_type.handle().unwrap();
opt.computed.syntax_set = assets.syntax_set;
+ let stdin_data = if !atty::is(atty::Stream::Stdin) {
+ let mut buf = Vec::new();
+ io::stdin().lock().read_to_end(&mut buf)?;
+ if !buf.is_empty() {
+ Some(buf)
+ } else {
+ None
+ }
+ } else {
+ None
+ };
+
if !(opt.dark || opt.light) {
- _show_syntax_themes(opt.clone(), false, &mut writer)?;
- _show_syntax_themes(opt, true, &mut writer)?;
+ _show_syntax_themes(opt.clone(), false, &mut writer, stdin_data.as_ref())?;
+ _show_syntax_themes(opt, true, &mut writer, stdin_data.as_ref())?;
} else if opt.light {
- _show_syntax_themes(opt, true, &mut writer)?;
+ _show_syntax_themes(opt, true, &mut writer, stdin_data.as_ref())?;
} else {
- _show_syntax_themes(opt, false, &mut writer)?
+ _show_syntax_themes(opt, false, &mut writer, stdin_data.as_ref())?
};
Ok(())
}
@@ -390,10 +402,14 @@ fn _show_syntax_themes(
mut opt: cli::Opt,
is_light_mode: bool,
writer: &mut dyn Write,
+ stdin: Option<&Vec<u8>>,
) -> std::io::Result<()> {
use bytelines::ByteLines;
use std::io::BufReader;
- let mut input = b"\
+ let input = match stdin {
+ Some(stdin_data) => &stdin_data[..],
+ None => {
+ b"\
diff --git a/example.rs b/example.rs
index f38589a..0f1bb83 100644
--- a/example.rs
@@ -408,12 +424,6 @@ index f38589a..0f1bb83 100644
+ let result = f64::powf(num, 3.0);
+ println!(\"The cube of {:.2} is {:.2}.\", num, result);
"
- .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;
}
};
@@ -520,12 +530,12 @@ mod main_tests {
let opt = integration_test_utils::make_options_from_args(&[]);
let mut writer = Cursor::new(vec![0; 1024]);
- _show_syntax_themes(opt, true, &mut writer).unwrap();
+ _show_syntax_themes(opt, true, &mut writer, None).unwrap();
let mut s = String::new();
writer.seek(SeekFrom::Start(0)).unwrap();
writer.read_to_string(&mut s).unwrap();
let s = ansi::strip_ansi_codes(&s);
- assert!(s.contains("\nTheme: gruvbox-white\n"));
+ assert!(s.contains("\nTheme: gruvbox-light\n"));
println!("{}", s);
assert!(s.contains("\nfn print_cube(num: f64) {\n"));
}