From af6ef01ea619a96767ffbd5700ad02861fefb9a0 Mon Sep 17 00:00:00 2001
From: Michael Carlberg <c@rlberg.se>
Date: Wed, 1 Jun 2016 08:18:11 +0200
Subject: [PATCH] fix(cpu): Bad value for total load percentage

---
 src/modules/cpu.cpp | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/modules/cpu.cpp b/src/modules/cpu.cpp
index 5b547659..36e06c7b 100644
--- a/src/modules/cpu.cpp
+++ b/src/modules/cpu.cpp
@@ -37,11 +37,14 @@ bool CpuModule::update()
   if (!this->read_values())
     return false;
 
-  this->current_total_load = 0;
+  this->current_total_load = 0.0f;
   this->current_load.clear();
 
   int cores_n = this->cpu_times.size();
 
+  if (cores_n == 0)
+    return false;
+
   repeat(cores_n)
   {
     auto load = this->get_load(repeat_i_rev(cores_n));
@@ -49,9 +52,10 @@ bool CpuModule::update()
     this->current_load.emplace_back(load);
   }
 
+  this->current_total_load = this->current_total_load / float(cores_n);
+
   this->label_tokenized->text = this->label->text;
-  this->label_tokenized->replace_token("%percentage%",
-    std::to_string((int) this->current_total_load / cores_n)+"%");
+  this->label_tokenized->replace_token("%percentage%", std::to_string((int)(this->current_total_load + 0.5f)) +"%");
 
   return true;
 }
@@ -61,14 +65,14 @@ bool CpuModule::build(Builder *builder, const std::string& tag)
   if (tag == TAG_LABEL)
     builder->node(this->label_tokenized);
   else if (tag == TAG_BAR_LOAD)
-    builder->node(this->bar_load, (int) this->current_total_load);
+    builder->node(this->bar_load, this->current_total_load);
   else if (tag == TAG_RAMP_LOAD)
-    builder->node(this->ramp_load, (int) this->current_total_load);
+    builder->node(this->ramp_load, this->current_total_load);
   else if (tag == TAG_RAMP_LOAD_PER_CORE) {
     int i = 0;
     for (auto &&load : this->current_load) {
       if (i++ > 0) builder->space(1);
-      builder->node(this->ramp_load_per_core, (int) load);
+      builder->node(this->ramp_load_per_core, load);
     }
     builder->node(builder->flush());