From 9456f7d8cdb712e972b096160567d863cd8368ce Mon Sep 17 00:00:00 2001 From: abhinavcool42 Date: Sun, 2 Nov 2025 11:31:07 +0530 Subject: [PATCH] list-themes buffer added --- src/bin/bat/main.rs | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index a94b6d23..9b1b4ea0 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -16,7 +16,7 @@ use std::io::{BufReader, Write}; use std::path::Path; use std::process; -use bat::output::OutputType; +use bat::output::{OutputType, OutputHandle}; use bat::theme::DetectColorScheme; use nu_ansi_term::Color::Green; use nu_ansi_term::Style; @@ -205,13 +205,11 @@ pub fn list_themes( style.insert(StyleComponent::Plain); config.language = Some("Rust"); 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 mut buf = String::new(); + let mut handle = OutputHandle::FmtWrite(&mut buf); + for theme in assets.themes() { let default_theme_info = if default_theme_name == theme { " (default)" @@ -222,35 +220,39 @@ pub fn list_themes( } else { "" }; + if config.colored_output { - writeln!( - writer, - "Theme: {}{default_theme_info}\n", + handle.write_fmt(format_args!( + "Theme: {}\n\n", Style::new().bold().paint(theme.to_string()), - )?; + ))?; config.theme = theme.to_string(); Controller::new(&config, &assets) - .run(vec![theme_preview_file()], Some(&mut writer)) + .run(vec![theme_preview_file()], Some(&mut handle)) .ok(); - writeln!(writer)?; + handle.write_fmt(format_args!("\n"))?; } else if config.loop_through { - writeln!(writer, "{theme}")?; + handle.write_fmt(format_args!("{theme}\n"))?; } else { - writeln!(writer, "{theme}{default_theme_info}")?; + handle.write_fmt(format_args!("{theme}{default_theme_info}\n"))?; } } if config.colored_output { - writeln!( - writer, + handle.write_fmt(format_args!( "Further themes can be installed to '{}', \ and are added to the cache with `bat cache --build`. \ For more information, see:\n\n \ https://github.com/sharkdp/bat#adding-new-themes", 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(()) }