| 1 | use std::io::{self, IsTerminal, Write}; |
| 2 | |
| 3 | use itertools::Itertools; |
| 4 | |
| 5 | use crate::{options::theme::is_light_syntax_theme, utils}; |
| 6 | |
| 7 | #[cfg(not(tarpaulin_include))] |
| 8 | pub fn list_syntax_themes() -> std::io::Result<()> { |
| 9 | let stdout = io::stdout(); |
| 10 | let mut stdout = stdout.lock(); |
| 11 | if stdout.is_terminal() { |
| 12 | _list_syntax_themes_for_humans(&mut stdout) |
| 13 | } else { |
| 14 | _list_syntax_themes_for_machines(&mut stdout) |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | pub fn _list_syntax_themes_for_humans(writer: &mut dyn Write) -> std::io::Result<()> { |
| 19 | let assets = utils::bat::assets::load_highlighting_assets(); |
| 20 | |
| 21 | writeln!(writer, "Light syntax themes:")?; |
| 22 | for theme in assets.themes().filter(|t| is_light_syntax_theme(t)) { |
| 23 | writeln!(writer, " {theme}")?; |
| 24 | } |
| 25 | writeln!(writer, "\nDark syntax themes:")?; |
| 26 | for theme in assets.themes().filter(|t| !is_light_syntax_theme(t)) { |
| 27 | writeln!(writer, " {theme}")?; |
| 28 | } |
| 29 | writeln!( |
| 30 | writer, |
| 31 | "\nUse delta --show-syntax-themes to demo the themes." |
| 32 | )?; |
| 33 | Ok(()) |
| 34 | } |
| 35 | |
| 36 | pub fn _list_syntax_themes_for_machines(writer: &mut dyn Write) -> std::io::Result<()> { |
| 37 | let assets = utils::bat::assets::load_highlighting_assets(); |
| 38 | for theme in assets.themes().sorted_by_key(|t| is_light_syntax_theme(t)) { |
| 39 | writeln!( |
| 40 | writer, |
| 41 | "{}\t{}", |
| 42 | if is_light_syntax_theme(theme) { |
| 43 | "light" |
| 44 | } else { |
| 45 | "dark" |
| 46 | }, |
| 47 | theme |
| 48 | )?; |
| 49 | } |
| 50 | Ok(()) |
| 51 | } |
| 52 | |
| 53 | #[cfg(test)] |
| 54 | mod tests { |
| 55 | use std::io::{Cursor, Read, Seek}; |
| 56 | |
| 57 | use super::*; |
| 58 | |
| 59 | #[test] |
| 60 | fn test_list_syntax_themes_for_humans() { |
| 61 | let mut writer = Cursor::new(vec![0; 512]); |
| 62 | _list_syntax_themes_for_humans(&mut writer).unwrap(); |
| 63 | let mut s = String::new(); |
| 64 | writer.rewind().unwrap(); |
| 65 | writer.read_to_string(&mut s).unwrap(); |
| 66 | assert!(s.contains("Light syntax themes:\n")); |
| 67 | assert!(s.contains(" GitHub\n")); |
| 68 | assert!(s.contains("Dark syntax themes:\n")); |
| 69 | assert!(s.contains(" Dracula\n")); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn test_list_syntax_themes_for_machines() { |
| 74 | let mut writer = Cursor::new(vec![0; 512]); |
| 75 | _list_syntax_themes_for_machines(&mut writer).unwrap(); |
| 76 | let mut s = String::new(); |
| 77 | writer.rewind().unwrap(); |
| 78 | writer.read_to_string(&mut s).unwrap(); |
| 79 | assert!(s.contains("light GitHub\n")); |
| 80 | assert!(s.contains("dark Dracula\n")); |
| 81 | } |
| 82 | } |