mu: add fields/flags commands
Useful information for devising queries. Directly generated from the source. Add manpages, too.
This commit is contained in:
@ -25,6 +25,7 @@ mu = executable(
|
||||
'mu.cc',
|
||||
'mu-cmd-cfind.cc',
|
||||
'mu-cmd-extract.cc',
|
||||
'mu-cmd-fields.cc',
|
||||
'mu-cmd-find.cc',
|
||||
'mu-cmd-index.cc',
|
||||
'mu-cmd-script.cc',
|
||||
|
||||
131
mu/mu-cmd-fields.cc
Normal file
131
mu/mu-cmd-fields.cc
Normal file
@ -0,0 +1,131 @@
|
||||
/*
|
||||
** Copyright (C) 2022 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 Free Software
|
||||
** Foundation; either version 3, or (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
** details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License along with
|
||||
** this program; if not, write to the Free Software Foundation, Inc., 51
|
||||
** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
||||
|
||||
#include "mu-cmd.hh"
|
||||
#include <message/mu-message.hh>
|
||||
#include <iostream>
|
||||
#include "mu-flags.hh"
|
||||
#include "utils/mu-utils.hh"
|
||||
#include "thirdparty/tabulate.hpp"
|
||||
|
||||
using namespace Mu;
|
||||
|
||||
static void
|
||||
show_fields(const MuConfig* opts)
|
||||
{
|
||||
using namespace tabulate;
|
||||
using namespace std::string_literals;
|
||||
|
||||
Table fields;
|
||||
fields.add_row({"field-name", "alias", "short", "search",
|
||||
"value", "example", "description"});
|
||||
|
||||
if (!opts->nocolor) {
|
||||
(*fields.begin()).format()
|
||||
.font_style({FontStyle::bold})
|
||||
.font_color({Color::blue});
|
||||
}
|
||||
|
||||
auto disp= [&](std::string_view sv)->std::string {
|
||||
if (sv.empty())
|
||||
return "";
|
||||
else
|
||||
return format("%*s", STR_V(sv));
|
||||
};
|
||||
|
||||
auto searchable=[&](const Field& field)->std::string {
|
||||
if (field.is_boolean_term())
|
||||
return "boolean";
|
||||
if (field.is_indexable_term())
|
||||
return "index";
|
||||
if (field.is_normal_term())
|
||||
return "yes";
|
||||
if (field.is_contact())
|
||||
return "contact";
|
||||
if (field.is_range())
|
||||
return "range";
|
||||
return "no";
|
||||
};
|
||||
|
||||
size_t row{};
|
||||
field_for_each([&](auto&& field){
|
||||
if (field.is_internal())
|
||||
return; // skip.
|
||||
|
||||
fields.add_row({format("%*s", STR_V(field.name)),
|
||||
field.alias.empty() ? "" : format("%*s", STR_V(field.alias)),
|
||||
field.shortcut ? format("%c", field.shortcut) : ""s,
|
||||
searchable(field),
|
||||
field.is_value() ? "yes" : "no",
|
||||
disp(field.example_query),
|
||||
disp(field.description)});
|
||||
++row;
|
||||
});
|
||||
|
||||
std::cout << fields << '\n';
|
||||
}
|
||||
|
||||
static void
|
||||
show_flags(const MuConfig* opts)
|
||||
{
|
||||
using namespace tabulate;
|
||||
using namespace std::string_literals;
|
||||
|
||||
Table flags;
|
||||
flags.add_row({"flag", "shortcut", "category", "description"});
|
||||
|
||||
if (!opts->nocolor) {
|
||||
(*flags.begin()).format()
|
||||
.font_style({FontStyle::bold})
|
||||
.font_color({Color::green});
|
||||
}
|
||||
|
||||
flag_infos_for_each([&](const MessageFlagInfo& info) {
|
||||
|
||||
flags.add_row({format("%*s", STR_V(info.name)),
|
||||
format("%c", info.shortcut),
|
||||
"<cat>"s,
|
||||
std::string{info.description}});
|
||||
});
|
||||
|
||||
std::cout << flags << '\n';
|
||||
}
|
||||
|
||||
|
||||
|
||||
Result<void>
|
||||
Mu::mu_cmd_fields(const MuConfig* opts)
|
||||
{
|
||||
g_return_val_if_fail(opts, Err(Error::Code::Internal, "no opts"));
|
||||
|
||||
show_fields(opts);
|
||||
|
||||
return Ok();
|
||||
|
||||
}
|
||||
|
||||
Result<void>
|
||||
Mu::mu_cmd_flags(const MuConfig* opts)
|
||||
{
|
||||
g_return_val_if_fail(opts, Err(Error::Code::Internal, "no opts"));
|
||||
|
||||
show_flags(opts);
|
||||
|
||||
return Ok();
|
||||
|
||||
}
|
||||
46
mu/mu-cmd.cc
46
mu/mu-cmd.cc
@ -43,6 +43,8 @@
|
||||
#include "utils/mu-utils.hh"
|
||||
#include "message/mu-message.hh"
|
||||
|
||||
#include <thirdparty/tabulate.hpp>
|
||||
|
||||
#define VIEW_TERMINATOR '\f' /* form-feed */
|
||||
|
||||
using namespace Mu;
|
||||
@ -467,14 +469,18 @@ cmd_verify(const MuConfig* opts)
|
||||
static MuError
|
||||
cmd_info(const Mu::Store& store, const MuConfig* opts, GError** err)
|
||||
{
|
||||
Mu::MaybeAnsi col{!opts->nocolor};
|
||||
using namespace tabulate;
|
||||
|
||||
key_val(col, "maildir", store.properties().root_maildir);
|
||||
key_val(col, "database-path", store.properties().database_path);
|
||||
key_val(col, "schema-version", store.properties().schema_version);
|
||||
key_val(col, "max-message-size", store.properties().max_message_size);
|
||||
key_val(col, "batch-size", store.properties().batch_size);
|
||||
key_val(col, "messages in store", store.size());
|
||||
Table info;
|
||||
|
||||
//Mu::MaybeAnsi col{!opts->nocolor};
|
||||
|
||||
info.add_row({"maildir", store.properties().root_maildir});
|
||||
info.add_row({"database-path", store.properties().database_path});
|
||||
info.add_row({"schema-version", store.properties().schema_version});
|
||||
info.add_row({"max-message-size", format("%zu", store.properties().max_message_size)});
|
||||
info.add_row({"batch-size", format("%zu", store.properties().batch_size)});
|
||||
info.add_row({"messages in store", format("%zu", store.size())});
|
||||
|
||||
const auto created{store.properties().created};
|
||||
const auto tstamp{::localtime(&created)};
|
||||
@ -485,14 +491,27 @@ cmd_info(const Mu::Store& store, const MuConfig* opts, GError** err)
|
||||
strftime(tbuf, sizeof(tbuf), "%c", tstamp);
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
key_val(col, "created", tbuf);
|
||||
info.add_row({"created", tbuf});
|
||||
|
||||
const auto addrs{store.properties().personal_addresses};
|
||||
if (addrs.empty())
|
||||
key_val(col, "personal-address", "<none>");
|
||||
info.add_row({"personal-address", "<none>"});
|
||||
else
|
||||
for (auto&& c : addrs)
|
||||
key_val(col, "personal-address", c);
|
||||
info.add_row({"personal-address", c});
|
||||
|
||||
if (!opts->nocolor) {
|
||||
for (auto&& row: info) {
|
||||
row.cells().at(0)->format().font_style({FontStyle::bold})
|
||||
.font_color({Color::green});
|
||||
row.cells().at(1)->format().font_color({Color::blue});
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << info << std::endl;
|
||||
|
||||
|
||||
|
||||
|
||||
return MU_OK;
|
||||
}
|
||||
@ -621,7 +640,12 @@ Mu::mu_cmd_execute(const MuConfig* opts, GError** err) try {
|
||||
/*
|
||||
* no store needed
|
||||
*/
|
||||
|
||||
case MU_CONFIG_CMD_FIELDS:
|
||||
merr = mu_error_from_result(mu_cmd_fields(opts), err);
|
||||
break;
|
||||
case MU_CONFIG_CMD_FLAGS:
|
||||
merr = mu_error_from_result(mu_cmd_flags(opts), err);
|
||||
break;
|
||||
case MU_CONFIG_CMD_MKDIR: merr = cmd_mkdir(opts, err); break;
|
||||
case MU_CONFIG_CMD_SCRIPT: merr = mu_cmd_script(opts, err); break;
|
||||
case MU_CONFIG_CMD_VIEW:
|
||||
|
||||
20
mu/mu-cmd.hh
20
mu/mu-cmd.hh
@ -45,6 +45,26 @@ Result<void> mu_cmd_find(const Mu::Store& store, const MuConfig* opts);
|
||||
*/
|
||||
Result<void> mu_cmd_extract(const MuConfig* opts);
|
||||
|
||||
|
||||
/**
|
||||
* execute the 'fields' command
|
||||
*
|
||||
* @param opts configuration options
|
||||
*
|
||||
* @return Ok() or some error
|
||||
*/
|
||||
Result<void> mu_cmd_fields(const MuConfig* opts);
|
||||
|
||||
/**
|
||||
* execute the 'flags' command
|
||||
*
|
||||
* @param opts configuration options
|
||||
*
|
||||
* @return Ok() or some error
|
||||
*/
|
||||
Result<void> mu_cmd_flags(const MuConfig* opts);
|
||||
|
||||
|
||||
/**
|
||||
* execute the 'script' command
|
||||
*
|
||||
|
||||
@ -443,7 +443,9 @@ cmd_from_string(const char* str)
|
||||
{"info", MU_CONFIG_CMD_INFO}, {"init", MU_CONFIG_CMD_INIT},
|
||||
{"mkdir", MU_CONFIG_CMD_MKDIR}, {"remove", MU_CONFIG_CMD_REMOVE},
|
||||
{"script", MU_CONFIG_CMD_SCRIPT}, {"server", MU_CONFIG_CMD_SERVER},
|
||||
{"verify", MU_CONFIG_CMD_VERIFY}, {"view", MU_CONFIG_CMD_VIEW}};
|
||||
{"verify", MU_CONFIG_CMD_VERIFY}, {"view", MU_CONFIG_CMD_VIEW},
|
||||
{"fields", MU_CONFIG_CMD_FIELDS}, {"flags", MU_CONFIG_CMD_FLAGS}
|
||||
};
|
||||
|
||||
if (!str)
|
||||
return MU_CONFIG_CMD_UNKNOWN;
|
||||
|
||||
@ -64,7 +64,9 @@ typedef enum {
|
||||
MU_CONFIG_CMD_ADD,
|
||||
MU_CONFIG_CMD_CFIND,
|
||||
MU_CONFIG_CMD_EXTRACT,
|
||||
MU_CONFIG_CMD_FIELDS,
|
||||
MU_CONFIG_CMD_FIND,
|
||||
MU_CONFIG_CMD_FLAGS,
|
||||
MU_CONFIG_CMD_HELP,
|
||||
MU_CONFIG_CMD_INDEX,
|
||||
MU_CONFIG_CMD_INFO,
|
||||
|
||||
Reference in New Issue
Block a user