refactor: remove tr_strip_positional_args() (#2881)

* refactor: use fmt in gtk/SystemIconTray

* refactor: use fmt in gtk/MakeDialog

* refactor: remove unused tr_strip_positional_args()

Co-authored-by: Mike Gelfand <mikedld@mikedld.com>
This commit is contained in:
Charles Kerr
2022-04-07 08:23:04 -05:00
committed by GitHub
parent 8d11f0bc22
commit ffda5bb68a
8 changed files with 72 additions and 146 deletions

View File

@@ -818,48 +818,52 @@ void DetailsDialog::Impl::refreshInfo(std::vector<tr_torrent*> const& torrents)
/* size_lb */
{
uint64_t size = 0;
int pieces = 0;
int32_t pieceSize = 0;
auto const piece_count = std::accumulate(
std::begin(infos),
std::end(infos),
uint64_t{},
[](auto sum, auto const& info) { return sum + info.n_pieces; });
for (auto const& info : infos)
{
size += info.total_size;
pieces += info.n_pieces;
if (pieceSize == 0)
{
pieceSize = info.piece_size;
}
else if (pieceSize != (int)info.piece_size)
{
pieceSize = -1;
}
}
auto const sizebuf = tr_strlsize(size);
if (size == 0)
if (piece_count == 0)
{
str.clear();
}
else if (pieceSize >= 0)
{
str = fmt::format(
ngettext(
"{file_size} ({piece_count} piece @ {piece_size})",
"{file_size} ({piece_count} pieces @ {piece_size})",
pieces),
fmt::arg("file_size", sizebuf),
fmt::arg("piece_count", pieces),
fmt::arg("piece_size", tr_formatter_mem_B(pieceSize)));
}
else
{
auto const total_size = std::accumulate(
std::begin(infos),
std::end(infos),
uint64_t{},
[](auto sum, auto const& info) { return sum + info.total_size; });
auto const file_count = std::accumulate(
std::begin(torrents),
std::end(torrents),
std::size_t{},
[](auto sum, auto const* tor) { return sum + tr_torrentFileCount(tor); });
str = fmt::format(
ngettext("{file_size} ({piece_count} piece)", "{file_size} ({piece_count} pieces)", pieces),
fmt::arg("file_size", sizebuf),
fmt::arg("piece_count", pieces));
ngettext("{total_size} in {file_count:L} file", "{total_size} in {file_count:L} files", file_count),
fmt::arg("total_size", tr_strlsize(total_size)),
fmt::arg("file_count", file_count));
auto const piece_size = std::empty(infos) ? uint32_t{} : infos.front().piece_size;
auto const piece_size_is_uniform = std::all_of(
std::begin(infos),
std::end(infos),
[piece_size](auto const& info) { return info.piece_size == piece_size; });
if (piece_size_is_uniform)
{
str += ' ';
str += fmt::format(
ngettext(
"({piece_count} BitTorrent piece @ {piece_size})",
"({piece_count} BitTorrent pieces @ {piece_size})",
piece_count),
fmt::arg("piece_count", piece_count),
fmt::arg("piece_size", tr_formatter_mem_B(piece_size)));
}
}
size_lb_->set_text(str);
@@ -2669,8 +2673,8 @@ void DetailsDialog::Impl::set_torrents(std::vector<int> const& ids)
else
{
title = fmt::format(
ngettext("Properties - {count} Torrent", "Properties - {count} Torrents", len),
fmt::arg("count", len));
ngettext("Properties - {torrent_count:L} Torrent", "Properties - {torrent_count:L} Torrents", len),
fmt::arg("torrent_count", len));
file_list_->clear();
file_list_->hide();

View File

@@ -3,6 +3,7 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <iostream>
#include <memory>
#include <string>
@@ -337,9 +338,8 @@ void onSourceToggled(Gtk::ToggleButton* tb, Gtk::Widget* widget)
void MakeDialog::Impl::updatePiecesLabel()
{
char const* filename = builder_ != nullptr ? builder_->top : nullptr;
Glib::ustring gstr;
gstr += "<i>";
auto gstr = Glib::ustring{ "<i>" };
if (filename == nullptr)
{
@@ -347,15 +347,18 @@ void MakeDialog::Impl::updatePiecesLabel()
}
else
{
gstr += gtr_sprintf(
ngettext("%1$s; %2$'d File", "%1$s; %2$'d Files", builder_->fileCount),
tr_strlsize(builder_->totalSize),
builder_->fileCount);
gstr += "; ";
gstr += gtr_sprintf(
ngettext("%1$'d Piece @ %2$s", "%1$'d Pieces @ %2$s", builder_->pieceCount),
builder_->pieceCount,
tr_formatter_mem_B(builder_->pieceSize));
gstr += fmt::format(
ngettext("{total_size} in {file_count:L} file", "{total_size} in {file_count:L} files", builder_->fileCount),
fmt::arg("total_size", tr_strlsize(builder_->totalSize)),
fmt::arg("file_count", builder_->fileCount));
gstr += ' ';
gstr += fmt::format(
ngettext(
"({piece_count} BitTorrent piece @ {piece_size})",
"({piece_count} BitTorrent pieces @ {piece_size})",
builder_->pieceCount),
fmt::arg("piece_count", builder_->pieceCount),
fmt::arg("piece_size", tr_formatter_mem_B(builder_->pieceSize)));
}
gstr += "</i>";

View File

@@ -413,7 +413,7 @@ void updateBlocklistText(Gtk::Label* w, Glib::RefPtr<Session> const& core)
{
int const n = tr_blocklistGetRuleCount(core->get_session());
auto const msg = fmt::format(
ngettext("Blocklist has {count} entry", "Blocklist has {count} entries", n),
ngettext("Blocklist has {count:L} entry", "Blocklist has {count:L} entries", n),
fmt::arg("count", n));
w->set_markup(fmt::format(FMT_STRING("<i>{:s}</i>"), msg));
}
@@ -441,7 +441,7 @@ void onBlocklistUpdated(Glib::RefPtr<Session> const& core, int n, blocklist_data
bool const success = n >= 0;
int const count = n >= 0 ? n : tr_blocklistGetRuleCount(core->get_session());
auto const msg = fmt::format(
ngettext("Blocklist has {count} entry", "Blocklist has {count} entries", count),
ngettext("Blocklist has {count:L} entry", "Blocklist has {count:L} entries", count),
fmt::arg("count", count));
data->updateBlocklistButton->set_sensitive(true);
data->updateBlocklistDialog->set_message(

View File

@@ -48,6 +48,8 @@ private:
void activated();
void popup(guint button, guint when);
[[nodiscard]] std::string make_tooltip_text() const;
private:
Glib::RefPtr<Session> const core_;
@@ -87,42 +89,7 @@ void SystemTrayIcon::Impl::popup(guint /*button*/, guint /*when*/)
void SystemTrayIcon::Impl::refresh()
{
double KBps;
double limit;
Glib::ustring upLimit;
Glib::ustring downLimit;
char const* const idle = _("Idle");
auto* session = core_->get_session();
/* up */
KBps = tr_sessionGetRawSpeed_KBps(session, TR_UP);
auto const up = KBps < 0.001 ? idle : tr_formatter_speed_KBps(KBps);
/* up limit */
if (tr_sessionGetActiveSpeedLimit_KBps(session, TR_UP, &limit))
{
upLimit = gtr_sprintf(_(" (Limit: %s)"), tr_formatter_speed_KBps(limit));
}
/* down */
KBps = tr_sessionGetRawSpeed_KBps(session, TR_DOWN);
auto const down = KBps < 0.001 ? idle : tr_formatter_speed_KBps(KBps);
/* down limit */
if (tr_sessionGetActiveSpeedLimit_KBps(session, TR_DOWN, &limit))
{
downLimit = gtr_sprintf(_(" (Limit: %s)"), tr_formatter_speed_KBps(limit));
}
/* %1$s: current upload speed
* %2$s: current upload limit, if any
* %3$s: current download speed
* %4$s: current download limit, if any */
auto const tip = gtr_sprintf(_("Transmission\nUp: %1$s %2$s\nDown: %3$s %4$s"), up, upLimit, down, downLimit);
icon_->set_tooltip_text(tip);
icon_->set_tooltip_text(make_tooltip_text());
}
#endif
@@ -187,3 +154,12 @@ SystemTrayIcon::Impl::Impl(Gtk::Window& main_window, Glib::RefPtr<Session> const
#endif
}
std::string SystemTrayIcon::Impl::make_tooltip_text() const
{
auto const* const session = core_->get_session();
return fmt::format(
_("{upload_speed} ▲ {download_speed} ▼"),
fmt::arg("upload_speed", tr_formatter_speed_KBps(tr_sessionGetRawSpeed_KBps(session, TR_UP))),
fmt::arg("download_speed", tr_formatter_speed_KBps(tr_sessionGetRawSpeed_KBps(session, TR_DOWN))));
}

View File

@@ -94,7 +94,7 @@ Glib::ustring tr_strltime(time_t seconds)
}
auto const days = (int)(seconds / 86400);
auto const d = fmt::format(ngettext("{days} day", "{days} days", days), fmt::arg("days", days));
auto const d = fmt::format(ngettext("{days:L} day", "{days:L} days", days), fmt::arg("days", days));
int const hours = (seconds % 86400) / 3600;
auto const h = fmt::format(ngettext("{hours} hour", "{hours} hours", hours), fmt::arg("hours", hours));
if (days != 0)

View File

@@ -5,7 +5,6 @@
#include <algorithm> // std::sort
#include <array> // std::array
#include <cctype> /* isdigit() */
#include <cerrno>
#include <cfloat> // DBL_DIG
#include <chrono>
@@ -161,47 +160,6 @@ void* tr_memdup(void const* src, size_t byteCount)
****
***/
char const* tr_strip_positional_args(char const* str)
{
static auto buf = std::array<char, 512>{};
char const* in = str;
size_t pos = 0;
for (; (str != nullptr) && (*str != 0) && pos + 1 < std::size(buf); ++str)
{
buf[pos++] = *str;
if (*str == '%' && (isdigit(str[1]) != 0))
{
char const* tmp = str + 1;
while (isdigit(*tmp) != 0)
{
++tmp;
}
if (*tmp == '$')
{
str = tmp[1] == '\'' ? tmp + 1 : tmp;
}
}
if (*str == '%' && str[1] == '\'')
{
str = str + 1;
}
}
buf[pos] = '\0';
return (in != nullptr) && (strcmp(buf.data(), in) == 0) ? in : buf.data();
}
/**
***
**/
void tr_timerAdd(struct event* timer, int seconds, int microseconds)
{
auto tv = timeval{};

View File

@@ -33,8 +33,6 @@ struct tr_error;
* @{
*/
char const* tr_strip_positional_args(char const* fmt);
#if !defined(_)
#if defined(HAVE_GETTEXT) && !defined(__APPLE__)
#include <libintl.h>
@@ -57,8 +55,8 @@ char const* tr_strip_positional_args(char const* fmt);
#ifdef DISABLE_GETTEXT
#undef _
#undef ngettext
#define _(a) tr_strip_positional_args(a)
#define ngettext(singular, plural, count) tr_strip_positional_args((count) == 1 ? (singular) : (plural))
#define _(a) (a)
#define ngettext(singular, plural, count) ((count) == 1 ? (singular) : (plural))
#endif
/****

View File

@@ -33,19 +33,6 @@ using ::libtransmission::test::makeString;
using UtilsTest = ::testing::Test;
using namespace std::literals;
TEST_F(UtilsTest, trStripPositionalArgs)
{
auto const* in = "Hello %1$s foo %2$.*f";
auto const* expected = "Hello %s foo %.*f";
auto const* out = tr_strip_positional_args(in);
EXPECT_STREQ(expected, out);
in = "Hello %1$'d foo %2$'f";
expected = "Hello %d foo %f";
out = tr_strip_positional_args(in);
EXPECT_STREQ(expected, out);
}
TEST_F(UtilsTest, trStrvJoin)
{
EXPECT_EQ(""sv, tr_strvJoin(""sv));