* WIP: basic 'mu cfind' command + start of man page

This commit is contained in:
Dirk-Jan C. Binnema
2011-03-05 00:59:35 +02:00
parent 6226754a25
commit 86971d53d8
5 changed files with 195 additions and 17 deletions

View File

@ -131,10 +131,62 @@ mu_cmd_mkdir (MuConfig *opts)
}
static void
each_contact (const char *email, const char *name, time_t tstamp, gpointer data)
enum _OutputFormat {
FORMAT_PLAIN,
FORMAT_MUTT,
FORMAT_WL,
FORMAT_CSV,
FORMAT_ORG_CONTACT,
FORMAT_NONE
};
typedef enum _OutputFormat OutputFormat;
static OutputFormat
get_output_format (const char *formatstr)
{
g_print ("%u: %s %s\n", (unsigned)tstamp, email, name ? name : "");
int i;
struct {
const char* name;
OutputFormat format;
} formats [] = {
{MU_CONFIG_FORMAT_PLAIN, FORMAT_PLAIN},
{MU_CONFIG_FORMAT_MUTT, FORMAT_MUTT},
{MU_CONFIG_FORMAT_WL, FORMAT_WL},
{MU_CONFIG_FORMAT_CSV, FORMAT_CSV},
{MU_CONFIG_FORMAT_ORG_CONTACT, FORMAT_ORG_CONTACT}
};
for (i = 0; i != G_N_ELEMENTS(formats); i++)
if (g_strcmp0 (formats[i].name, formatstr) == 0)
return formats[i].format;
return FORMAT_NONE;
}
static void
each_contact (const char *email, const char *name, time_t tstamp,
OutputFormat format)
{
switch (format) {
case FORMAT_MUTT:
if (name)
g_print ("alias %s <%s>\n", name, email);
break;
case FORMAT_WL:
if (name)
g_print ("%s \"%s\"\n", email, name);
break;
case FORMAT_ORG_CONTACT:
if (name)
g_print ("* %s\n:PROPERTIES:\n:EMAIL: %s\n:END:\n\n",
name, email);
break;
default:
g_print ("%u: %s %s\n", (unsigned)tstamp, email, name ? name : "");
}
}
@ -143,14 +195,23 @@ each_contact (const char *email, const char *name, time_t tstamp, gpointer data)
MuExitCode
mu_cmd_cfind (MuConfig *opts)
{
OutputFormat format;
MuContacts *contacts;
g_return_val_if_fail (opts, MU_EXITCODE_ERROR);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_CFIND,
MU_EXITCODE_ERROR);
format = get_output_format (opts->formatstr);
if (format == FORMAT_NONE) {
g_warning ("invalid output format %s",
opts->formatstr ? opts->formatstr : "<none>");
return FALSE;
}
/* if (!opts->params[1]) { */
/* g_warning ("usage: mu cfind [ptrn] "); */
/* g_warning ("usage: mu cfind [OPTIONS] [<ptrn>]"); */
/* return MU_EXITCODE_ERROR; */
/* } */
@ -160,8 +221,8 @@ mu_cmd_cfind (MuConfig *opts)
return MU_EXITCODE_ERROR;
}
mu_contacts_foreach (contacts, (MuContactsForeachFunc)each_contact, NULL,
opts->params[1]);
mu_contacts_foreach (contacts, (MuContactsForeachFunc)each_contact,
GINT_TO_POINTER(format), opts->params[1]);
mu_contacts_destroy (contacts);
return MU_OK;

View File

@ -162,7 +162,7 @@ config_options_group_find (MuConfig *opts)
"clear old links before filling a linksdir (false)", NULL},
{"format", 'o', 0, G_OPTION_ARG_STRING, &opts->formatstr,
"output format ('plain'(*), 'links', 'xml',"
"'json', 'sexp', 'xquery') (plain)", NULL},
"'json', 'sexp', 'xquery')", NULL},
{"xquery", 0, 0, G_OPTION_ARG_NONE, &opts->xquery,
"obsolete, use --format=xquery instead", NULL},
@ -197,6 +197,35 @@ config_options_group_mkdir (MuConfig *opts)
return og;
}
static void
set_group_cfind_defaults (MuConfig *opts)
{
if (!opts->formatstr) /* by default, use plain output */
opts->formatstr = MU_CONFIG_FORMAT_PLAIN;
}
static GOptionGroup *
config_options_group_cfind (MuConfig *opts)
{
GOptionGroup *og;
GOptionEntry entries[] = {
{"format", 'o', 0, G_OPTION_ARG_STRING, &opts->formatstr,
"output format ('plain'(*), 'mutt', 'wanderlust',"
"'org-contact', 'csv')", NULL},
{NULL, 0, 0, 0, NULL, NULL, NULL}
};
og = g_option_group_new("cfind", "options for the 'cfind' command",
"", NULL, NULL);
g_option_group_add_entries(og, entries);
return og;
}
static GOptionGroup*
config_options_group_extract(MuConfig *opts)
{
@ -281,7 +310,6 @@ parse_params (MuConfig *opts, int *argcp, char ***argvp)
context = g_option_context_new("- mu general option");
g_option_context_set_main_group(context,
config_options_group_mu(opts));
switch (opts->cmd) {
case MU_CONFIG_CMD_INDEX:
g_option_context_add_group(context, config_options_group_index(opts));
@ -295,6 +323,9 @@ parse_params (MuConfig *opts, int *argcp, char ***argvp)
case MU_CONFIG_CMD_EXTRACT:
g_option_context_add_group(context, config_options_group_extract(opts));
break;
case MU_CONFIG_CMD_CFIND:
g_option_context_add_group(context, config_options_group_cfind(opts));
break;
default: break;
}
@ -324,9 +355,10 @@ mu_config_new (int *argcp, char ***argvp)
}
/* fill in the defaults if user did not specify */
set_group_mu_defaults(config);
set_group_index_defaults(config);
set_group_find_defaults(config);
set_group_mu_defaults (config);
set_group_index_defaults (config);
set_group_find_defaults (config);
set_group_cfind_defaults (config);
/* set_group_mkdir_defaults (config); */
return config;
@ -338,11 +370,11 @@ mu_config_destroy (MuConfig *opts)
if (!opts)
return;
g_free(opts->muhome);
g_free(opts->maildir);
g_free(opts->linksdir);
g_free(opts->targetdir);
g_strfreev(opts->params);
g_free (opts->muhome);
g_free (opts->maildir);
g_free (opts->linksdir);
g_free (opts->targetdir);
g_strfreev (opts->params);
g_free (opts);
}

View File

@ -34,6 +34,16 @@ G_BEGIN_DECLS
#define MU_CONFIG_FORMAT_SEXP "sexp" /* output sexps */
#define MU_CONFIG_FORMAT_XQUERY "xquery" /* output the xapian query */
/* for cfind */
#define MU_CONFIG_FORMAT_MUTT "mutt" /* output in mutt alias style */
#define MU_CONFIG_FORMAT_WL "wl" /* output in Wanderlust
* address-book style */
#define MU_CONFIG_FORMAT_CSV "csv" /* output in
* comma-separated
* values */
#define MU_CONFIG_FORMAT_ORG_CONTACT "org-contact" /* org-contact
* format */
enum _MuConfigCmd {
MU_CONFIG_CMD_INDEX,
MU_CONFIG_CMD_FIND,
@ -78,7 +88,7 @@ struct _MuConfig {
* default */
int max_msg_size; /* maximum size for message files */
/* options for querying */
/* options for querying */
gboolean xquery; /* (obsolete) give the Xapian
query instead of search
results */