libshared: Change string_for_rate to use KiB/s instead of Kbps.

string_for_size uses KiB, etc., and so when the two are combined (e.g.
pkgman's progress display), it looked especially strange to have two
different units.
This commit is contained in:
Augustin Cavalier 2018-09-02 00:08:57 -04:00
parent f45e4bd1b6
commit e54f86aa6a
2 changed files with 28 additions and 24 deletions

View File

@ -11,8 +11,7 @@
namespace BPrivate {
const char* string_for_rate(double rate, char* string, size_t stringSize,
double base = 1000.0f);
const char* string_for_rate(double rate, char* string, size_t stringSize);
} // namespace BPrivate

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012 Haiku Inc. All rights reserved.
* Copyright 2012-2018, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
@ -7,6 +7,7 @@
#include <stdio.h>
#include <StringFormat.h>
#include <SystemCatalog.h>
using BPrivate::gSystemCatalog;
@ -20,42 +21,46 @@ namespace BPrivate {
const char*
string_for_rate(double rate, char* string, size_t stringSize, double base)
string_for_rate(double rate, char* string, size_t stringSize)
{
double kbps = rate / base;
if (kbps < 10.0) {
const char* trKey = B_TRANSLATE_MARK("%d bps");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), (int)rate);
double kib = rate / 1024.0;
if (kib < 1.0) {
BString tmp;
BStringFormat format(
gSystemCatalog.GetString(B_TRANSLATE_MARK(
"{0, plural, one{# byte/s} other{# bytes/s}}"),
B_TRANSLATION_CONTEXT, "bytes per second"));
format.Format(tmp, (int)rate);
strlcpy(string, tmp.String(), stringSize);
return string;
}
double mbps = kbps / base;
if (mbps < 10.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f Kbps");
double mib = kib / 1024.0;
if (mib < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%3.2f KiB/s");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), kbps);
B_TRANSLATION_CONTEXT, "KiB per second"), kib);
return string;
}
double gbps = mbps / base;
if (gbps < 10.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f Mbps");
double gib = mib / 1024.0;
if (gib < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%3.2f MiB/s");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), mbps);
B_TRANSLATION_CONTEXT, "MiB per second"), mib);
return string;
}
double tbps = gbps / base;
if (tbps < 10.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f Gbps");
double tib = gib / 1024.0;
if (tib < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%3.2f GiB/s");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), gbps);
B_TRANSLATION_CONTEXT, "GiB per second"), gib);
return string;
}
const char* trKey = B_TRANSLATE_MARK("%.0f Tbps");
const char* trKey = B_TRANSLATE_MARK("%.2f TiB/s");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), tbps);
B_TRANSLATION_CONTEXT, "TiB per second"), tib);
return string;
}
} // namespace BPrivate