From 12eebbcc23617272357ec591b1de241ed1fb9f92 Mon Sep 17 00:00:00 2001 From: djcb Date: Sun, 21 Oct 2012 17:20:20 +0300 Subject: [PATCH] * mu: support description fields in stat scripts, some refactoring --- guile/scripts/stats/msg-per-year.scm | 41 -------------------- guile/scripts/stats/msgs-per-day.scm | 2 + guile/scripts/stats/msgs-per-hour.scm | 2 + guile/scripts/stats/msgs-per-month.scm | 2 + guile/scripts/stats/msgs-per-year-month.scm | 2 + guile/scripts/stats/msgs-per-year.scm | 2 + lib/mu-script.c | 43 ++++++++++++++++++++- lib/mu-script.h | 4 +- mu/mu-cmd-script.c | 30 ++++++++++---- 9 files changed, 77 insertions(+), 51 deletions(-) delete mode 100755 guile/scripts/stats/msg-per-year.scm diff --git a/guile/scripts/stats/msg-per-year.scm b/guile/scripts/stats/msg-per-year.scm deleted file mode 100755 index e9001157..00000000 --- a/guile/scripts/stats/msg-per-year.scm +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/sh -exec guile -e main -s $0 $@ -!# -;; -;; Copyright (C) 2012 Dirk-Jan C. Binnema -;; -;; 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: diff --git a/guile/scripts/stats/msgs-per-day.scm b/guile/scripts/stats/msgs-per-day.scm index da4a9c9a..fcaad6bc 100755 --- a/guile/scripts/stats/msgs-per-day.scm +++ b/guile/scripts/stats/msgs-per-day.scm @@ -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) diff --git a/guile/scripts/stats/msgs-per-hour.scm b/guile/scripts/stats/msgs-per-hour.scm index 325f5d23..928d1910 100755 --- a/guile/scripts/stats/msgs-per-hour.scm +++ b/guile/scripts/stats/msgs-per-hour.scm @@ -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)) diff --git a/guile/scripts/stats/msgs-per-month.scm b/guile/scripts/stats/msgs-per-month.scm index 504ad1f6..a4e7eeab 100755 --- a/guile/scripts/stats/msgs-per-month.scm +++ b/guile/scripts/stats/msgs-per-month.scm @@ -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) diff --git a/guile/scripts/stats/msgs-per-year-month.scm b/guile/scripts/stats/msgs-per-year-month.scm index 796662d5..0a6d2f27 100755 --- a/guile/scripts/stats/msgs-per-year-month.scm +++ b/guile/scripts/stats/msgs-per-year-month.scm @@ -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) diff --git a/guile/scripts/stats/msgs-per-year.scm b/guile/scripts/stats/msgs-per-year.scm index 75bc4787..e175019a 100755 --- a/guile/scripts/stats/msgs-per-year.scm +++ b/guile/scripts/stats/msgs-per-year.scm @@ -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) diff --git a/lib/mu-script.c b/lib/mu-script.c index d22bf256..4bc0df30 100644 --- a/lib/mu-script.c +++ b/lib/mu-script.c @@ -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); } diff --git a/lib/mu-script.h b/lib/mu-script.h index 9cd85a10..a8e877fc 100644 --- a/lib/mu-script.h +++ b/lib/mu-script.h @@ -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 diff --git a/mu/mu-cmd-script.c b/mu/mu-cmd-script.c index 53133d83..35dc1557 100644 --- a/mu/mu-cmd-script.c +++ b/mu/mu-cmd-script.c @@ -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=):\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;