Repository: sharkdp/bat
File: src/main.rs (Rust)

Code before:
        println!();
    }
}

pub fn list_themes(assets: &HighlightingAssets, config: &mut Config) {
    let themes = &assets.theme_set.themes;
    config.files = vec![Some("assets/hello.rs")];
    for (theme, _) in themes.iter() {
        println!("{}", theme);
        config.theme = theme.to_string();
        let _controller = Controller::new(&config, &assets).run();
    }
}

/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
/// `Some(false)` if any intermediate errors occurred (were printed).
fn run() -> Result<bool> {
    let app = App::new();

    match app.matches.subcommand() {
        ("cache", Some(cache_matches)) => {
            run_cache_subcommand(cache_matches)?;
            Ok(true)
        }
        _ => {
            let mut config = app.config()?;
            let assets = HighlightingAssets::new();

            if app.matches.is_present("list-languages") {
                list_languages(&assets, config.term_width);

                Ok(true)
            } else if app.matches.is_present("list-themes") {
                list_themes(&assets, &mut config);

                Ok(true)
            } else {
                let controller = Controller::new(&config, &assets);
                controller.run()
            }
        }
    }
}

fn main() {
    let result = run();

    match result {
        Err(error) => {
            handle_error(&error);
            process::exit(1);

Review comment (performance):
Can we try to avoid the `mut` here and just copy the `Config` object inside `list_themes`? That shouldn't be expensive.

Code after:
                print!("{}", comma_separator);
            }
        }
        println!();
    }
}

pub fn list_themes(assets: &HighlightingAssets, cfg: &Config) {
    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()));
        config.theme = theme.to_string();
        let _controller = Controller::new(&config, &assets).run();
        println!()
    }
}

/// Returns `Err(..)` upon fatal errors. Otherwise, returns `Some(true)` on full success and
/// `Some(false)` if any intermediate errors occurred (were printed).
fn run() -> Result<bool> {
    let app = App::new();

    match app.matches.subcommand() {
        ("cache", Some(cache_matches)) => {
            run_cache_subcommand(cache_matches)?;
            Ok(true)
        }
        _ => {
            let config = app.config()?;
            let assets = HighlightingAssets::new();

            if app.matches.is_present("list-languages") {
                list_languages(&assets, config.term_width);

                Ok(true)
            } else if app.matches.is_present("list-themes") {
                list_themes(&assets, &config);

                Ok(true)
            } else {
                let controller = Controller::new(&config, &assets);
                controller.run()
            }
        }
    }
}