Skip to content

Commit

Permalink
migrate some more code to mu_format / join_paths
Browse files Browse the repository at this point in the history
Let's modernize a bit.
  • Loading branch information
djcb committed Jul 20, 2023
1 parent 8c11f6f commit 7b38f09
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
5 changes: 3 additions & 2 deletions lib/message/mu-mime-object.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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
Expand All @@ -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 <mutex>
#include <regex>
#include <fcntl.h>
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion lib/mu-query-threads.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/mu-query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)};

Expand Down
48 changes: 21 additions & 27 deletions lib/mu-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)};
Expand All @@ -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"
<< ";; (<command-name> :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"
";; (<command-name> :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<std::string> names;
Expand All @@ -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(";;");
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion mu/mu-cmd-cfind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mu/mu-cmd-extract.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 7b38f09

Please sign in to comment.