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

blocks/apt: Added config option ignore_updates_regex to filter the list of updates #1967

Merged
merged 2 commits into from
Nov 15, 2023
Merged
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
23 changes: 21 additions & 2 deletions src/blocks/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
//! `format_up_to_date` | Same as `format`, but for when no updates are available. | `" $icon $count.eng(w:1) "`
//! `warning_updates_regex` | Display block as warning if updates matching regex are available. | `None`
//! `critical_updates_regex` | Display block as critical if updates matching regex are available. | `None`
//! `ignore_updates_regex` | Doesn't include updates matching regex in the count. | `None`
//! `ignore_phased_updates` | Doesn't include potentially held back phased updates in the count. | `false`
//!
//! Placeholder | Value | Type | Unit
Expand Down Expand Up @@ -67,6 +68,7 @@ pub struct Config {
pub format_up_to_date: FormatConfig,
pub warning_updates_regex: Option<String>,
pub critical_updates_regex: Option<String>,
pub ignore_updates_regex: Option<String>,
pub ignore_phased_updates: bool,
}

Expand All @@ -91,6 +93,12 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
.map(Regex::new)
.transpose()
.error("invalid critical updates regex")?;
let ignore_updates_regex = config
.ignore_updates_regex
.as_deref()
.map(Regex::new)
.transpose()
.error("invalid ignore updates regex")?;

let mut cache_dir = env::temp_dir();
cache_dir.push("i3rs-apt");
Expand Down Expand Up @@ -124,7 +132,13 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
loop {
let mut widget = Widget::new();
let updates = get_updates_list(config_file).await?;
let count = get_update_count(config_file, config.ignore_phased_updates, &updates).await?;
let count = get_update_count(
config_file,
config.ignore_phased_updates,
ignore_updates_regex.as_ref(),
&updates,
)
.await?;

widget.set_format(match count {
0 => format_up_to_date.clone(),
Expand Down Expand Up @@ -189,11 +203,16 @@ async fn get_updates_list(config_path: &str) -> Result<String> {
async fn get_update_count(
config_path: &str,
ignore_phased_updates: bool,
ignore_updates_regex: Option<&Regex>,
updates: &str,
) -> Result<usize> {
let mut cnt = 0;

for update_line in updates.lines().filter(|line| line.contains("[upgradable")) {
for update_line in updates
.lines()
.filter(|line| line.contains("[upgradable"))
.filter(|line| ignore_updates_regex.map_or(true, |re| !re.is_match(line)))
{
if !ignore_phased_updates || !is_phased_update(config_path, update_line).await? {
cnt += 1;
}
Expand Down