mu/cmd: update for new sexp API

Makes the code a bit cleaner.
This commit is contained in:
Dirk-Jan C. Binnema
2022-11-07 18:36:33 +02:00
parent a417b38624
commit 08ffba42b9
3 changed files with 12 additions and 18 deletions

View File

@ -372,12 +372,10 @@ static bool
output_sexp(const Option<Message>& msg, const OutputInfo& info, const MuConfig* opts, GError** err) output_sexp(const Option<Message>& msg, const OutputInfo& info, const MuConfig* opts, GError** err)
{ {
if (msg) { if (msg) {
if (const auto sexp{msg->sexp()}; !sexp.empty())
if (const auto sexp{msg->cached_sexp()}; !sexp.empty()) fputs(sexp.to_string().c_str(), stdout);
fputs(sexp.c_str(), stdout);
else else
fputs(msg->to_sexp().to_sexp_string().c_str(), stdout); fputs(msg->sexp().to_string().c_str(), stdout);
fputs("\n", stdout); fputs("\n", stdout);
} }
@ -401,7 +399,7 @@ output_json(const Option<Message>& msg, const OutputInfo& info, const MuConfig*
return true; return true;
g_print("%s%s\n", g_print("%s%s\n",
msg->to_sexp().to_json_string().c_str(), msg->sexp().to_json_string().c_str(),
info.last ? "" : ","); info.last ? "" : ",");
return true; return true;

View File

@ -31,7 +31,7 @@
#include "mu-server.hh" #include "mu-server.hh"
#include "utils/mu-utils.hh" #include "utils/mu-utils.hh"
#include "utils/mu-command-parser.hh" #include "utils/mu-command-handler.hh"
#include "utils/mu-readline.hh" #include "utils/mu-readline.hh"
using namespace Mu; using namespace Mu;
@ -82,15 +82,15 @@ cookie(size_t n)
} }
static void static void
output_sexp_stdout(Sexp&& sexp, Server::OutputFlags flags) output_sexp_stdout(const Sexp& sexp, Server::OutputFlags flags)
{ {
/* if requested, insert \n between list elements; note: /* if requested, insert \n between list elements; note:
* is _not_ inherited by children */ * is _not_ inherited by children */
Sexp::Format fopts{};
if (any_of(flags & Server::OutputFlags::SplitList)) if (any_of(flags & Server::OutputFlags::SplitList))
sexp.formatting_opts |= Sexp::FormattingOptions::SplitList; fopts |= Sexp::Format::SplitList;
const auto str{sexp.to_sexp_string()};
const auto str{sexp.to_string(fopts)};
cookie(str.size() + 1); cookie(str.size() + 1);
if (G_UNLIKELY(::puts(str.c_str()) < 0)) { if (G_UNLIKELY(::puts(str.c_str()) < 0)) {
g_critical("failed to write output '%s'", str.c_str()); g_critical("failed to write output '%s'", str.c_str());
@ -104,12 +104,8 @@ output_sexp_stdout(Sexp&& sexp, Server::OutputFlags flags)
static void static void
report_error(const Mu::Error& err) noexcept report_error(const Mu::Error& err) noexcept
{ {
Sexp::List e; output_sexp_stdout(Sexp(":error"_sym, static_cast<size_t>(err.code()),
":message"_sym, err.what()),
e.add_prop(":error", Sexp::make_number(static_cast<size_t>(err.code())));
e.add_prop(":message", Sexp::make_string(err.what()));
output_sexp_stdout(Sexp::make_list(std::move(e)),
Server::OutputFlags::Flush); Server::OutputFlags::Flush);
} }

View File

@ -51,7 +51,7 @@ using namespace Mu;
static Mu::Result<void> static Mu::Result<void>
view_msg_sexp(const Message& message, const MuConfig* opts) view_msg_sexp(const Message& message, const MuConfig* opts)
{ {
::fputs(message.to_sexp().to_sexp_string().c_str(), stdout); ::fputs(message.sexp().to_string().c_str(), stdout);
::fputs("\n", stdout); ::fputs("\n", stdout);
return Ok(); return Ok();