on master help sign in
Fix panic when running `bat --list-languages | head`

Fixes: https://github.com/sharkdp/bat/issues/232

commit56002267d26deb5c0cf2cb891ee9c28ef0f07255 authormchlrhw <4028654+mchlrhw@users.noreply.github.com> dateFri 31 Aug 2018, 10:27 (8 years ago) parent84734ea
1 file changed+28−12
src/main.rs+28−12
src/main.rs+28−12
@@ -30,6 +30,8 @@ mod terminal;
3030
3131use std::collections::HashSet;
3232use std::io;
33+use std::io::stdout;
34+use std::io::Write;
3335use std::path::Path;
3436use std::process;
3537
@@ -80,13 +82,13 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
8082 } else if matches.is_present("clear") {
8183 clear_assets();
8284 } else if matches.is_present("config-dir") {
83− println!("{}", config_dir());
85+ writeln!(stdout(), "{}", config_dir())?;
8486 }
8587
8688 Ok(())
8789}
8890
89−pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
91+pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<()> {
9092 let mut languages = assets
9193 .syntax_set
9294 .syntaxes()
@@ -107,7 +109,13 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
107109 let desired_width = term_width - longest - separator.len();
108110
109111 for lang in languages {
110− print!("{:width$}{}", lang.name, separator, width = longest);
112+ write!(
113+ stdout(),
114+ "{:width$}{}",
115+ lang.name,
116+ separator,
117+ width = longest
118+ )?;
111119
112120 // Number of characters on this line so far, wrap before `desired_width`
113121 let mut num_chars = 0;
@@ -118,20 +126,22 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
118126 let new_chars = word.len() + comma_separator.len();
119127 if num_chars + new_chars >= desired_width {
120128 num_chars = 0;
121− print!("\n{:width$}{}", "", separator, width = longest);
129+ write!(stdout(), "\n{:width$}{}", "", separator, width = longest)?;
122130 }
123131
124132 num_chars += new_chars;
125− print!("{}", Green.paint(&word[..]));
133+ write!(stdout(), "{}", Green.paint(&word[..]))?;
126134 if extension.peek().is_some() {
127− print!("{}", comma_separator);
135+ write!(stdout(), "{}", comma_separator)?;
128136 }
129137 }
130− println!();
138+ writeln!(stdout())?;
131139 }
140+
141+ Ok(())
132142}
133143
134−pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) {
144+pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
135145 let themes = &assets.theme_set.themes;
136146 let mut config = cfg.clone();
137147 let mut style = HashSet::new();
@@ -139,11 +149,17 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) {
139149 config.files = vec![InputFile::ThemePreviewFile];
140150 config.output_components = OutputComponents(style);
141151 for (theme, _) in themes.iter() {
142− println!("Theme: {}\n", Style::new().bold().paint(theme.to_string()));
152+ writeln!(
153+ stdout(),
154+ "Theme: {}\n",
155+ Style::new().bold().paint(theme.to_string())
156+ )?;
143157 config.theme = theme.to_string();
144158 let _controller = Controller::new(&config, &assets).run();
145− println!()
159+ writeln!(stdout())?;
146160 }
161+
162+ Ok(())
147163}
148164
149165/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
@@ -161,11 +177,11 @@ fn run() -> Result<bool> {
161177 let assets = HighlightingAssets::new();
162178
163179 if app.matches.is_present("list-languages") {
164− list_languages(&assets, config.term_width);
180+ list_languages(&assets, config.term_width)?;
165181
166182 Ok(true)
167183 } else if app.matches.is_present("list-themes") {
168− list_themes(&assets, &config);
184+ list_themes(&assets, &config)?;
169185
170186 Ok(true)
171187 } else {