Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd)!: add --quiet to suppress log output #748

Merged
merged 5 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 31 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct Pacaptr {
using: Option<String>,

/// Perform a dry run.
#[arg(global = true, long = "dry-run", visible_alias = "dryrun")]
#[arg(global = true, long, visible_alias = "dryrun")]
dry_run: bool,

/// Prevent reinstalling previously installed packages.
Expand All @@ -73,16 +73,20 @@ pub struct Pacaptr {
/// Answer yes to every question.
#[arg(
global = true,
long = "no-confirm",
long,
visible_alias = "noconfirm",
visible_alias = "yes"
)]
no_confirm: bool,

/// Remove cache after installation.
#[arg(global = true, long = "no-cache", visible_alias = "nocache")]
#[arg(global = true, long, visible_alias = "nocache")]
no_cache: bool,

/// Suppress log output.
#[arg(global = true, long, conflicts_with = "dry_run")]
quiet: Option<bool>,

/// Package name or (sometimes) regex.
#[arg(global = true, name = "KEYWORDS")]
keywords: Vec<String>,
Expand Down Expand Up @@ -226,6 +230,7 @@ impl Pacaptr {
needed: self.needed,
no_confirm: self.no_confirm,
no_cache: self.no_cache,
quiet: self.quiet,
default_pm: self.using.clone(),
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,30 @@ pub struct Config {
#[serde(default)]
pub no_cache: bool,

/// Suppress log output.
pub quiet: Option<bool>,

/// The default package manager to be invoked.
pub default_pm: Option<String>,
}

impl Config {
/// Returns the value of the `quiet` flag if it is present,
/// otherwise returns whether the current `stdout` is **not** a TTY.
#[must_use]
pub fn quiet(&self) -> bool {
self.quiet
.unwrap_or_else(|| !console::Term::stdout().is_term())
}

/// Performs a left-biased join of two `Config`s.
pub fn join(&self, other: Self) -> Self {
Self {
dry_run: self.dry_run || other.dry_run,
needed: self.needed || other.dry_run,
no_confirm: self.no_confirm || other.no_confirm,
no_cache: self.no_cache || other.no_cache,
quiet: self.quiet.or(other.quiet),
default_pm: self.default_pm.clone().or(other.default_pm),
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
};

/// Different ways in which a [`Cmd`] shall be dealt with.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Default, Debug)]
pub enum Mode {
/// Solely prints out the command that should be executed and stops.
PrintCmd,
Expand All @@ -44,19 +44,26 @@ pub enum Mode {
///
/// This is potentially dangerous as it destroys the colored `stdout`. Use
/// it only if really necessary.
CheckAll,
CheckAll {
/// Whether the log output should be suppressed.
quiet: bool,
},

/// Prints out the command which should be executed, runs it and collects
/// its `stderr`.
///
/// This will work with a colored `stdout`.
CheckErr,
CheckErr {
/// Whether the log output should be suppressed.
quiet: bool,
},

/// A CUSTOM prompt implemented by a `pacaptr` module itself.
///
/// Prints out the command which should be executed, runs it and collects
/// its `stderr`. Also, this will ask for confirmation before
/// proceeding.
#[default]
Prompt,
}

Expand Down Expand Up @@ -226,12 +233,16 @@ impl Cmd {
Ok(Output::default())
}
Mode::Mute => self.exec_checkall(true).await,
Mode::CheckAll => {
println_quoted(&*prompt::RUNNING, &self);
Mode::CheckAll { quiet } => {
if !quiet {
println_quoted(&*prompt::RUNNING, &self);
}
self.exec_checkall(false).await
}
Mode::CheckErr => {
println_quoted(&*prompt::RUNNING, &self);
Mode::CheckErr { quiet } => {
if !quiet {
println_quoted(&*prompt::RUNNING, &self);
}
self.exec_checkerr(false).await
}
Mode::Prompt => self.exec_prompt(false).await,
Expand Down
Loading
Loading