on master help sign in
Fix panic when running `bat --list-languages | head`
5600226mchlrhw
src/main.rs+28−12
@@ -30,6 +30,8 @@ mod terminal;
30
30
31use std::collections::HashSet;
31use std::collections::HashSet;
32use std::io;
32use std::io;
33+use std::io::stdout;
34+use std::io::Write;
33use std::path::Path;
35use std::path::Path;
34use std::process;
36use std::process;
35
37
@@ -80,13 +82,13 @@ fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
80 } else if matches.is_present("clear") {
82 } else if matches.is_present("clear") {
81 clear_assets();
83 clear_assets();
82 } else if matches.is_present("config-dir") {
84 } else if matches.is_present("config-dir") {
83− println!("{}", config_dir());
85+ writeln!(stdout(), "{}", config_dir())?;
84 }
86 }
85
87
86 Ok(())
88 Ok(())
87}
89}
88
90
89−pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
91+pub fn list_languages(assets: &HighlightingAssets, term_width: usize) -> Result<()> {
90 let mut languages = assets
92 let mut languages = assets
91 .syntax_set
93 .syntax_set
92 .syntaxes()
94 .syntaxes()
@@ -107,7 +109,13 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
107 let desired_width = term_width - longest - separator.len();
109 let desired_width = term_width - longest - separator.len();
108
110
109 for lang in languages {
111 for lang in languages {
112+ write!(
113+ stdout(),
114+ "{:width$}{}",
115+ lang.name,
116+ separator,
110− print!("{:width$}{}", lang.name, separator, width = longest);
117+ width = longest
118+ )?;
111
119
112 // Number of characters on this line so far, wrap before `desired_width`
120 // Number of characters on this line so far, wrap before `desired_width`
113 let mut num_chars = 0;
121 let mut num_chars = 0;
@@ -118,20 +126,22 @@ pub fn list_languages(assets: &HighlightingAssets, term_width: usize) {
118 let new_chars = word.len() + comma_separator.len();
126 let new_chars = word.len() + comma_separator.len();
119 if num_chars + new_chars >= desired_width {
127 if num_chars + new_chars >= desired_width {
120 num_chars = 0;
128 num_chars = 0;
121− print!("\n{:width$}{}", "", separator, width = longest);
129+ write!(stdout(), "\n{:width$}{}", "", separator, width = longest)?;
122 }
130 }
123
131
124 num_chars += new_chars;
132 num_chars += new_chars;
125− print!("{}", Green.paint(&word[..]));
133+ write!(stdout(), "{}", Green.paint(&word[..]))?;
126 if extension.peek().is_some() {
134 if extension.peek().is_some() {
127− print!("{}", comma_separator);
135+ write!(stdout(), "{}", comma_separator)?;
128 }
136 }
129 }
137 }
130− println!();
138+ writeln!(stdout())?;
131 }
139 }
140+
141+ Ok(())
132}
142}
133
143
134−pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) {
144+pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) -> Result<()> {
135 let themes = &assets.theme_set.themes;
145 let themes = &assets.theme_set.themes;
136 let mut config = cfg.clone();
146 let mut config = cfg.clone();
137 let mut style = HashSet::new();
147 let mut style = HashSet::new();
@@ -139,11 +149,17 @@ pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) {
139 config.files = vec![InputFile::ThemePreviewFile];
149 config.files = vec![InputFile::ThemePreviewFile];
140 config.output_components = OutputComponents(style);
150 config.output_components = OutputComponents(style);
141 for (theme, _) in themes.iter() {
151 for (theme, _) in themes.iter() {
152+ writeln!(
153+ stdout(),
154+ "Theme: {}\n",
142− println!("Theme: {}\n", Style::new().bold().paint(theme.to_string()));
155+ Style::new().bold().paint(theme.to_string())
156+ )?;
143 config.theme = theme.to_string();
157 config.theme = theme.to_string();
144 let _controller = Controller::new(&config, &assets).run();
158 let _controller = Controller::new(&config, &assets).run();
145− println!()
159+ writeln!(stdout())?;
146 }
160 }
161+
162+ Ok(())
147}
163}
148
164
149/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
165/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
@@ -161,11 +177,11 @@ fn run() -> Result<bool> {
161 let assets = HighlightingAssets::new();
177 let assets = HighlightingAssets::new();
162
178
163 if app.matches.is_present("list-languages") {
179 if app.matches.is_present("list-languages") {
164− list_languages(&assets, config.term_width);
180+ list_languages(&assets, config.term_width)?;
165
181
166 Ok(true)
182 Ok(true)
167 } else if app.matches.is_present("list-themes") {
183 } else if app.matches.is_present("list-themes") {
168− list_themes(&assets, &config);
184+ list_themes(&assets, &config)?;
169
185
170 Ok(true)
186 Ok(true)
171 } else {
187 } else {