From 56002267d26deb5c0cf2cb891ee9c28ef0f07255 Mon Sep 17 00:00:00 2001 From: mchlrhw <4028654+mchlrhw@users.noreply.github.com> Date: Fri, 31 Aug 2018 10:27:29 +0100 Subject: [PATCH] Fix panic when running `bat --list-languages | head` Fixes: https://github.com/sharkdp/bat/issues/232 --- src/main.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 445b55da..af2749a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,8 @@ mod terminal; use std::collections::HashSet; use std::io; +use std::io::stdout; +use std::io::Write; use std::path::Path; use std::process; @@ -80,13 +82,13 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> { } else if matches.is_present("clear") { clear_assets(); } else if matches.is_present("config-dir") { - println!("{}", config_dir()); + writeln!(stdout(), "{}", config_dir())?; } Ok(()) } -pub fn list_languages(assets: &HighlightingAssets, term_width: usize) { +pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<()> { let mut languages = assets .syntax_set .syntaxes() @@ -107,7 +109,13 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) { let desired_width = term_width - longest - separator.len(); for lang in languages { - print!("{:width$}{}", lang.name, separator, width = longest); + write!( + stdout(), + "{:width$}{}", + lang.name, + separator, + width = longest + )?; // Number of characters on this line so far, wrap before `desired_width` let mut num_chars = 0; @@ -118,20 +126,22 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) { let new_chars = word.len() + comma_separator.len(); if num_chars + new_chars >= desired_width { num_chars = 0; - print!("\n{:width$}{}", "", separator, width = longest); + write!(stdout(), "\n{:width$}{}", "", separator, width = longest)?; } num_chars += new_chars; - print!("{}", Green.paint(&word[..])); + write!(stdout(), "{}", Green.paint(&word[..]))?; if extension.peek().is_some() { - print!("{}", comma_separator); + write!(stdout(), "{}", comma_separator)?; } } - println!(); + writeln!(stdout())?; } + + Ok(()) } -pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) { +pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> { let themes = &assets.theme_set.themes; let mut config = cfg.clone(); let mut style = HashSet::new(); @@ -139,11 +149,17 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) { config.files = vec![InputFile::ThemePreviewFile]; config.output_components = OutputComponents(style); for (theme, _) in themes.iter() { - println!("Theme: {}\n", Style::new().bold().paint(theme.to_string())); + writeln!( + stdout(), + "Theme: {}\n", + Style::new().bold().paint(theme.to_string()) + )?; config.theme = theme.to_string(); let _controller = Controller::new(&config, &assets).run(); - println!() + writeln!(stdout())?; } + + Ok(()) } /// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and @@ -161,11 +177,11 @@ fn run() -> Result { let assets = HighlightingAssets::new(); if app.matches.is_present("list-languages") { - list_languages(&assets, config.term_width); + list_languages(&assets, config.term_width)?; Ok(true) } else if app.matches.is_present("list-themes") { - list_themes(&assets, &config); + list_themes(&assets, &config)?; Ok(true) } else {