Skip to content

Commit

Permalink
refactor: Use same hashmap while setting the values
Browse files Browse the repository at this point in the history
  • Loading branch information
IshanGrover2004 committed Jan 21, 2024
1 parent 61902f4 commit 60ff705
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
28 changes: 15 additions & 13 deletions src/blocks/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
}

loop {
let mut package_manager_map: HashMap<&str, u32> =
[("apt", 0), ("pacman", 0), ("aur", 0), ("dnf", 0)].into();
let mut package_manager_map: HashMap<Cow<'static, str>, Value> = HashMap::new();

let mut critical = false;
let mut warning = false;
Expand All @@ -309,7 +308,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let updates_count = updates.len();

package_manager_map.insert(package_manager.name(), updates_count as u32);
package_manager_map.insert(package_manager.name(), Value::number(updates_count));

warning |= warning_updates_regex
.as_ref()
Expand All @@ -321,20 +320,16 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {

let mut widget = Widget::new();

let total_count = package_manager_map.values().sum();
let total_count = package_manager_map.values().map(get_inner_value).sum();
package_manager_map.insert("icon".into(), Value::icon("update"));
package_manager_map.insert("total".into(), Value::number(total_count));

widget.set_format(match total_count {
0 => format_up_to_date.clone(),
1 => format_singular.clone(),
_ => format.clone(),
});
widget.set_values(map!(
"icon" => Value::icon("update"),
"apt" => Value::number(package_manager_map["apt"]),
"pacman" => Value::number(package_manager_map["pacman"]),
"aur" => Value::number(package_manager_map["aur"]),
"dnf" => Value::number(package_manager_map["dnf"]),
"total" => Value::number(total_count),
));
widget.set_values(package_manager_map);

widget.state = match total_count {
0 => State::Idle,
Expand All @@ -357,9 +352,16 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
}
}

fn get_inner_value(value: &Value) -> u32 {
match value.inner {
crate::formatting::value::ValueInner::Number { val, unit: _ } => val as u32,
_ => 0,
}
}

#[async_trait]
pub trait Backend {
fn name(&self) -> &str;
fn name(&self) -> Cow<'static, str>;

async fn get_updates_list(&self) -> Result<Vec<String>>;
}
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/packages/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ impl Apt {

#[async_trait]
impl Backend for Apt {
fn name(&self) -> &str {
"apt"
fn name(&self) -> Cow<'static, str> {
"apt".into()
}

async fn get_updates_list(&self) -> Result<Vec<String>> {
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/packages/dnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl Dnf {

#[async_trait]
impl Backend for Dnf {
fn name(&self) -> &str {
"dnf"
fn name(&self) -> Cow<'static, str> {
"dnf".into()
}

async fn get_updates_list(&self) -> Result<Vec<String>> {
Expand Down
8 changes: 4 additions & 4 deletions src/blocks/packages/pacman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl Aur {

#[async_trait]
impl Backend for Pacman {
fn name(&self) -> &str {
"pacman"
fn name(&self) -> Cow<'static, str> {
"pacman".into()
}

async fn get_updates_list(&self) -> Result<Vec<String>> {
Expand Down Expand Up @@ -127,8 +127,8 @@ impl Backend for Pacman {

#[async_trait]
impl Backend for Aur {
fn name(&self) -> &str {
"aur"
fn name(&self) -> Cow<'static, str> {
"aur".into()
}

async fn get_updates_list(&self) -> Result<Vec<String>> {
Expand Down

0 comments on commit 60ff705

Please sign in to comment.