From 50d8a1ba06329a18a8839edfa9b8ec6dae6205c1 Mon Sep 17 00:00:00 2001 From: Thibaut CHARLES Date: Sun, 29 Nov 2020 14:15:27 +0100 Subject: [PATCH] feat(network): Ability to change speed unit text (B/s) (#2068) New config option `speed-unit = B/s` will be used to suffix the upload and download speeds. * mod::network: udspeed-unit to set network speed unit suffix * Changed udspeed-unit to speed-unit --- include/adapters/net.hpp | 6 +++--- include/modules/network.hpp | 1 + src/adapters/net.cpp | 16 ++++++++-------- src/modules/network.cpp | 5 +++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/include/adapters/net.hpp b/include/adapters/net.hpp index 97d6d391..fe73fc76 100644 --- a/include/adapters/net.hpp +++ b/include/adapters/net.hpp @@ -82,14 +82,14 @@ namespace net { string ip() const; string ip6() const; - string downspeed(int minwidth = 3) const; - string upspeed(int minwidth = 3) const; + string downspeed(int minwidth = 3, const string& unit = "B/s") const; + string upspeed(int minwidth = 3, const string& unit = "B/s") const; void set_unknown_up(bool unknown = true); protected: void check_tuntap_or_bridge(); bool test_interface() const; - string format_speedrate(float bytes_diff, int minwidth) const; + string format_speedrate(float bytes_diff, int minwidth, const string& unit) const; void query_ip6(); const logger& m_log; diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 62ba08f7..fc7ce594 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -54,6 +54,7 @@ namespace modules { int m_udspeed_minwidth{0}; bool m_accumulate{false}; bool m_unknown_up{false}; + string m_udspeed_unit{"B/s"}; }; } // namespace modules diff --git a/src/adapters/net.cpp b/src/adapters/net.cpp index 5fd5499c..ec89666b 100644 --- a/src/adapters/net.cpp +++ b/src/adapters/net.cpp @@ -153,17 +153,17 @@ namespace net { /** * Get download speed rate */ - string network::downspeed(int minwidth) const { + string network::downspeed(int minwidth, const string& unit) const { float bytes_diff = m_status.current.received - m_status.previous.received; - return format_speedrate(bytes_diff, minwidth); + return format_speedrate(bytes_diff, minwidth, unit); } /** * Get upload speed rate */ - string network::upspeed(int minwidth) const { + string network::upspeed(int minwidth, const string& unit) const { float bytes_diff = m_status.current.transmitted - m_status.previous.transmitted; - return format_speedrate(bytes_diff, minwidth); + return format_speedrate(bytes_diff, minwidth, unit); } /** @@ -223,14 +223,14 @@ namespace net { /** * Format up- and download speed */ - string network::format_speedrate(float bytes_diff, int minwidth) const { + string network::format_speedrate(float bytes_diff, int minwidth, const string& unit) const { // Get time difference in seconds as a float const std::chrono::duration duration = m_status.current.time - m_status.previous.time; float time_diff = duration.count(); float speedrate = bytes_diff / time_diff; - vector suffixes{"GB", "MB"}; - string suffix{"KB"}; + vector suffixes{"G", "M"}; + string suffix{"K"}; while ((speedrate /= 1000) > 999) { suffix = suffixes.back(); @@ -238,7 +238,7 @@ namespace net { } return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speedrate - << " " << suffix << "/s"; + << " " << suffix << unit; } // }}} diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 21138777..39a31a24 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -21,6 +21,7 @@ namespace modules { m_accumulate = m_conf.get(name(), "accumulate-stats", m_accumulate); m_interval = m_conf.get(name(), "interval", 1s); m_unknown_up = m_conf.get(name(), "unknown-as-up", false); + m_udspeed_unit = m_conf.get(name(), "speed-unit", m_udspeed_unit); m_conf.warn_deprecated(name(), "udspeed-minwidth", "%downspeed:min:max% and %upspeed:min:max%"); @@ -109,8 +110,8 @@ namespace modules { m_counter = 0; } - auto upspeed = network->upspeed(m_udspeed_minwidth); - auto downspeed = network->downspeed(m_udspeed_minwidth); + auto upspeed = network->upspeed(m_udspeed_minwidth, m_udspeed_unit); + auto downspeed = network->downspeed(m_udspeed_minwidth, m_udspeed_unit); // Update label contents const auto replace_tokens = [&](label_t& label) {