* mu: support description fields in stat scripts, some refactoring
This commit is contained in:
@ -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:
|
|
||||||
@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
|
|||||||
;; along with this program; if not, write to the Free Software Foundation,
|
;; along with this program; if not, write to the Free Software Foundation,
|
||||||
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
;; 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))
|
(use-modules (mu) (mu script) (mu stats) (mu plot))
|
||||||
|
|
||||||
(define (per-day expr text-only)
|
(define (per-day expr text-only)
|
||||||
|
|||||||
@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
|
|||||||
;; along with this program; if not, write to the Free Software Foundation,
|
;; along with this program; if not, write to the Free Software Foundation,
|
||||||
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
;; 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))
|
(use-modules (mu) (mu script) (mu stats) (mu plot))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
|
|||||||
;; along with this program; if not, write to the Free Software Foundation,
|
;; along with this program; if not, write to the Free Software Foundation,
|
||||||
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
;; 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))
|
(use-modules (mu) (mu script) (mu stats) (mu plot))
|
||||||
|
|
||||||
(define (per-month expr text-only)
|
(define (per-month expr text-only)
|
||||||
|
|||||||
@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
|
|||||||
;; along with this program; if not, write to the Free Software Foundation,
|
;; along with this program; if not, write to the Free Software Foundation,
|
||||||
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
;; 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))
|
(use-modules (mu) (mu script) (mu stats) (mu plot))
|
||||||
|
|
||||||
(define (per-year-month expr text-only)
|
(define (per-year-month expr text-only)
|
||||||
|
|||||||
@ -19,6 +19,8 @@ exec guile -e main -s $0 $@
|
|||||||
;; along with this program; if not, write to the Free Software Foundation,
|
;; along with this program; if not, write to the Free Software Foundation,
|
||||||
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
;; 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))
|
(use-modules (mu) (mu script) (mu stats) (mu plot))
|
||||||
|
|
||||||
(define (per-year expr text-only)
|
(define (per-year expr text-only)
|
||||||
|
|||||||
@ -62,6 +62,10 @@ script_info_destroy (MuScriptInfo *msi)
|
|||||||
if (!msi)
|
if (!msi)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
g_free (msi->_name);
|
||||||
|
g_free (msi->_path);
|
||||||
|
g_free (msi->_descr);
|
||||||
|
|
||||||
g_slice_free (MuScriptInfo, msi);
|
g_slice_free (MuScriptInfo, msi);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,10 +105,45 @@ mu_script_info_list_destroy (GSList *lst)
|
|||||||
g_slist_free (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*
|
GSList*
|
||||||
mu_script_get_script_info_list (const char *path, const char *ext,
|
mu_script_get_script_info_list (const char *path, const char *ext,
|
||||||
GError **err)
|
const char *descprefix, GError **err)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
GSList *lst;
|
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);
|
msi->_name = g_strdup (dentry->d_name);
|
||||||
if (ext) /* strip the extension */
|
if (ext) /* strip the extension */
|
||||||
msi->_name[strlen(msi->_name) - strlen(ext)] = '\0';
|
msi->_name[strlen(msi->_name) - strlen(ext)] = '\0';
|
||||||
|
|
||||||
msi->_path = g_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR,
|
msi->_path = g_strdup_printf ("%s%c%s", path, G_DIR_SEPARATOR,
|
||||||
dentry->d_name);
|
dentry->d_name);
|
||||||
|
msi->_descr = get_description (msi->_path, descprefix);
|
||||||
lst = g_slist_prepend (lst, msi);
|
lst = g_slist_prepend (lst, msi);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -63,12 +63,14 @@ const char* mu_script_info_description (MuScriptInfo *msi);
|
|||||||
*
|
*
|
||||||
* @param path a file system path
|
* @param path a file system path
|
||||||
* @param ext an extension (e.g., ".scm"), or NULL
|
* @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
|
* @param err receives error information, if any
|
||||||
*
|
*
|
||||||
* @return a list of Mu
|
* @return a list of Mu
|
||||||
*/
|
*/
|
||||||
GSList *mu_script_get_script_info_list (const char *path, const char *ext,
|
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
|
* destroy a list of MuScriptInfo* objects
|
||||||
|
|||||||
@ -34,12 +34,17 @@
|
|||||||
#include "mu-str.h"
|
#include "mu-str.h"
|
||||||
#include "mu-script.h"
|
#include "mu-script.h"
|
||||||
|
|
||||||
|
#define MU_GUILE_EXT ".scm"
|
||||||
|
#define MU_GUILE_DESCR_PREFIX ";; DESCRIPTION: "
|
||||||
|
|
||||||
static MuError
|
static MuError
|
||||||
list_stats (GError **err)
|
list_stats (GError **err)
|
||||||
{
|
{
|
||||||
GSList *scripts;
|
GSList *scripts;
|
||||||
|
scripts = mu_script_get_script_info_list (MU_STATSDIR,
|
||||||
scripts = mu_script_get_script_info_list (MU_STATSDIR, ".scm", err);
|
MU_GUILE_EXT,
|
||||||
|
MU_GUILE_DESCR_PREFIX,
|
||||||
|
err);
|
||||||
if (err && *err)
|
if (err && *err)
|
||||||
return MU_ERROR;
|
return MU_ERROR;
|
||||||
|
|
||||||
@ -49,10 +54,19 @@ list_stats (GError **err)
|
|||||||
GSList *cur;
|
GSList *cur;
|
||||||
g_print ("Available statistics "
|
g_print ("Available statistics "
|
||||||
"(use with --stat=<stastistic>):\n");
|
"(use with --stat=<stastistic>):\n");
|
||||||
for (cur = scripts; cur; cur = g_slist_next (cur))
|
for (cur = scripts; cur; cur = g_slist_next (cur)) {
|
||||||
g_print ("\t%s\n",
|
|
||||||
mu_script_info_name
|
MuScriptInfo *msi;
|
||||||
((MuScriptInfo*)cur->data));
|
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);
|
mu_script_info_list_destroy (scripts);
|
||||||
@ -92,7 +106,9 @@ mu_cmd_stats (MuConfig *opts, GError **err)
|
|||||||
if (!opts->stat)
|
if (!opts->stat)
|
||||||
return list_stats (err);
|
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);
|
err);
|
||||||
if (err && *err)
|
if (err && *err)
|
||||||
return MU_ERROR;
|
return MU_ERROR;
|
||||||
|
|||||||
Reference in New Issue
Block a user