* mu: support description fields in stat scripts, some refactoring

This commit is contained in:
djcb 2012-10-21 17:20:20 +03:00
parent ba670dd645
commit 12eebbcc23
9 changed files with 77 additions and 51 deletions

View File

@ -1,41 +0,0 @@
#!/bin/sh
exec guile -e main -s $0 $@
!#
;;
;; Copyright (C) 2012 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.
(use-modules (mu) (mu script) (mu stats) (mu plot))
(define (per-year expr text-only)
"Count the total number of messages for each weekday (0-6 for
Sun..Sat) that match EXPR. If TEXT-ONLY is true, use a plain-text
display, otherwise, use a graphical window."
(mu:plot
(sort (mu:tabulate
(lambda (msg)
(+ 1900 (tm:year (localtime (mu:date msg))))) expr)
(lambda (x y) (< (car x) (car y))))
(format #f "Messages per year matching ~a" expr)
"Year" "Messages" text-only))
(define (main args)
(mu:run args per-year))
;; Local Variables:
;; mode: scheme
;; End:

View File

@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;; DESCRIPTION: number of messages per weekday
(use-modules (mu) (mu script) (mu stats) (mu plot))
(define (per-day expr text-only)

View File

@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;; DESCRIPTION: number of messages per hour of the day
(use-modules (mu) (mu script) (mu stats) (mu plot))

View File

@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;; DESCRIPTION: number of messages per month
(use-modules (mu) (mu script) (mu stats) (mu plot))
(define (per-month expr text-only)

View File

@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;; DESCRIPTION: number of messages per year-month
(use-modules (mu) (mu script) (mu stats) (mu plot))
(define (per-year-month expr text-only)

View File

@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
;; along with this program; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;; DESCRIPTION: number of messages per year
(use-modules (mu) (mu script) (mu stats) (mu plot))
(define (per-year expr text-only)

View File

@ -62,6 +62,10 @@ script_info_destroy (MuScriptInfo *msi)
if (!msi)
return;
g_free (msi->_name);
g_free (msi->_path);
g_free (msi->_descr);
g_slice_free (MuScriptInfo, msi);
}
@ -101,10 +105,45 @@ mu_script_info_list_destroy (GSList *lst)
g_slist_free (lst);
}
static gchar*
get_description (const char *path, const char *prefix)
{
FILE *script;
char *line, *descr;
size_t n;
if (!prefix)
return NULL; /* not an error */
script = fopen (path, "r");
if (!script) {
g_warning ("failed to open %s: %s",
path, strerror(errno));
return NULL;
}
descr = NULL;
line = NULL;
while (!descr && getline (&line, &n, script) != -1) {
if (g_str_has_prefix(line, prefix)) {
descr = g_strdup (line + strlen(prefix));
/* remove trailing '\n' */
descr[strlen(descr) - 1] = '\0';
}
free (line);
line = NULL;
}
fclose (script);
return descr;
}
GSList*
mu_script_get_script_info_list (const char *path, const char *ext,
GError **err)
const char *descprefix, GError **err)
{
DIR *dir;
GSList *lst;
@ -133,9 +172,9 @@ mu_script_get_script_info_list (const char *path, const char *ext,
msi->_name = g_strdup (dentry->d_name);
if (ext) /* strip the extension */
msi->_name[strlen(msi->_name) - strlen(ext)] = '\0';
msi->_path = g_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR,
dentry->d_name);
msi->_descr = get_description (msi->_path, descprefix);
lst = g_slist_prepend (lst, msi);
}

View File

@ -63,12 +63,14 @@ const char* mu_script_info_description (MuScriptInfo *msi);
*
* @param path a file system path
* @param ext an extension (e.g., ".scm"), or NULL
* @param prefix for the one-line description
* (e.g., ";; DESCRIPTION: "), or NULL
* @param err receives error information, if any
*
* @return a list of Mu
*/
GSList *mu_script_get_script_info_list (const char *path, const char *ext,
GError **err);
const char *descprefix, GError **err);
/**
* destroy a list of MuScriptInfo* objects

View File

@ -34,12 +34,17 @@
#include "mu-str.h"
#include "mu-script.h"
#define MU_GUILE_EXT ".scm"
#define MU_GUILE_DESCR_PREFIX ";; DESCRIPTION: "
static MuError
list_stats (GError **err)
{
GSList *scripts;
scripts = mu_script_get_script_info_list (MU_STATSDIR, ".scm", err);
scripts = mu_script_get_script_info_list (MU_STATSDIR,
MU_GUILE_EXT,
MU_GUILE_DESCR_PREFIX,
err);
if (err && *err)
return MU_ERROR;
@ -49,10 +54,19 @@ list_stats (GError **err)
GSList *cur;
g_print ("Available statistics "
"(use with --stat=<stastistic>):\n");
for (cur = scripts; cur; cur = g_slist_next (cur))
g_print ("\t%s\n",
mu_script_info_name
((MuScriptInfo*)cur->data));
for (cur = scripts; cur; cur = g_slist_next (cur)) {
MuScriptInfo *msi;
const char* descr;
msi = (MuScriptInfo*)cur->data;
descr = mu_script_info_description (msi);
g_print ("\t%s%s%s\n",
mu_script_info_name (msi),
descr ? ": " : "",
descr ? descr : "");
}
}
mu_script_info_list_destroy (scripts);
@ -92,7 +106,9 @@ mu_cmd_stats (MuConfig *opts, GError **err)
if (!opts->stat)
return list_stats (err);
scripts = mu_script_get_script_info_list (MU_STATSDIR, ".scm",
scripts = mu_script_get_script_info_list (MU_STATSDIR,
MU_GUILE_EXT,
MU_GUILE_DESCR_PREFIX,
err);
if (err && *err)
return MU_ERROR;