mu: use fmt-based apis in mu index/server and options

iostream is so 1998.
This commit is contained in:
Dirk-Jan C. Binnema
2023-07-25 20:53:48 +03:00
parent 85a2490300
commit dcf2298680
3 changed files with 38 additions and 33 deletions

View File

@ -26,7 +26,7 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <cstdio>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
@ -68,30 +68,34 @@ print_stats(const Indexer::Progress& stats, bool color)
MaybeAnsi col{color}; MaybeAnsi col{color};
using Color = MaybeAnsi::Color; using Color = MaybeAnsi::Color;
std::cout << col.fg(Color::Yellow) << kars[++i % 4] << col.reset() << " indexing messages; " mu_print("{}{}{} indexing messages; "
<< "checked: " << col.fg(Color::Green) << stats.checked << col.reset() "checked: {}{}{}; "
<< "; updated/new: " << col.fg(Color::Green) << stats.updated << col.reset() "updated/new: {}{}{}; "
<< "; cleaned-up: " << col.fg(Color::Green) << stats.removed << col.reset(); "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> Result<void>
Mu::mu_cmd_index(Store& store, const Options& opts) Mu::mu_cmd_index(Store& store, const Options& opts)
{ {
const auto mdir{store.root_maildir()}; 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: {}", return Err(Error::Code::File, "'{}' is not readable: {}",
mdir.c_str(), g_strerror(errno)); mdir, g_strerror(errno));
MaybeAnsi col{!opts.nocolor}; MaybeAnsi col{!opts.nocolor};
using Color = MaybeAnsi::Color; using Color = MaybeAnsi::Color;
if (!opts.quiet) { if (!opts.quiet) {
if (opts.index.lazycheck) if (opts.index.lazycheck)
std::cout << "lazily "; mu_print("lazily ");
std::cout << "indexing maildir " << col.fg(Color::Green) mu_println("indexing maildir {}{}{} -> "
<< store.root_maildir() << col.reset() << " -> store " "store {}{}{}",
<< col.fg(Color::Green) << store.path() << col.reset() col.fg(Color::Green), store.root_maildir(), col.reset(),
<< std::endl; col.fg(Color::Blue), store.path(), col.reset());
} }
Mu::Indexer::Config conf{}; Mu::Indexer::Config conf{};
@ -108,11 +112,11 @@ Mu::mu_cmd_index(Store& store, const Options& opts)
if (!opts.quiet) if (!opts.quiet)
print_stats(indexer.progress(), !opts.nocolor); 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) { if (!opts.quiet) {
std::cout << "\r"; mu_print("\r");
std::cout.flush(); ::fflush({});
} }
} }
@ -120,7 +124,8 @@ Mu::mu_cmd_index(Store& store, const Options& opts)
if (!opts.quiet) { if (!opts.quiet) {
print_stats(store.indexer().progress(), !opts.nocolor); print_stats(store.indexer().progress(), !opts.nocolor);
std::cout << std::endl; mu_print("\n");
::fflush({});
} }
return Ok(); return Ok();

View File

@ -136,9 +136,9 @@ Mu::mu_cmd_server(const Mu::Options& opts) try {
setup_readline(histpath, 50); setup_readline(histpath, 50);
install_sig_handler(); install_sig_handler();
std::cout << ";; Welcome to the " << PACKAGE_STRING << " command-server" mu_println(";; Welcome to the " PACKAGE_STRING " command-server{}\n"
<< (opts.debug ? " (debug-mode)" : "") << '\n' ";; Use (help) to get a list of commands, (quit) to quit.",
<< ";; Use (help) to get a list of commands, (quit) to quit.\n"; opts.debug ? " (debug-mode)" : "");
bool do_quit{}; bool do_quit{};
while (!MuTerminate && !do_quit) { while (!MuTerminate && !do_quit) {

View File

@ -661,7 +661,7 @@ static Result<Options>
cmd_help(const CLI::App& app, Options& opts) cmd_help(const CLI::App& app, Options& opts)
{ {
if (opts.help.command.empty()) { if (opts.help.command.empty()) {
std::cout << app.help() << "\n"; mu_println("{}", app.help());
return Ok(std::move(opts)); return Ok(std::move(opts));
} }
@ -718,12 +718,12 @@ 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. This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. There is NO WARRANTY, to the extent permitted by law.
)"); )");
app.set_version_flag("-V,--version", PACKAGE_VERSION); app.set_version_flag("-V,--version", PACKAGE_VERSION);
app.set_help_flag("-h,--help", "Show help informmation"); app.set_help_flag("-h,--help", "Show help informmation");
app.set_help_all_flag("--help-all"); app.set_help_all_flag("--help-all");
app.require_subcommand(0, 1); app.require_subcommand(0, 1);
add_global_options(app, opts); add_global_options(app, opts);
/* /*
* subcommands * subcommands
@ -731,7 +731,7 @@ There is NO WARRANTY, to the extent permitted by law.
* we keep around a map of the subcommand pointers, so we can * we keep around a map of the subcommand pointers, so we can
* easily find the chosen one (if any) later. * easily find the chosen one (if any) later.
*/ */
for (auto&& cmdinfo: SubCommandInfos) { for (auto&& cmdinfo: SubCommandInfos) {
//const auto cmdtype = cmdinfo.first; //const auto cmdtype = cmdinfo.first;
const auto name{std::string{cmdinfo.second.name}}; const auto name{std::string{cmdinfo.second.name}};
const auto help{std::string{cmdinfo.second.help}}; const auto help{std::string{cmdinfo.second.help}};
@ -756,12 +756,12 @@ There is NO WARRANTY, to the extent permitted by law.
opts.muhome, "Specify alternative mu directory") opts.muhome, "Specify alternative mu directory")
->envname("MUHOME") ->envname("MUHOME")
->type_name("<dir>"); ->type_name("<dir>");
} }
/* add scripts (if supported) as semi-subscommands as well */ /* add scripts (if supported) as semi-subscommands as well */
const auto scripts = add_scripts(app, opts); const auto scripts = add_scripts(app, opts);
try { try {
app.parse(argc, argv); app.parse(argc, argv);
// find the chosen sub command, if any. // find the chosen sub command, if any.
@ -789,11 +789,11 @@ There is NO WARRANTY, to the extent permitted by law.
return cmd_help(app, opts); return cmd_help(app, opts);
} catch (const CLI::CallForHelp& cfh) { } catch (const CLI::CallForHelp& cfh) {
std::cout << app.help() << std::flush; mu_println("{}", app.help());
} catch (const CLI::CallForAllHelp& cfah) { } 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&) { } catch (const CLI::CallForVersion&) {
std::cout << "version " << PACKAGE_VERSION << "\n"; mu_println("version {}", PACKAGE_VERSION);
} catch (const CLI::ParseError& pe) { } catch (const CLI::ParseError& pe) {
return Err(Error::Code::InvalidArgument, "{}", pe.what()); return Err(Error::Code::InvalidArgument, "{}", pe.what());
} catch (...) { } catch (...) {