From ab915fb724546e7abfcb35cbaf863ce2bca613ec Mon Sep 17 00:00:00 2001 From: Prateek Sunal <41370460+prateekmedia@users.noreply.github.com> Date: Mon, 7 Feb 2022 01:42:38 +0530 Subject: [PATCH] feat(net): %netspeed% token to show combined up+download speed (#2590) Closes #1083 * Add netspeed parameter (#1083) * Update net.cpp * Update net.hpp * Update network.cpp * Update CHANGELOG.md --- CHANGELOG.md | 1 + include/adapters/net.hpp | 1 + src/adapters/net.cpp | 9 +++++++++ src/modules/network.cpp | 2 ++ 4 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c250fa1..8b789c10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - For module actions, you can now also specify the module name, action name, and optional data as separate arguments. - Added man page +- `internal/network`: New token `%netspeed%` that provides the total speed of the internet (up + down speed) ([#2590](https://github.com/polybar/polybar/issues/2590)) ### Changed - Polybar now also reads `config.ini` when searching for config files. diff --git a/include/adapters/net.hpp b/include/adapters/net.hpp index 05a13036..40ed214d 100644 --- a/include/adapters/net.hpp +++ b/include/adapters/net.hpp @@ -90,6 +90,7 @@ namespace net { string mac() const; string downspeed(int minwidth = 3, const string& unit = "B/s") const; string upspeed(int minwidth = 3, const string& unit = "B/s") const; + string netspeed(int minwidth = 3, const string& unit = "B/s") const; void set_unknown_up(bool unknown = true); protected: diff --git a/src/adapters/net.cpp b/src/adapters/net.cpp index b8334924..97e8ab5f 100644 --- a/src/adapters/net.cpp +++ b/src/adapters/net.cpp @@ -274,6 +274,15 @@ namespace net { float bytes_diff = m_status.current.transmitted - m_status.previous.transmitted; return format_speedrate(bytes_diff, minwidth, unit); } + + /** + * Get total net speed rate + */ + string network::netspeed(int minwidth, const string& unit) const { + float bytes_diff = m_status.current.received - m_status.previous.received + + m_status.current.transmitted - m_status.previous.transmitted; + return format_speedrate(bytes_diff, minwidth, unit); + } /** * Set if unknown counts as up diff --git a/src/modules/network.cpp b/src/modules/network.cpp index 78199b89..f8ecb490 100644 --- a/src/modules/network.cpp +++ b/src/modules/network.cpp @@ -148,6 +148,7 @@ namespace modules { auto upspeed = network->upspeed(m_udspeed_minwidth, m_udspeed_unit); auto downspeed = network->downspeed(m_udspeed_minwidth, m_udspeed_unit); + auto netspeed = network->netspeed(m_udspeed_minwidth, m_udspeed_unit); // Update label contents const auto replace_tokens = [&](label_t& label) { @@ -158,6 +159,7 @@ namespace modules { label->replace_token("%local_ip6%", network->ip6()); label->replace_token("%upspeed%", upspeed); label->replace_token("%downspeed%", downspeed); + label->replace_token("%netspeed%", netspeed); if (m_wired) { label->replace_token("%linkspeed%", m_wired->linkspeed());