Skip to content
Myr

main.rs

This file

Path

src/main.rs

76 lines · 2.0 KiB · Rust

Last changed by

Merge pull request #967 from sharkdp/codex/quantity-formattingDavid Peterbefore adoptiona6c3010

History of this file

Take it

1#![cfg_attr(2    all(windows, feature = "windows_process_extensions_main_thread_handle"),3    feature(windows_process_extensions_main_thread_handle)4)]56use std::env;7use std::io::{self, Write};89use benchmark::scheduler::Scheduler;10use cli::get_cli_arguments;11use command::Commands;12use error::ConsoleOutputError;13use export::ExportManager;14use options::Options;1516use anyhow::Result;17use colored::*;1819pub mod benchmark;20pub mod cli;21pub mod command;22pub mod error;23pub mod export;24pub mod options;25pub mod outlier_detection;26pub mod output;27pub mod parameter;28pub mod quantity;29pub mod timer;30pub mod util;3132fn run() -> Result<()> {33    // Enabled ANSI colors on Windows 1034    #[cfg(windows)]35    colored::control::set_virtual_terminal(true).unwrap();3637    let cli_arguments = get_cli_arguments(env::args_os());38    let mut options = Options::from_cli_arguments(&cli_arguments)?;39    let commands = Commands::from_cli_arguments(&cli_arguments)?;40    options.validate_against_command_list(&commands)?;4142    let export_manager = ExportManager::from_cli_arguments(43        &cli_arguments,44        options.time_unit,45        options.sort_order_exports,46    )?;4748    let mut scheduler = Scheduler::new(&commands, &options, &export_manager);49    scheduler.run_benchmarks()?;50    scheduler.print_relative_speed_comparison()?;51    scheduler.final_export()?;5253    Ok(())54}5556fn caused_by_broken_console_pipe(error: &anyhow::Error) -> bool {57    error58        .downcast_ref::<ConsoleOutputError>()59        .is_some_and(|e| e.0.kind() == io::ErrorKind::BrokenPipe)60}6162fn main() {63    match run() {64        Ok(_) => {}65        Err(e) => {66            // Exit quietly when a reader stops consuming console output early.67            if caused_by_broken_console_pipe(&e) {68                std::process::exit(0);69            }70            // The write to stderr can itself fail if stderr is closed; the71            // error message cannot be shown in that case anyway.72            let _ = writeln!(io::stderr(), "{} {:#}", "Error:".red(), e);73            std::process::exit(1);74        }75    }76}