1
0
mirror of https://github.com/sharkdp/bat synced 2026-08-11 20:11:43 +00:00

list-themes buffer added

This commit is contained in:
abhinavcool42
2025-11-02 11:31:07 +05:30
parent 8e0e13a899
commit 9456f7d8cd
+19 -17
View File
@@ -16,7 +16,7 @@ use std::io::{BufReader, Write};
use std::path::Path; use std::path::Path;
use std::process; use std::process;
use bat::output::OutputType; use bat::output::{OutputType, OutputHandle};
use bat::theme::DetectColorScheme; use bat::theme::DetectColorScheme;
use nu_ansi_term::Color::Green; use nu_ansi_term::Color::Green;
use nu_ansi_term::Style; use nu_ansi_term::Style;
@@ -205,13 +205,11 @@ pub fn list_themes(
style.insert(StyleComponent::Plain); style.insert(StyleComponent::Plain);
config.language = Some("Rust"); config.language = Some("Rust");
config.style_components = StyleComponents(style); config.style_components = StyleComponents(style);
config.paging_mode = PagingMode::Always;
let mut output_type =
OutputType::from_mode(config.paging_mode, config.wrapping_mode, config.pager)?;
let mut writer = output_type.handle()?;
let default_theme_name = default_theme(color_scheme(detect_color_scheme).unwrap_or_default()); let default_theme_name = default_theme(color_scheme(detect_color_scheme).unwrap_or_default());
let mut buf = String::new();
let mut handle = OutputHandle::FmtWrite(&mut buf);
for theme in assets.themes() { for theme in assets.themes() {
let default_theme_info = if default_theme_name == theme { let default_theme_info = if default_theme_name == theme {
" (default)" " (default)"
@@ -222,35 +220,39 @@ pub fn list_themes(
} else { } else {
"" ""
}; };
if config.colored_output { if config.colored_output {
writeln!( handle.write_fmt(format_args!(
writer, "Theme: {}\n\n",
"Theme: {}{default_theme_info}\n",
Style::new().bold().paint(theme.to_string()), Style::new().bold().paint(theme.to_string()),
)?; ))?;
config.theme = theme.to_string(); config.theme = theme.to_string();
Controller::new(&config, &assets) Controller::new(&config, &assets)
.run(vec![theme_preview_file()], Some(&mut writer)) .run(vec![theme_preview_file()], Some(&mut handle))
.ok(); .ok();
writeln!(writer)?; handle.write_fmt(format_args!("\n"))?;
} else if config.loop_through { } else if config.loop_through {
writeln!(writer, "{theme}")?; handle.write_fmt(format_args!("{theme}\n"))?;
} else { } else {
writeln!(writer, "{theme}{default_theme_info}")?; handle.write_fmt(format_args!("{theme}{default_theme_info}\n"))?;
} }
} }
if config.colored_output { if config.colored_output {
writeln!( handle.write_fmt(format_args!(
writer,
"Further themes can be installed to '{}', \ "Further themes can be installed to '{}', \
and are added to the cache with `bat cache --build`. \ and are added to the cache with `bat cache --build`. \
For more information, see:\n\n \ For more information, see:\n\n \
https://github.com/sharkdp/bat#adding-new-themes", https://github.com/sharkdp/bat#adding-new-themes",
config_dir.join("themes").to_string_lossy() config_dir.join("themes").to_string_lossy()
)?; ))?;
} }
let mut output_type =
OutputType::from_mode(config.paging_mode, config.wrapping_mode, config.pager)?;
let mut writer = output_type.handle()?;
writer.write_fmt(format_args!("{buf}"))?;
Ok(()) Ok(())
} }