1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-11-19 16:25:56 +00:00

Merge pull request #3457 from abhinavcool42/fix/list-themes-pager-hang

fix: `--list-themes` pagination
This commit is contained in:
Keith Hall
2025-11-15 15:49:40 +02:00
committed by GitHub
4 changed files with 53 additions and 35 deletions

View File

@@ -4,6 +4,7 @@
## Bugfixes ## Bugfixes
- Fix hang when using `--list-themes` with an explicit pager, see #3457 (@abhinavcool42)
- Fix negative values of N not being parsed in <N:M> line ranges without `=` flag value separator, see #3442 (@lmmx) - Fix negative values of N not being parsed in <N:M> line ranges without `=` flag value separator, see #3442 (@lmmx)
## Other ## Other

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::{OutputHandle, OutputType};
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;
@@ -206,11 +206,10 @@ pub fn list_themes(
config.language = Some("Rust"); config.language = Some("Rust");
config.style_components = StyleComponents(style); config.style_components = StyleComponents(style);
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)"
@@ -221,35 +220,39 @@ pub fn list_themes(
} else { } else {
"" ""
}; };
if config.colored_output { if config.colored_output {
writeln!( handle.write_fmt(format_args!(
writer, "{}{default_theme_info}\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(())
} }

View File

@@ -50,10 +50,12 @@ impl Controller<'_> {
output_handle: Option<&mut OutputHandle<'_>>, output_handle: Option<&mut OutputHandle<'_>>,
mut handle_error: impl FnMut(&Error, &mut dyn Write), mut handle_error: impl FnMut(&Error, &mut dyn Write),
) -> Result<bool> { ) -> Result<bool> {
let mut output_type; // only create our own OutputType if no output handle was provided.
#[allow(unused_mut)]
let mut output_type_opt: Option<OutputType> = None;
#[cfg(feature = "paging")] #[cfg(feature = "paging")]
{ if output_handle.is_none() {
use crate::input::InputKind; use crate::input::InputKind;
use std::path::Path; use std::path::Path;
@@ -74,25 +76,35 @@ impl Controller<'_> {
let wrapping_mode = self.config.wrapping_mode; let wrapping_mode = self.config.wrapping_mode;
output_type = OutputType::from_mode(paging_mode, wrapping_mode, self.config.pager)?; output_type_opt = Some(OutputType::from_mode(
paging_mode,
wrapping_mode,
self.config.pager,
)?);
} }
#[cfg(not(feature = "paging"))] #[cfg(not(feature = "paging"))]
{ if output_handle.is_none() {
output_type = OutputType::stdout(); output_type_opt = Some(OutputType::stdout());
} }
let attached_to_pager = output_type.is_pager(); let attached_to_pager = match (&output_handle, &output_type_opt) {
(Some(_), _) => true,
(None, Some(ot)) => ot.is_pager(),
(None, None) => false,
};
let stdout_identifier = if cfg!(windows) || attached_to_pager { let stdout_identifier = if cfg!(windows) || attached_to_pager {
None None
} else { } else {
clircle::Identifier::stdout() clircle::Identifier::stdout()
}; };
let mut writer = match output_handle { let mut writer = match (output_handle, &mut output_type_opt) {
Some(OutputHandle::FmtWrite(w)) => OutputHandle::FmtWrite(w), (Some(OutputHandle::FmtWrite(w)), _) => OutputHandle::FmtWrite(w),
Some(OutputHandle::IoWrite(w)) => OutputHandle::IoWrite(w), (Some(OutputHandle::IoWrite(w)), _) => OutputHandle::IoWrite(w),
None => output_type.handle()?, (None, Some(ot)) => ot.handle()?,
(None, None) => unreachable!("No output handle and no output type available"),
}; };
let mut no_errors: bool = true; let mut no_errors: bool = true;
let stderr = io::stderr(); let stderr = io::stderr();

View File

@@ -141,6 +141,7 @@ impl OutputType {
// //
// For newer versions (530 or 558 on Windows), we omit '--no-init' as it // For newer versions (530 or 558 on Windows), we omit '--no-init' as it
// is not needed anymore. // is not needed anymore.
if single_screen_action == SingleScreenAction::Quit {
match retrieve_less_version(&pager.bin) { match retrieve_less_version(&pager.bin) {
None => { None => {
p.arg("--no-init"); p.arg("--no-init");
@@ -152,6 +153,7 @@ impl OutputType {
} }
_ => {} _ => {}
} }
}
} else { } else {
p.args(args); p.args(args);
} }