From c30159abc68523b51aefd5894dd6a8f7168baab2 Mon Sep 17 00:00:00 2001
From: patrick96 <p.ziegler96@gmail.com>
Date: Thu, 15 Feb 2018 10:33:20 +0100
Subject: [PATCH] feat(temp): Add units option

Avoids having lots of tokens
---
 include/modules/temperature.hpp |  3 +++
 src/modules/temperature.cpp     | 18 +++++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/include/modules/temperature.hpp b/include/modules/temperature.hpp
index 75ee57f8..e69cb1a2 100644
--- a/include/modules/temperature.hpp
+++ b/include/modules/temperature.hpp
@@ -32,6 +32,9 @@ namespace modules {
     int m_tempwarn = 0;
     int m_temp = 0;
     int m_perc = 0;
+
+    // Whether or not to show units with the %temperature-X% tokens
+    bool m_units{true};
   };
 }
 
diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp
index 642ec380..a7dde4aa 100644
--- a/src/modules/temperature.cpp
+++ b/src/modules/temperature.cpp
@@ -19,6 +19,7 @@ namespace modules {
     m_path = m_conf.get(name(), "hwmon-path", ""s);
     m_tempwarn = m_conf.get(name(), "warn-temperature", 80);
     m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 1s);
+    m_units = m_conf.get(name(), "units", m_units);
 
     if (m_path.empty()) {
       m_path = string_util::replace(PATH_TEMPERATURE_INFO, "%zone%", to_string(m_zone));
@@ -53,15 +54,22 @@ namespace modules {
     int m_temp_f = floor(((1.8 * m_temp) + 32) + 0.5);
     m_perc = math_util::cap(math_util::percentage(m_temp, 0, m_tempwarn), 0, 100);
 
+    string temp_c_string = to_string(m_temp);
+    string temp_f_string = to_string(m_temp_f);
+
+    // Add units if `units = true` in config
+    if(m_units) {
+      temp_c_string += "°C";
+      temp_f_string += "°F";
+    }
+
     const auto replace_tokens = [&](label_t& label) {
       label->reset_tokens();
-      label->replace_token("%temperature-f%", to_string(m_temp_f) + "°F");
-      label->replace_token("%temperature-c%", to_string(m_temp) + "°C");
-      label->replace_token("%temperature-f-n%", to_string(m_temp_f));
-      label->replace_token("%temperature-c-n%", to_string(m_temp));
+      label->replace_token("%temperature-f%", temp_f_string);
+      label->replace_token("%temperature-c%", temp_c_string);
 
       // DEPRECATED: Will be removed in later release
-      label->replace_token("%temperature%", to_string(m_temp) + "°C");
+      label->replace_token("%temperature%", temp_c_string);
     };
 
     if (m_label[temp_state::NORMAL]) {