Skip to content

Commit

Permalink
add option to suppress color
Browse files Browse the repository at this point in the history
  • Loading branch information
hallettj committed Nov 22, 2024
1 parent 8eb6fe6 commit 147701a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub enum Command {
pub struct Context {
pub path: PathBuf,
pub connection_uri: Option<String>,
pub display_color: bool,
}

/// Run a command in a given directory.
Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub struct Args {
)]
pub connection_uri: Option<String>,

/// Disable color in command output.
#[arg(long = "no-color", short = 'C')]
pub no_color: bool,

/// The command to invoke.
#[command(subcommand)]
pub subcommand: Command,
Expand All @@ -49,6 +53,7 @@ pub async fn main() -> anyhow::Result<()> {
let context = Context {
path,
connection_uri: args.connection_uri,
display_color: !args.no_color,
};
run(args.subcommand, &context).await?;
Ok(())
Expand Down
21 changes: 10 additions & 11 deletions crates/cli/src/native_query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ async fn delete(context: &Context, native_query_name: &str) -> anyhow::Result<()

async fn show(context: &Context, native_query_name: &str) -> anyhow::Result<()> {
let (native_query, path) = find_native_query(context, native_query_name).await?;
pretty_print_native_query(
&mut StandardStream::stdout(ColorChoice::Auto),
&native_query,
&path,
)
.await?;
pretty_print_native_query(&mut stdout(context), &native_query, &path).await?;
Ok(())
}

Expand Down Expand Up @@ -184,11 +179,7 @@ async fn create(
native_query_path.to_string_lossy()
);
eprintln!();
pretty_print_native_query_info(
&mut StandardStream::stdout(ColorChoice::Auto),
&native_query.value,
)
.await?;
pretty_print_native_query_info(&mut stdout(context), &native_query.value).await?;
Ok(())
}

Expand Down Expand Up @@ -293,3 +284,11 @@ pub fn native_query_from_pipeline(
description: None,
})
}

fn stdout(context: &Context) -> StandardStream {
if context.display_color {
StandardStream::stdout(ColorChoice::Auto)
} else {
StandardStream::stdout(ColorChoice::Never)
}
}

0 comments on commit 147701a

Please sign in to comment.