1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00

net: increase speedrate precision (#2054)

more granular units need lower precision, while less granular need
higher precision. assume sane default of:

unit | precision
KB   | 0
MB   | 1
GB   | 2

Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com>
This commit is contained in:
Jan Palus 2020-12-01 17:54:54 +01:00 committed by GitHub
parent 26a9bc5964
commit 9f2459be8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -229,15 +229,17 @@ namespace net {
float time_diff = duration.count();
float speedrate = bytes_diff / time_diff;
vector<string> suffixes{"G", "M"};
vector<pair<string,int>> units{make_pair("G", 2), make_pair("M", 1)};
string suffix{"K"};
int precision = 0;
while ((speedrate /= 1000) > 999) {
suffix = suffixes.back();
suffixes.pop_back();
suffix = units.back().first;
precision = units.back().second;
units.pop_back();
}
return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(0) << std::fixed << speedrate
return sstream() << std::setw(minwidth) << std::setfill(' ') << std::setprecision(precision) << std::fixed << speedrate
<< " " << suffix << unit;
}