Skip to content

Commit

Permalink
Disable error coloring when specified by the user (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia authored Jun 5, 2023
1 parent 9bc43aa commit 796a1be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
20 changes: 14 additions & 6 deletions crates/bws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ enum GetCommand {

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
color_eyre::install()?;

env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

process_commands().await
Expand All @@ -106,6 +104,16 @@ const SERVER_URL_KEY_VAR_NAME: &str = "BWS_SERVER_URL";
async fn process_commands() -> Result<()> {
let cli = Cli::parse();

let color = cli.color.is_enabled();
if color {
color_eyre::install()?;
} else {
// Use an empty theme to disable error coloring
color_eyre::config::HookBuilder::new()
.theme(color_eyre::config::Theme::new())
.install()?;
}

let Some(command) = cli.command else {
let mut cmd = Cli::command();
cmd.print_help()?;
Expand Down Expand Up @@ -198,7 +206,7 @@ async fn process_commands() -> Result<()> {
})
.await?
.data;
serialize_response(projects, cli.output, cli.color);
serialize_response(projects, cli.output, color);
}

Commands::List {
Expand Down Expand Up @@ -226,7 +234,7 @@ async fn process_commands() -> Result<()> {
let secret = client.secrets().get(&SecretGetRequest { id: s.id }).await?;
secrets.push(secret);
}
serialize_response(secrets, cli.output, cli.color);
serialize_response(secrets, cli.output, color);
}

Commands::Get {
Expand All @@ -236,7 +244,7 @@ async fn process_commands() -> Result<()> {
.projects()
.get(&ProjectGetRequest { id: project_id })
.await?;
serialize_response(project, cli.output, cli.color);
serialize_response(project, cli.output, color);
}

Commands::Get {
Expand All @@ -246,7 +254,7 @@ async fn process_commands() -> Result<()> {
.secrets()
.get(&SecretGetRequest { id: secret_id })
.await?;
serialize_response(secret, cli.output, cli.color);
serialize_response(secret, cli.output, color);
}
Commands::Config { .. } => {
unreachable!()
Expand Down
24 changes: 17 additions & 7 deletions crates/bws/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@ pub(crate) enum Color {
Auto,
}

impl Color {
pub(crate) fn is_enabled(self) -> bool {
match self {
Color::No => false,
Color::Yes => true,
Color::Auto => {
if std::env::var("NO_COLOR").is_ok() {
false
} else {
atty::is(atty::Stream::Stdout)
}
}
}
}
}

const ASCII_HEADER_ONLY: &str = " -- ";

pub(crate) fn serialize_response<T: Serialize + TableSerialize<N>, const N: usize>(
data: T,
output: Output,
color: Color,
color: bool,
) {
let color = match color {
Color::No => false,
Color::Yes => true,
Color::Auto => atty::is(atty::Stream::Stdout),
};

match output {
Output::JSON => {
let text = serde_json::to_string_pretty(&data).unwrap();
Expand Down

0 comments on commit 796a1be

Please sign in to comment.