Skip to content

Commit 57629bc

Browse files
authored
Merge pull request #3239 from einfachIrgendwer0815/feature/page_list_themes
Add paging to `--list-themes`
2 parents fc7dff5 + 5edaa96 commit 57629bc

File tree

9 files changed

+56
-40
lines changed

9 files changed

+56
-40
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Features
44

5+
- Add paging to `--list-themes`, see PR #3239 (@einfachIrgendwer0815)
6+
57
## Bugfixes
68

79
- Fix `BAT_THEME_DARK` and `BAT_THEME_LIGHT` being ignored, see issue #3171 and PR #3168 (@bash)

examples/buffer.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use bat::{assets::HighlightingAssets, config::Config, controller::Controller, Input};
1+
use bat::{
2+
assets::HighlightingAssets, config::Config, controller::Controller, output::OutputHandle, Input,
3+
};
24

35
fn main() {
46
let mut buffer = String::new();
@@ -10,7 +12,10 @@ fn main() {
1012
let controller = Controller::new(&config, &assets);
1113
let input = Input::from_file(file!());
1214
controller
13-
.run(vec![input.into()], Some(&mut buffer))
15+
.run(
16+
vec![input.into()],
17+
Some(OutputHandle::FmtWrite(&mut buffer)),
18+
)
1419
.unwrap();
1520

1621
println!("{buffer}");

src/bin/bat/app.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ impl App {
124124
// If we have -pp as an option when in auto mode, the pager should be disabled.
125125
if extra_plain || self.matches.get_flag("no-paging") {
126126
PagingMode::Never
127-
} else if inputs.iter().any(Input::is_stdin) {
127+
} else if inputs.iter().any(Input::is_stdin)
128+
// ignore stdin when --list-themes is used because in that case no input will be read anyways
129+
&& !self.matches.get_flag("list-themes")
130+
{
128131
// If we are reading from stdin, only enable paging if we write to an
129132
// interactive terminal and if we do not *read* from an interactive
130133
// terminal.

src/bin/bat/main.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use std::io::{BufReader, Write};
1616
use std::path::Path;
1717
use std::process;
1818

19+
use bat::output::{OutputHandle, OutputType};
1920
use bat::theme::DetectColorScheme;
2021
use nu_ansi_term::Color::Green;
2122
use nu_ansi_term::Style;
@@ -205,8 +206,9 @@ pub fn list_themes(
205206
config.language = Some("Rust");
206207
config.style_components = StyleComponents(style);
207208

208-
let stdout = io::stdout();
209-
let mut stdout = stdout.lock();
209+
let mut output_type =
210+
OutputType::from_mode(config.paging_mode, config.wrapping_mode, config.pager)?;
211+
let mut writer = output_type.handle()?;
210212

211213
let default_theme_name = default_theme(color_scheme(detect_color_scheme).unwrap_or_default());
212214
for theme in assets.themes() {
@@ -221,26 +223,29 @@ pub fn list_themes(
221223
};
222224
if config.colored_output {
223225
writeln!(
224-
stdout,
226+
writer,
225227
"Theme: {}{}\n",
226228
Style::new().bold().paint(theme.to_string()),
227229
default_theme_info
228230
)?;
229231
config.theme = theme.to_string();
230232
Controller::new(&config, &assets)
231-
.run(vec![theme_preview_file()], None)
233+
.run(
234+
vec![theme_preview_file()],
235+
Some(OutputHandle::IoWrite(&mut writer)),
236+
)
232237
.ok();
233-
writeln!(stdout)?;
238+
writeln!(writer)?;
234239
} else if config.loop_through {
235-
writeln!(stdout, "{theme}")?;
240+
writeln!(writer, "{theme}")?;
236241
} else {
237-
writeln!(stdout, "{theme}{default_theme_info}")?;
242+
writeln!(writer, "{theme}{default_theme_info}")?;
238243
}
239244
}
240245

241246
if config.colored_output {
242247
writeln!(
243-
stdout,
248+
writer,
244249
"Further themes can be installed to '{}', \
245250
and are added to the cache with `bat cache --build`. \
246251
For more information, see:\n\n \

src/controller.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use crate::lessopen::LessOpenPreprocessor;
1111
#[cfg(feature = "git")]
1212
use crate::line_range::LineRange;
1313
use crate::line_range::{LineRanges, RangeCheckResult};
14-
use crate::output::OutputType;
14+
use crate::output::{OutputHandle, OutputType};
1515
#[cfg(feature = "paging")]
1616
use crate::paging::PagingMode;
17-
use crate::printer::{InteractivePrinter, OutputHandle, Printer, SimplePrinter};
17+
use crate::printer::{InteractivePrinter, Printer, SimplePrinter};
1818

1919
use clircle::{Clircle, Identifier};
2020

@@ -35,18 +35,14 @@ impl Controller<'_> {
3535
}
3636
}
3737

38-
pub fn run(
39-
&self,
40-
inputs: Vec<Input>,
41-
output_buffer: Option<&mut dyn std::fmt::Write>,
42-
) -> Result<bool> {
43-
self.run_with_error_handler(inputs, output_buffer, default_error_handler)
38+
pub fn run(&self, inputs: Vec<Input>, output_handle: Option<OutputHandle<'_>>) -> Result<bool> {
39+
self.run_with_error_handler(inputs, output_handle, default_error_handler)
4440
}
4541

4642
pub fn run_with_error_handler(
4743
&self,
4844
inputs: Vec<Input>,
49-
output_buffer: Option<&mut dyn std::fmt::Write>,
45+
output_handle: Option<OutputHandle<'_>>,
5046
mut handle_error: impl FnMut(&Error, &mut dyn Write),
5147
) -> Result<bool> {
5248
let mut output_type;
@@ -88,8 +84,9 @@ impl Controller<'_> {
8884
clircle::Identifier::stdout()
8985
};
9086

91-
let mut writer = match output_buffer {
92-
Some(buf) => OutputHandle::FmtWrite(buf),
87+
let mut writer = match output_handle {
88+
Some(OutputHandle::FmtWrite(w)) => OutputHandle::FmtWrite(w),
89+
Some(OutputHandle::IoWrite(w)) => OutputHandle::IoWrite(w),
9390
None => OutputHandle::IoWrite(output_type.handle()?),
9491
};
9592
let mut no_errors: bool = true;

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod less;
3838
mod lessopen;
3939
pub mod line_range;
4040
pub(crate) mod nonprintable_notation;
41-
mod output;
41+
pub mod output;
4242
#[cfg(feature = "paging")]
4343
mod pager;
4444
#[cfg(feature = "paging")]

src/output.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::io::{self, Write};
23
#[cfg(feature = "paging")]
34
use std::process::Child;
@@ -162,3 +163,17 @@ impl Drop for OutputType {
162163
}
163164
}
164165
}
166+
167+
pub enum OutputHandle<'a> {
168+
IoWrite(&'a mut dyn io::Write),
169+
FmtWrite(&'a mut dyn fmt::Write),
170+
}
171+
172+
impl OutputHandle<'_> {
173+
pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result<()> {
174+
match self {
175+
Self::IoWrite(handle) => handle.write_fmt(args).map_err(Into::into),
176+
Self::FmtWrite(handle) => handle.write_fmt(args).map_err(Into::into),
177+
}
178+
}
179+
}

src/pretty_printer.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
error::Result,
1111
input,
1212
line_range::{HighlightedLineRanges, LineRange, LineRanges},
13+
output::OutputHandle,
1314
style::StyleComponent,
1415
StripAnsiMode, SyntaxMapping, WrappingMode,
1516
};
@@ -325,7 +326,10 @@ impl<'a> PrettyPrinter<'a> {
325326

326327
// If writer is provided, pass it to the controller, otherwise pass None
327328
if let Some(mut w) = writer {
328-
controller.run(inputs.into_iter().map(|i| i.into()).collect(), Some(&mut w))
329+
controller.run(
330+
inputs.into_iter().map(|i| i.into()).collect(),
331+
Some(OutputHandle::FmtWrite(&mut w)),
332+
)
329333
} else {
330334
controller.run(inputs.into_iter().map(|i| i.into()).collect(), None)
331335
}

src/printer.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt;
2-
use std::io;
31
use std::vec::Vec;
42

53
use nu_ansi_term::Color::{Fixed, Green, Red, Yellow};
@@ -29,6 +27,7 @@ use crate::diff::LineChanges;
2927
use crate::error::*;
3028
use crate::input::OpenedInput;
3129
use crate::line_range::RangeCheckResult;
30+
use crate::output::OutputHandle;
3231
use crate::preprocessor::strip_ansi;
3332
use crate::preprocessor::{expand_tabs, replace_nonprintable};
3433
use crate::style::StyleComponent;
@@ -68,20 +67,6 @@ const EMPTY_SYNTECT_STYLE: syntect::highlighting::Style = syntect::highlighting:
6867
font_style: FontStyle::empty(),
6968
};
7069

71-
pub enum OutputHandle<'a> {
72-
IoWrite(&'a mut dyn io::Write),
73-
FmtWrite(&'a mut dyn fmt::Write),
74-
}
75-
76-
impl OutputHandle<'_> {
77-
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result<()> {
78-
match self {
79-
Self::IoWrite(handle) => handle.write_fmt(args).map_err(Into::into),
80-
Self::FmtWrite(handle) => handle.write_fmt(args).map_err(Into::into),
81-
}
82-
}
83-
}
84-
8570
pub(crate) trait Printer {
8671
fn print_header(
8772
&mut self,

0 commit comments

Comments
 (0)