Skip to content

Commit

Permalink
mu: use fmt-based apis in mu index/server and options
Browse files Browse the repository at this point in the history
iostream is so 1998.
  • Loading branch information
djcb committed Jul 25, 2023
1 parent 85a2490 commit dcf2298
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
37 changes: 21 additions & 16 deletions mu/mu-cmd-index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <cstdio>
#include <signal.h>
#include <unistd.h>

Expand Down Expand Up @@ -68,30 +68,34 @@ print_stats(const Indexer::Progress& stats, bool color)
MaybeAnsi col{color};
using Color = MaybeAnsi::Color;

std::cout << col.fg(Color::Yellow) << kars[++i % 4] << col.reset() << " indexing messages; "
<< "checked: " << col.fg(Color::Green) << stats.checked << col.reset()
<< "; updated/new: " << col.fg(Color::Green) << stats.updated << col.reset()
<< "; cleaned-up: " << col.fg(Color::Green) << stats.removed << col.reset();
mu_print("{}{}{} indexing messages; "
"checked: {}{}{}; "
"updated/new: {}{}{}; "
"cleaned-up: {}{}{}",
col.fg(Color::Yellow), kars[++i % 4], col.reset(),
col.fg(Color::Green), static_cast<size_t>(stats.checked), col.reset(),
col.fg(Color::Green), static_cast<size_t>(stats.updated), col.reset(),
col.fg(Color::Green), static_cast<size_t>(stats.removed), col.reset());
}

Result<void>
Mu::mu_cmd_index(Store& store, const Options& opts)
{
const auto mdir{store.root_maildir()};
if (G_UNLIKELY(access(mdir.c_str(), R_OK) != 0))
if (G_UNLIKELY(::access(mdir.c_str(), R_OK) != 0))
return Err(Error::Code::File, "'{}' is not readable: {}",
mdir.c_str(), g_strerror(errno));
mdir, g_strerror(errno));

MaybeAnsi col{!opts.nocolor};
using Color = MaybeAnsi::Color;
if (!opts.quiet) {
if (opts.index.lazycheck)
std::cout << "lazily ";
mu_print("lazily ");

std::cout << "indexing maildir " << col.fg(Color::Green)
<< store.root_maildir() << col.reset() << " -> store "
<< col.fg(Color::Green) << store.path() << col.reset()
<< std::endl;
mu_println("indexing maildir {}{}{} -> "
"store {}{}{}",
col.fg(Color::Green), store.root_maildir(), col.reset(),
col.fg(Color::Blue), store.path(), col.reset());
}

Mu::Indexer::Config conf{};
Expand All @@ -108,19 +112,20 @@ Mu::mu_cmd_index(Store& store, const Options& opts)
if (!opts.quiet)
print_stats(indexer.progress(), !opts.nocolor);

std::this_thread::sleep_for(std::chrono::milliseconds(250));
std::this_thread::sleep_for(std::chrono::milliseconds(100));

if (!opts.quiet) {
std::cout << "\r";
std::cout.flush();
mu_print("\r");
::fflush({});
}
}

store.indexer().stop();

if (!opts.quiet) {
print_stats(store.indexer().progress(), !opts.nocolor);
std::cout << std::endl;
mu_print("\n");
::fflush({});
}

return Ok();
Expand Down
6 changes: 3 additions & 3 deletions mu/mu-cmd-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ Mu::mu_cmd_server(const Mu::Options& opts) try {
setup_readline(histpath, 50);

install_sig_handler();
std::cout << ";; Welcome to the " << PACKAGE_STRING << " command-server"
<< (opts.debug ? " (debug-mode)" : "") << '\n'
<< ";; Use (help) to get a list of commands, (quit) to quit.\n";
mu_println(";; Welcome to the " PACKAGE_STRING " command-server{}\n"
";; Use (help) to get a list of commands, (quit) to quit.",
opts.debug ? " (debug-mode)" : "");

bool do_quit{};
while (!MuTerminate && !do_quit) {
Expand Down
28 changes: 14 additions & 14 deletions mu/mu-options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ static Result<Options>
cmd_help(const CLI::App& app, Options& opts)
{
if (opts.help.command.empty()) {
std::cout << app.help() << "\n";
mu_println("{}", app.help());
return Ok(std::move(opts));
}

Expand Down Expand Up @@ -718,20 +718,20 @@ License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
)");
app.set_version_flag("-V,--version", PACKAGE_VERSION);
app.set_help_flag("-h,--help", "Show help informmation");
app.set_help_all_flag("--help-all");
app.require_subcommand(0, 1);
app.set_version_flag("-V,--version", PACKAGE_VERSION);
app.set_help_flag("-h,--help", "Show help informmation");
app.set_help_all_flag("--help-all");
app.require_subcommand(0, 1);

add_global_options(app, opts);
add_global_options(app, opts);

/*
* subcommands
*
* we keep around a map of the subcommand pointers, so we can
* easily find the chosen one (if any) later.
*/
for (auto&& cmdinfo: SubCommandInfos) {
for (auto&& cmdinfo: SubCommandInfos) {
//const auto cmdtype = cmdinfo.first;
const auto name{std::string{cmdinfo.second.name}};
const auto help{std::string{cmdinfo.second.help}};
Expand All @@ -756,12 +756,12 @@ There is NO WARRANTY, to the extent permitted by law.
opts.muhome, "Specify alternative mu directory")
->envname("MUHOME")
->type_name("<dir>");
}
}

/* add scripts (if supported) as semi-subscommands as well */
const auto scripts = add_scripts(app, opts);
/* add scripts (if supported) as semi-subscommands as well */
const auto scripts = add_scripts(app, opts);

try {
try {
app.parse(argc, argv);

// find the chosen sub command, if any.
Expand Down Expand Up @@ -789,11 +789,11 @@ There is NO WARRANTY, to the extent permitted by law.
return cmd_help(app, opts);

} catch (const CLI::CallForHelp& cfh) {
std::cout << app.help() << std::flush;
mu_println("{}", app.help());
} catch (const CLI::CallForAllHelp& cfah) {
std::cout << app.help("", CLI::AppFormatMode::All) << std::flush;
mu_println("{}", app.help("", CLI::AppFormatMode::All));
} catch (const CLI::CallForVersion&) {
std::cout << "version " << PACKAGE_VERSION << "\n";
mu_println("version {}", PACKAGE_VERSION);
} catch (const CLI::ParseError& pe) {
return Err(Error::Code::InvalidArgument, "{}", pe.what());
} catch (...) {
Expand Down

0 comments on commit dcf2298

Please sign in to comment.