Skip to content

Commit 1e4a4b7

Browse files
authored
Merge pull request #3457 from abhinavcool42/fix/list-themes-pager-hang
fix: `--list-themes` pagination
2 parents c51cff8 + cfe4977 commit 1e4a4b7

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## Bugfixes
66

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

910
## Other

src/bin/bat/main.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::io::{BufReader, Write};
1616
use std::path::Path;
1717
use std::process;
1818

19-
use bat::output::OutputType;
19+
use bat::output::{OutputHandle, OutputType};
2020
use bat::theme::DetectColorScheme;
2121
use nu_ansi_term::Color::Green;
2222
use nu_ansi_term::Style;
@@ -206,11 +206,10 @@ pub fn list_themes(
206206
config.language = Some("Rust");
207207
config.style_components = StyleComponents(style);
208208

209-
let mut output_type =
210-
OutputType::from_mode(config.paging_mode, config.wrapping_mode, config.pager)?;
211-
let mut writer = output_type.handle()?;
212-
213209
let default_theme_name = default_theme(color_scheme(detect_color_scheme).unwrap_or_default());
210+
let mut buf = String::new();
211+
let mut handle = OutputHandle::FmtWrite(&mut buf);
212+
214213
for theme in assets.themes() {
215214
let default_theme_info = if default_theme_name == theme {
216215
" (default)"
@@ -221,35 +220,39 @@ pub fn list_themes(
221220
} else {
222221
""
223222
};
223+
224224
if config.colored_output {
225-
writeln!(
226-
writer,
227-
"Theme: {}{default_theme_info}\n",
225+
handle.write_fmt(format_args!(
226+
"{}{default_theme_info}\n\n",
228227
Style::new().bold().paint(theme.to_string()),
229-
)?;
228+
))?;
230229
config.theme = theme.to_string();
231230
Controller::new(&config, &assets)
232-
.run(vec![theme_preview_file()], Some(&mut writer))
231+
.run(vec![theme_preview_file()], Some(&mut handle))
233232
.ok();
234-
writeln!(writer)?;
233+
handle.write_fmt(format_args!("\n"))?;
235234
} else if config.loop_through {
236-
writeln!(writer, "{theme}")?;
235+
handle.write_fmt(format_args!("{theme}\n"))?;
237236
} else {
238-
writeln!(writer, "{theme}{default_theme_info}")?;
237+
handle.write_fmt(format_args!("{theme}{default_theme_info}\n"))?;
239238
}
240239
}
241240

242241
if config.colored_output {
243-
writeln!(
244-
writer,
242+
handle.write_fmt(format_args!(
245243
"Further themes can be installed to '{}', \
246244
and are added to the cache with `bat cache --build`. \
247245
For more information, see:\n\n \
248246
https://github.com/sharkdp/bat#adding-new-themes",
249247
config_dir.join("themes").to_string_lossy()
250-
)?;
248+
))?;
251249
}
252250

251+
let mut output_type =
252+
OutputType::from_mode(config.paging_mode, config.wrapping_mode, config.pager)?;
253+
let mut writer = output_type.handle()?;
254+
writer.write_fmt(format_args!("{buf}"))?;
255+
253256
Ok(())
254257
}
255258

src/controller.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ impl Controller<'_> {
5050
output_handle: Option<&mut OutputHandle<'_>>,
5151
mut handle_error: impl FnMut(&Error, &mut dyn Write),
5252
) -> Result<bool> {
53-
let mut output_type;
53+
// only create our own OutputType if no output handle was provided.
54+
#[allow(unused_mut)]
55+
let mut output_type_opt: Option<OutputType> = None;
5456

5557
#[cfg(feature = "paging")]
56-
{
58+
if output_handle.is_none() {
5759
use crate::input::InputKind;
5860
use std::path::Path;
5961

@@ -74,25 +76,35 @@ impl Controller<'_> {
7476

7577
let wrapping_mode = self.config.wrapping_mode;
7678

77-
output_type = OutputType::from_mode(paging_mode, wrapping_mode, self.config.pager)?;
79+
output_type_opt = Some(OutputType::from_mode(
80+
paging_mode,
81+
wrapping_mode,
82+
self.config.pager,
83+
)?);
7884
}
7985

8086
#[cfg(not(feature = "paging"))]
81-
{
82-
output_type = OutputType::stdout();
87+
if output_handle.is_none() {
88+
output_type_opt = Some(OutputType::stdout());
8389
}
8490

85-
let attached_to_pager = output_type.is_pager();
91+
let attached_to_pager = match (&output_handle, &output_type_opt) {
92+
(Some(_), _) => true,
93+
(None, Some(ot)) => ot.is_pager(),
94+
(None, None) => false,
95+
};
96+
8697
let stdout_identifier = if cfg!(windows) || attached_to_pager {
8798
None
8899
} else {
89100
clircle::Identifier::stdout()
90101
};
91102

92-
let mut writer = match output_handle {
93-
Some(OutputHandle::FmtWrite(w)) => OutputHandle::FmtWrite(w),
94-
Some(OutputHandle::IoWrite(w)) => OutputHandle::IoWrite(w),
95-
None => output_type.handle()?,
103+
let mut writer = match (output_handle, &mut output_type_opt) {
104+
(Some(OutputHandle::FmtWrite(w)), _) => OutputHandle::FmtWrite(w),
105+
(Some(OutputHandle::IoWrite(w)), _) => OutputHandle::IoWrite(w),
106+
(None, Some(ot)) => ot.handle()?,
107+
(None, None) => unreachable!("No output handle and no output type available"),
96108
};
97109
let mut no_errors: bool = true;
98110
let stderr = io::stderr();

src/output.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,18 @@ impl OutputType {
141141
//
142142
// For newer versions (530 or 558 on Windows), we omit '--no-init' as it
143143
// is not needed anymore.
144-
match retrieve_less_version(&pager.bin) {
145-
None => {
146-
p.arg("--no-init");
147-
}
148-
Some(LessVersion::Less(version))
149-
if (version < 530 || (cfg!(windows) && version < 558)) =>
150-
{
151-
p.arg("--no-init");
144+
if single_screen_action == SingleScreenAction::Quit {
145+
match retrieve_less_version(&pager.bin) {
146+
None => {
147+
p.arg("--no-init");
148+
}
149+
Some(LessVersion::Less(version))
150+
if (version < 530 || (cfg!(windows) && version < 558)) =>
151+
{
152+
p.arg("--no-init");
153+
}
154+
_ => {}
152155
}
153-
_ => {}
154156
}
155157
} else {
156158
p.args(args);

0 commit comments

Comments
 (0)