foundry_cli/opts/
global.rsuse clap::{ArgAction, Parser};
use foundry_common::shell::{ColorChoice, OutputFormat, OutputMode, Shell, Verbosity};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Serialize, Deserialize, Parser)]
pub struct GlobalOpts {
#[arg(help_heading = "Display options", global = true, short, long, verbatim_doc_comment, conflicts_with = "quiet", action = ArgAction::Count)]
verbosity: Verbosity,
#[arg(help_heading = "Display options", global = true, short, long, alias = "silent")]
quiet: bool,
#[arg(help_heading = "Display options", global = true, long, alias = "format-json", conflicts_with_all = &["quiet", "color"])]
json: bool,
#[arg(help_heading = "Display options", global = true, long, value_enum)]
color: Option<ColorChoice>,
#[arg(global = true, long, short = 'j', visible_alias = "jobs")]
threads: Option<usize>,
}
impl GlobalOpts {
pub fn init(&self) -> eyre::Result<()> {
self.shell().set();
if self.threads.is_some() {
self.force_init_thread_pool()?;
}
Ok(())
}
pub fn force_init_thread_pool(&self) -> eyre::Result<()> {
init_thread_pool(self.threads.unwrap_or(0))
}
pub fn shell(&self) -> Shell {
let mode = match self.quiet {
true => OutputMode::Quiet,
false => OutputMode::Normal,
};
let color = self.json.then_some(ColorChoice::Never).or(self.color).unwrap_or_default();
let format = match self.json {
true => OutputFormat::Json,
false => OutputFormat::Text,
};
Shell::new_with(format, mode, color, self.verbosity)
}
}
pub fn init_thread_pool(threads: usize) -> eyre::Result<()> {
rayon::ThreadPoolBuilder::new()
.thread_name(|i| format!("foundry-{i}"))
.num_threads(threads)
.build_global()?;
Ok(())
}