From 7b38f094c4dd15ccaae4005aa6142d03e71d3ac0 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 20 Jul 2023 18:36:00 +0300 Subject: [PATCH] migrate some more code to mu_format / join_paths Let's modernize a bit. --- lib/message/mu-mime-object.cc | 5 ++-- lib/mu-query-threads.cc | 2 +- lib/mu-query.cc | 4 +-- lib/mu-server.cc | 48 +++++++++++++++-------------------- mu/mu-cmd-cfind.cc | 2 +- mu/mu-cmd-extract.cc | 2 +- 6 files changed, 29 insertions(+), 34 deletions(-) diff --git a/lib/message/mu-mime-object.cc b/lib/message/mu-mime-object.cc index f7cf3736..f6f4bcef 100644 --- a/lib/message/mu-mime-object.cc +++ b/lib/message/mu-mime-object.cc @@ -1,5 +1,5 @@ /* -** Copyright (C) 2022 Dirk-Jan C. Binnema +** Copyright (C) 2022-2023 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -21,6 +21,7 @@ #include "mu-mime-object.hh" #include "gmime/gmime-message.h" #include "utils/mu-utils.hh" +#include "utils/mu-utils-file.hh" #include #include #include @@ -237,7 +238,7 @@ MimeCryptoContext::setup_gpg_test(const std::string& testpath) { /* setup clean environment for testing; inspired by gmime */ - g_setenv ("GNUPGHOME", format("%s/.gnupg", testpath.c_str()).c_str(), 1); + g_setenv ("GNUPGHOME", join_paths(testpath, ".gnupg").c_str(), 1); /* disable environment variables that gpg-agent uses for pinentry */ g_unsetenv ("DBUS_SESSION_BUS_ADDRESS"); diff --git a/lib/mu-query-threads.cc b/lib/mu-query-threads.cc index 3f92d520..4bb49645 100644 --- a/lib/mu-query-threads.cc +++ b/lib/mu-query-threads.cc @@ -359,7 +359,7 @@ to_string(const ThreadPath& tpath, size_t digits) bool first{true}; for (auto&& segm : tpath) { - str += format("%s%0*x", first ? "" : ":", (int)digits, segm); + str += mu_format("{}{:0{}x}", first ? "" : ":", segm, digits); first = false; } diff --git a/lib/mu-query.cc b/lib/mu-query.cc index c4a61848..26a320c0 100644 --- a/lib/mu-query.cc +++ b/lib/mu-query.cc @@ -257,8 +257,8 @@ Query::run(const std::string& expr, Field::Id sortfield_id, g_return_val_if_fail(none_of(qflags & QueryFlags::Leader), Err(Error::Code::InvalidArgument, "cannot pass Leader flag")); - StopWatch sw{format( - "ran query '%s'; related: %s; threads: %s; max-size: %zu", expr.c_str(), + StopWatch sw{mu_format( + "ran query '{}'; related: {}; threads: {}; max-size: {}", expr, any_of(qflags & QueryFlags::IncludeRelated) ? "yes" : "no", any_of(qflags & QueryFlags::Threading) ? "yes" : "no", maxnum)}; diff --git a/lib/mu-server.cc b/lib/mu-server.cc index 0997ec90..c02536e1 100644 --- a/lib/mu-server.cc +++ b/lib/mu-server.cc @@ -41,6 +41,8 @@ #include "mu-store.hh" #include "utils/mu-utils.hh" +#include "utils/mu-utils-file.hh" + #include "utils/mu-option.hh" #include "utils/mu-command-handler.hh" #include "utils/mu-readline.hh" @@ -429,8 +431,7 @@ maybe_add_attachment(Message& message, const MessagePart& part, size_t index) throw cache_path.error(); const auto cooked_name{part.cooked_filename()}; - const auto fname{format("%s/%s", cache_path->c_str(), - cooked_name.value_or("part").c_str())}; + const auto fname{join_paths(*cache_path, cooked_name.value_or("part"))}; const auto res = part.to_file(fname, true); if (!res) @@ -528,7 +529,7 @@ Server::Private::contacts_handler(const Command& cmd) Sexp seq; seq.put_props(":contacts", contacts, - ":tstamp", format("%" G_GINT64_FORMAT, g_get_monotonic_time())); + ":tstamp", mu_format("{}", g_get_monotonic_time())); /* dump the contacts cache as a giant sexp */ mu_debug("sending {} of {} contact(s)", n, store().contacts_cache().size()); @@ -646,8 +647,8 @@ Server::Private::find_handler(const Command& cmd) if (threads) qflags |= QueryFlags::Threading; - StopWatch sw{format("%s (indexing: %s)", __func__, - indexer().is_running() ? "yes" : "no")}; + StopWatch sw{mu_format("{} (indexing: {})", __func__, + indexer().is_running() ? "yes" : "no")}; std::lock_guard l{store_.lock()}; auto qres{store_.run_query(q, sort_field_id, qflags, maxnum)}; @@ -670,11 +671,11 @@ Server::Private::help_handler(const Command& cmd) auto&& info_map{command_handler_.info_map()}; if (command.empty()) { - std::cout << ";; Commands are single-line s-expressions of the form\n" - << ";; ( :param1 val1 :param2 val2 ...)\n" - << ";; For instance:\n;; (help :command mkdir)\n" - << ";; to get detailed information about the 'mkdir' command\n;;\n"; - std::cout << ";; The following commands are available:\n\n"; + mu_println(";; Commands are single-line s-expressions of the form\n" + ";; ( :param1 val1 :param2 val2 ...)\n" + ";; For instance:\n;; (help :command mkdir)\n" + ";; to get more information about the 'mkdir' command\n;;\n" + ";; The following commands are available:"); } std::vector names; @@ -689,24 +690,18 @@ Server::Private::help_handler(const Command& cmd) if (!command.empty() && name != command) continue; - if (!command.empty()) - std::cout << ";; " - << format("%-10s -- %s\n", name.c_str(), info.docstring.c_str()); - else - std::cout << ";; " << name.c_str() << " -- " << info.docstring.c_str() - << '\n'; + mu_println(";; {:<12} -- {}", name, info.docstring); + if (!full) continue; for (auto&& argname : info.sorted_argnames()) { const auto& arg{info.args.find(argname)}; - std::cout << ";; " - << format("%-17s : %-24s ", - arg->first.c_str(), - to_string(arg->second).c_str()); - std::cout << " " << arg->second.docstring << "\n"; + mu_println(";; {:<17} :: {:<24} -- {}", + arg->first, to_string(arg->second), + arg->second.docstring); } - std::cout << ";;\n"; + mu_println(";;"); } } @@ -766,8 +761,8 @@ Server::Private::mkdir_handler(const Command& cmd) * requested */ if (update) output_sexp(Sexp().put_props(":info", "mkdir", - ":message", format("%s has been created", - path.c_str()))); + ":message", + mu_format("{} has been created", path))); } void @@ -914,7 +909,7 @@ Server::Private::queries_handler(const Command& cmd) Sexp qresults; for (auto&& q : queries) { const auto count{store_.count_query(q)}; - const auto unreadq{format("flag:unread AND (%s)", q.c_str())}; + const auto unreadq{mu_format("flag:unread AND ({})", q)}; const auto unread{store_.count_query(unreadq)}; qresults.add(Sexp().put_props(":query", q, ":count", count, @@ -992,8 +987,7 @@ Server::Private::view_mark_as_read(Store::Id docid, Message&& msg, bool rename) void Server::Private::view_handler(const Command& cmd) { - StopWatch sw{format("%s (indexing: %s)", __func__, - indexer().is_running() ? "yes" : "no")}; + StopWatch sw{mu_format("{} (indexing: {})", __func__, indexer().is_running())}; const auto mark_as_read{cmd.boolean_arg(":mark-as-read")}; /* for now, do _not_ rename, as it seems to confuse mbsync */ diff --git a/mu/mu-cmd-cfind.cc b/mu/mu-cmd-cfind.cc index 962d0c7e..86c818b1 100644 --- a/mu/mu-cmd-cfind.cc +++ b/mu/mu-cmd-cfind.cc @@ -95,7 +95,7 @@ guess_nick(const Contact& contact) nicks.emplace(nick, 0); else { ++it->second; - nick = format("%s%zu", nick.c_str(), ++it->second); + nick = mu_format("{}{}", nick, ++it->second); } return nick; diff --git a/mu/mu-cmd-extract.cc b/mu/mu-cmd-extract.cc index 98e85b4a..f33e74ba 100644 --- a/mu/mu-cmd-extract.cc +++ b/mu/mu-cmd-extract.cc @@ -38,7 +38,7 @@ save_part(const Message::Part& part, size_t idx, const Options& opts) * cooking */ const auto path{targetdir + part.cooked_filename(opts.extract.uncooked) - .value_or(format("part-%zu", idx))}; + .value_or(mu_format("part-{}", idx))}; if (auto&& res{part.to_file(path, opts.extract.overwrite)}; !res) return Err(res.error());