From 790059243b1905e6b66f2701c090cff641d9bd77 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 11 Oct 2016 14:07:59 +0200 Subject: [PATCH] feat(network): Configurable min width for up/down speed --- README.md | 8 +++++++- TODO | 1 - include/adapters/net.hpp | 8 ++++---- include/modules/network.hpp | 15 ++++++++++----- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a9730658..e6eff98f 100644 --- a/README.md +++ b/README.md @@ -873,6 +873,10 @@ See [the bspwm module](#module-internalbspwm) for details on `label-dimmed`. ; - which would test the connection approx. every 10th sec. ; Default: 0 ;ping_interval = 3 + + ; Minimum output width of upload/download rate + ; Default: 3 + ;udspeed_minwidth = 0 ~~~ ##### Extra formatting (example) @@ -897,9 +901,11 @@ See [the bspwm module](#module-internalbspwm) for details on `label-dimmed`. ; %local_ip% [wireless+wired] ; %essid% [wireless] ; %signal% [wireless] + ; %upspeed% [wireless+wired] + ; %downspeed% [wireless+wired] ; %linkspeed% [wired] ; Default: %ifname% %local_ip% - label-connected = %essid% + label-connected = %essid% %downspeed% label-connected-foreground = #eefafafa ; Available tokens: diff --git a/TODO b/TODO index 3a64ff86..5cfedb3d 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,3 @@ -task: sandbox modules task: rewrite README for 2.0 task: rewrite i3 module task: improve test coverage diff --git a/include/adapters/net.hpp b/include/adapters/net.hpp index 5b64f032..7f7a03d8 100644 --- a/include/adapters/net.hpp +++ b/include/adapters/net.hpp @@ -166,7 +166,7 @@ namespace net { return m_linkdata.ip_address; } - string downspeed() { + string downspeed(int minwidth = 3) { if (!query_interface()) throw network_error("Failed to query interface"); @@ -185,12 +185,12 @@ namespace net { speed /= 1000; } - return string_util::from_stream(stringstream() << std::setw(3) << std::setfill(' ') + return string_util::from_stream(stringstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speed << " " << suffixes[suffix_n] << "/s"); } - string upspeed() { + string upspeed(int minwidth = 3) { if (!query_interface()) throw network_error("Failed to query interface"); @@ -209,7 +209,7 @@ namespace net { speed /= 1000; } - return string_util::from_stream(stringstream() << std::setw(3) << std::setfill(' ') + return string_util::from_stream(stringstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speed << " " << suffixes[suffix_n] << "/s"); } diff --git a/include/modules/network.hpp b/include/modules/network.hpp index 542417ab..5d59db5c 100644 --- a/include/modules/network.hpp +++ b/include/modules/network.hpp @@ -27,6 +27,7 @@ namespace modules { m_interface = m_conf.get(name(), "interface"); m_interval = chrono::duration(m_conf.get(name(), "interval", 1)); m_ping_nth_update = m_conf.get(name(), "ping_interval", 0); + m_udspeed_minwidth = m_conf.get(name(), "udspeed_minwidth", m_udspeed_minwidth); // Add formats m_formatter->add( @@ -75,6 +76,7 @@ namespace modules { std::exit(EXIT_FAILURE); } } + void start() { timer_module::start(); @@ -138,8 +140,11 @@ namespace modules { label->replace_token("%signal%", to_string(signal_quality) + "%"); } - label->replace_token("%upspeed%", network->upspeed()); - label->replace_token("%downspeed%", network->downspeed()); + auto upspeed = network->upspeed(m_udspeed_minwidth); + auto downspeed = network->downspeed(m_udspeed_minwidth); + + label->replace_token("%upspeed%", upspeed); + label->replace_token("%downspeed%", downspeed); }; if (m_label_connected) { @@ -220,14 +225,14 @@ namespace modules { label_t m_label_packetloss; label_t m_label_packetloss_tokenized; - string m_interface; - stateflag m_connected{false}; stateflag m_conseq_packetloss{false}; - int m_signal_quality; + string m_interface; + int m_signal_quality; int m_ping_nth_update; int m_counter = -1; // -1 to ignore the first run + int m_udspeed_minwidth = 3; }; }