| 1 | use crate::cli; |
| 2 | use crate::color::{ColorMode, ColorMode::*}; |
| 3 | use crate::config; |
| 4 | use crate::delta; |
| 5 | use crate::env::DeltaEnv; |
| 6 | use crate::options::theme::color_mode_from_syntax_theme; |
| 7 | use crate::utils; |
| 8 | use crate::utils::bat::output::{OutputType, PagingMode}; |
| 9 | use clap::Parser; |
| 10 | use std::io::{self, ErrorKind, IsTerminal, Read, Write}; |
| 11 | |
| 12 | #[cfg(not(tarpaulin_include))] |
| 13 | pub fn show_syntax_themes() -> std::io::Result<()> { |
| 14 | let env = DeltaEnv::default(); |
| 15 | let assets = utils::bat::assets::load_highlighting_assets(); |
| 16 | let mut output_type = OutputType::from_mode( |
| 17 | &env, |
| 18 | PagingMode::QuitIfOneScreen, |
| 19 | None, |
| 20 | &config::Config::from(cli::Opt::parse()).into(), |
| 21 | ) |
| 22 | .unwrap(); |
| 23 | let mut writer = output_type.handle().unwrap(); |
| 24 | |
| 25 | let stdin_data = if !io::stdin().is_terminal() { |
| 26 | let mut buf = Vec::new(); |
| 27 | io::stdin().lock().read_to_end(&mut buf)?; |
| 28 | if !buf.is_empty() { |
| 29 | Some(buf) |
| 30 | } else { |
| 31 | None |
| 32 | } |
| 33 | } else { |
| 34 | None |
| 35 | }; |
| 36 | |
| 37 | let make_opt = || { |
| 38 | let mut opt = cli::Opt::parse(); |
| 39 | opt.computed.syntax_set = assets.get_syntax_set().unwrap().clone(); |
| 40 | opt |
| 41 | }; |
| 42 | let opt = make_opt(); |
| 43 | |
| 44 | if !(opt.dark || opt.light) { |
| 45 | _show_syntax_themes(opt, Dark, &mut writer, stdin_data.as_ref())?; |
| 46 | _show_syntax_themes(make_opt(), Light, &mut writer, stdin_data.as_ref())?; |
| 47 | } else if opt.light { |
| 48 | _show_syntax_themes(opt, Light, &mut writer, stdin_data.as_ref())?; |
| 49 | } else { |
| 50 | _show_syntax_themes(opt, Dark, &mut writer, stdin_data.as_ref())? |
| 51 | }; |
| 52 | Ok(()) |
| 53 | } |
| 54 | |
| 55 | fn _show_syntax_themes( |
| 56 | mut opt: cli::Opt, |
| 57 | color_mode: ColorMode, |
| 58 | writer: &mut dyn Write, |
| 59 | stdin: Option<&Vec<u8>>, |
| 60 | ) -> std::io::Result<()> { |
| 61 | use bytelines::ByteLines; |
| 62 | use std::io::BufReader; |
| 63 | let input = match stdin { |
| 64 | Some(stdin_data) => &stdin_data[..], |
| 65 | None => { |
| 66 | b"\ |
| 67 | diff --git a/example.rs b/example.rs |
| 68 | index f38589a..0f1bb83 100644 |
| 69 | --- a/example.rs |
| 70 | +++ b/example.rs |
| 71 | @@ -1,5 +1,5 @@ |
| 72 | -// Output the square of a number. |
| 73 | -fn print_square(num: f64) { |
| 74 | - let result = f64::powf(num, 2.0); |
| 75 | - println!(\"The square of {:.2} is {:.2}.\", num, result); |
| 76 | +// Output the cube of a number. |
| 77 | +fn print_cube(num: f64) { |
| 78 | + let result = f64::powf(num, 3.0); |
| 79 | + println!(\"The cube of {:.2} is {:.2}.\", num, result); |
| 80 | " |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | opt.computed.color_mode = color_mode; |
| 85 | let mut config = config::Config::from(opt); |
| 86 | let title_style = ansi_term::Style::new().bold(); |
| 87 | let assets = utils::bat::assets::load_highlighting_assets(); |
| 88 | |
| 89 | for syntax_theme in assets |
| 90 | .themes() |
| 91 | .filter(|t| color_mode_from_syntax_theme(t) == color_mode) |
| 92 | { |
| 93 | writeln!( |
| 94 | writer, |
| 95 | "\n\nSyntax theme: {}\n", |
| 96 | title_style.paint(syntax_theme) |
| 97 | )?; |
| 98 | config.syntax_theme = Some(assets.get_theme(syntax_theme).clone()); |
| 99 | if let Err(error) = |
| 100 | delta::delta(ByteLines::new(BufReader::new(&input[0..])), writer, &config) |
| 101 | { |
| 102 | match error.kind() { |
| 103 | ErrorKind::BrokenPipe => std::process::exit(0), |
| 104 | _ => eprintln!("{error}"), |
| 105 | } |
| 106 | }; |
| 107 | } |
| 108 | Ok(()) |
| 109 | } |
| 110 | |
| 111 | #[cfg(test)] |
| 112 | mod tests { |
| 113 | use std::io::{Cursor, Seek}; |
| 114 | |
| 115 | use super::*; |
| 116 | use crate::ansi; |
| 117 | use crate::tests::integration_test_utils; |
| 118 | |
| 119 | #[test] |
| 120 | #[ignore] // Not working (timing out) when run by tarpaulin, presumably due to stdin detection. |
| 121 | fn test_show_syntax_themes() { |
| 122 | let opt = integration_test_utils::make_options_from_args(&[]); |
| 123 | |
| 124 | let mut writer = Cursor::new(vec![0; 1024]); |
| 125 | _show_syntax_themes(opt, Light, &mut writer, None).unwrap(); |
| 126 | let mut s = String::new(); |
| 127 | writer.rewind().unwrap(); |
| 128 | writer.read_to_string(&mut s).unwrap(); |
| 129 | let s = ansi::strip_ansi_codes(&s); |
| 130 | assert!(s.contains("\nSyntax theme: gruvbox-light\n")); |
| 131 | println!("{s}"); |
| 132 | assert!(s.contains("\nfn print_cube(num: f64) {\n")); |
| 133 | } |
| 134 | } |