Skip to content

Commit

Permalink
Fix panic when running bat --list-languages | head
Browse files Browse the repository at this point in the history
  • Loading branch information
mchlrhw committed Aug 31, 2018
1 parent 84734ea commit 24db44a
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand All @@ -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;
Expand All @@ -118,32 +126,40 @@ 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();
style.insert(OutputComponent::Plain);
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
Expand All @@ -161,11 +177,11 @@ fn run() -> Result<bool> {
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 {
Expand Down

0 comments on commit 24db44a

Please sign in to comment.