| 1 | use std::io::{self, ErrorKind, IsTerminal, Read}; |
| 2 | |
| 3 | use crate::cli; |
| 4 | use crate::color::ColorMode; |
| 5 | use crate::config; |
| 6 | use crate::delta; |
| 7 | use crate::env::DeltaEnv; |
| 8 | use crate::git_config; |
| 9 | use crate::options::get::get_themes; |
| 10 | use crate::utils::bat::output::{OutputType, PagingMode}; |
| 11 | |
| 12 | pub fn show_themes(dark: bool, light: bool, color_mode: ColorMode) -> std::io::Result<()> { |
| 13 | use std::io::BufReader; |
| 14 | |
| 15 | use bytelines::ByteLines; |
| 16 | |
| 17 | use super::sample_diff::DIFF; |
| 18 | |
| 19 | let env = DeltaEnv::default(); |
| 20 | let themes = get_themes(git_config::GitConfig::try_create(&env)); |
| 21 | if themes.is_empty() { |
| 22 | return Err(std::io::Error::new( |
| 23 | ErrorKind::NotFound, |
| 24 | "No themes found. Please see https://dandavison.github.io/delta/custom-themes.html.", |
| 25 | )); |
| 26 | } |
| 27 | |
| 28 | let mut input = DIFF.to_vec(); |
| 29 | |
| 30 | if !io::stdin().is_terminal() { |
| 31 | let mut buf = Vec::new(); |
| 32 | io::stdin().lock().read_to_end(&mut buf)?; |
| 33 | if !buf.is_empty() { |
| 34 | input = buf; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | let git_config = git_config::GitConfig::try_create(&env); |
| 39 | let opt = cli::Opt::from_iter_and_git_config( |
| 40 | &env, |
| 41 | &["delta", "--navigate", "--show-themes"], |
| 42 | git_config, |
| 43 | ); |
| 44 | let mut output_type = OutputType::from_mode( |
| 45 | &env, |
| 46 | PagingMode::Always, |
| 47 | None, |
| 48 | &config::Config::from(opt).into(), |
| 49 | ) |
| 50 | .unwrap(); |
| 51 | let title_style = ansi_term::Style::new().bold(); |
| 52 | let writer = output_type.handle().unwrap(); |
| 53 | |
| 54 | for theme in &themes { |
| 55 | let git_config = git_config::GitConfig::try_create(&env); |
| 56 | let opt = |
| 57 | cli::Opt::from_iter_and_git_config(&env, &["delta", "--features", theme], git_config); |
| 58 | let is_dark_theme = opt.dark; |
| 59 | let is_light_theme = opt.light; |
| 60 | let config = config::Config::from(opt); |
| 61 | |
| 62 | if (color_mode == ColorMode::Dark && is_dark_theme) |
| 63 | || (color_mode == ColorMode::Light && is_light_theme) |
| 64 | || (dark && light) |
| 65 | { |
| 66 | writeln!(writer, "\n\nTheme: {}\n", title_style.paint(theme))?; |
| 67 | |
| 68 | if let Err(error) = |
| 69 | delta::delta(ByteLines::new(BufReader::new(&input[0..])), writer, &config) |
| 70 | { |
| 71 | match error.kind() { |
| 72 | ErrorKind::BrokenPipe => std::process::exit(0), |
| 73 | _ => eprintln!("{error}"), |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | Ok(()) |
| 80 | } |