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:10:30 -04:00
parent f45e4bd1b6
commit e54f86aa6a
2 changed files with 28 additions and 24 deletions
+1 -2
View File
@@ -11,8 +11,7 @@
namespace BPrivate { namespace BPrivate {
const char* string_for_rate(double rate, char* string, size_t stringSize, const char* string_for_rate(double rate, char* string, size_t stringSize);
double base = 1000.0f);
} // namespace BPrivate } // namespace BPrivate
+27 -22
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. * Distributed under the terms of the MIT License.
*/ */
@@ -7,6 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <StringFormat.h>
#include <SystemCatalog.h> #include <SystemCatalog.h>
using BPrivate::gSystemCatalog; using BPrivate::gSystemCatalog;
@@ -20,42 +21,46 @@ namespace BPrivate {
const char* 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; double kib = rate / 1024.0;
if (kbps < 10.0) { if (kib < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%d bps"); BString tmp;
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, BStringFormat format(
B_TRANSLATION_CONTEXT), (int)rate); 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; return string;
} }
double mbps = kbps / base; double mib = kib / 1024.0;
if (mbps < 10.0) { if (mib < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f Kbps"); const char* trKey = B_TRANSLATE_MARK("%3.2f KiB/s");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), kbps); B_TRANSLATION_CONTEXT, "KiB per second"), kib);
return string; return string;
} }
double gbps = mbps / base; double gib = mib / 1024.0;
if (gbps < 10.0) { if (gib < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f Mbps"); const char* trKey = B_TRANSLATE_MARK("%3.2f MiB/s");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), mbps); B_TRANSLATION_CONTEXT, "MiB per second"), mib);
return string; return string;
} }
double tbps = gbps / base; double tib = gib / 1024.0;
if (tbps < 10.0) { if (tib < 1.0) {
const char* trKey = B_TRANSLATE_MARK("%.0f Gbps"); const char* trKey = B_TRANSLATE_MARK("%3.2f GiB/s");
snprintf(string, stringSize, gSystemCatalog.GetString(trKey, snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), gbps); B_TRANSLATION_CONTEXT, "GiB per second"), gib);
return string; 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, snprintf(string, stringSize, gSystemCatalog.GetString(trKey,
B_TRANSLATION_CONTEXT), tbps); B_TRANSLATION_CONTEXT, "TiB per second"), tib);
return string; return string;
} }
} // namespace BPrivate } // namespace BPrivate