make bat -L use pager

This commit is contained in:
Stefan Kunkel 2020-11-27 10:16:19 +01:00 committed by David Peter
parent cc7b89faf8
commit cffacad306
2 changed files with 15 additions and 13 deletions

View File

@ -2,6 +2,7 @@
## Features ## Features
- make `bat -L` use built-in pager
## Bugfixes ## Bugfixes

View File

@ -78,7 +78,9 @@ fn get_syntax_mapping_to_paths<'a>(
map map
} }
pub fn list_languages(config: &Config) -> Result<()> { pub fn get_languages(config: &Config) -> Result<String> {
let mut result: String = String::new();
let assets = assets_from_cache_or_binary()?; let assets = assets_from_cache_or_binary()?;
let mut languages = assets let mut languages = assets
.syntaxes() .syntaxes()
@ -119,12 +121,9 @@ pub fn list_languages(config: &Config) -> Result<()> {
} }
} }
let stdout = io::stdout();
let mut stdout = stdout.lock();
if config.loop_through { if config.loop_through {
for lang in languages { for lang in languages {
writeln!(stdout, "{}:{}", lang.name, lang.file_extensions.join(","))?; result += &format!("{}:{}\n", lang.name, lang.file_extensions.join(","));
} }
} else { } else {
let longest = languages let longest = languages
@ -145,7 +144,7 @@ pub fn list_languages(config: &Config) -> Result<()> {
}; };
for lang in languages { for lang in languages {
write!(stdout, "{:width$}{}", lang.name, separator, width = longest)?; result += &format!("{:width$}{}", lang.name, separator, width = longest);
// Number of characters on this line so far, wrap before `desired_width` // Number of characters on this line so far, wrap before `desired_width`
let mut num_chars = 0; let mut num_chars = 0;
@ -156,20 +155,20 @@ pub fn list_languages(config: &Config) -> Result<()> {
let new_chars = word.len() + comma_separator.len(); let new_chars = word.len() + comma_separator.len();
if num_chars + new_chars >= desired_width { if num_chars + new_chars >= desired_width {
num_chars = 0; num_chars = 0;
write!(stdout, "\n{:width$}{}", "", separator, width = longest)?; result += &format!("\n{:width$}{}", "", separator, width = longest);
} }
num_chars += new_chars; num_chars += new_chars;
write!(stdout, "{}", style.paint(&word[..]))?; result += &format!("{}", style.paint(&word[..]));
if extension.peek().is_some() { if extension.peek().is_some() {
write!(stdout, "{}", comma_separator)?; result += &format!("{}", comma_separator);
} }
} }
writeln!(stdout)?; result += "\n";
} }
} }
Ok(()) Ok(result)
} }
fn theme_preview_file<'a>() -> Input<'a> { fn theme_preview_file<'a>() -> Input<'a> {
@ -248,8 +247,10 @@ fn run() -> Result<bool> {
let config = app.config(&inputs)?; let config = app.config(&inputs)?;
if app.matches.is_present("list-languages") { if app.matches.is_present("list-languages") {
list_languages(&config)?; let languages: String = get_languages(&config)?;
Ok(true) let inputs: Vec<Input> = vec!(Input::from_reader(Box::new(languages.as_bytes())));
let config = app.config(&inputs)?;
run_controller(inputs, &config)
} else if app.matches.is_present("list-themes") { } else if app.matches.is_present("list-themes") {
list_themes(&config)?; list_themes(&config)?;
Ok(true) Ok(true)