Skip to content
Merged
Changes from 1 commit
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
25 changes: 23 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,18 @@ 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.is_none() || !ignore_updates_regex.unwrap().is_match(line)
Comment thread
MaxVerevkin marked this conversation as resolved.
Outdated
})
{
if !ignore_phased_updates || !is_phased_update(config_path, update_line).await? {
cnt += 1;
}
Expand Down