From dcf22986808aad801ba193bd58bce40ccfb51d8f Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 25 Jul 2023 20:53:48 +0300 Subject: [PATCH] mu: use fmt-based apis in mu index/server and options iostream is so 1998. --- mu/mu-cmd-index.cc | 37 +++++++++++++++++++++---------------- mu/mu-cmd-server.cc | 6 +++--- mu/mu-options.cc | 28 ++++++++++++++-------------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/mu/mu-cmd-index.cc b/mu/mu-cmd-index.cc index 3a026217..5dd8e3e4 100644 --- a/mu/mu-cmd-index.cc +++ b/mu/mu-cmd-index.cc @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include @@ -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(stats.checked), col.reset(), + col.fg(Color::Green), static_cast(stats.updated), col.reset(), + col.fg(Color::Green), static_cast(stats.removed), col.reset()); } Result 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{}; @@ -108,11 +112,11 @@ 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({}); } } @@ -120,7 +124,8 @@ Mu::mu_cmd_index(Store& store, const Options& opts) if (!opts.quiet) { print_stats(store.indexer().progress(), !opts.nocolor); - std::cout << std::endl; + mu_print("\n"); + ::fflush({}); } return Ok(); diff --git a/mu/mu-cmd-server.cc b/mu/mu-cmd-server.cc index 3d2f8a10..978a3223 100644 --- a/mu/mu-cmd-server.cc +++ b/mu/mu-cmd-server.cc @@ -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) { diff --git a/mu/mu-options.cc b/mu/mu-options.cc index ada68ada..a9f3a05a 100644 --- a/mu/mu-options.cc +++ b/mu/mu-options.cc @@ -661,7 +661,7 @@ static Result 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)); } @@ -718,12 +718,12 @@ License GPLv3+: GNU GPL version 3 or later . 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 @@ -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 * 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}}; @@ -756,12 +756,12 @@ There is NO WARRANTY, to the extent permitted by law. opts.muhome, "Specify alternative mu directory") ->envname("MUHOME") ->type_name(""); - } + } - /* 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. @@ -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 (...) {