[Xfce4-commits] <xfce-utils:completion> Add terminal option handling to XfrunHistory.

Jérôme Guelfucci noreply at xfce.org
Wed Nov 3 21:04:05 CET 2010


Updating branch refs/heads/completion
         to 925dc51c71dfcbd1989a1e233eb4c611e7225758 (commit)
       from e7e41b21efe1afaff04431989e4c805074b69d45 (commit)

commit 925dc51c71dfcbd1989a1e233eb4c611e7225758
Author: Jérôme Guelfucci <jeromeg at xfce.org>
Date:   Sun Oct 31 09:38:19 2010 +0100

    Add terminal option handling to XfrunHistory.

 xfrun/xfrun-history.c |  106 ++++++++++++++++++++++++++++++++++++-------------
 xfrun/xfrun-history.h |   31 +++++++++-----
 2 files changed, 98 insertions(+), 39 deletions(-)

diff --git a/xfrun/xfrun-history.c b/xfrun/xfrun-history.c
index 2b779c3..1bafdd7 100644
--- a/xfrun/xfrun-history.c
+++ b/xfrun/xfrun-history.c
@@ -38,12 +38,13 @@ struct _XfrunHistoryPrivate
 
 G_DEFINE_TYPE (XfrunHistory, xfrun_history, G_TYPE_OBJECT);
 
-static void          xfrun_history_finalize              (GObject *gobject);
-static void          xfrun_history_free_list             (gpointer item,
-                                                          gpointer user_data);
+static void          xfrun_history_finalize              (GObject  *gobject);
+static void          xfrun_history_free_list             (gpointer  item,
+                                                          gpointer  user_data);
 static gchar       **xfrun_history_get_file_content      (void);
 static GList        *xfrun_history_initialize_history    (void);
-static GCompletion  *xfrun_history_initialize_completion (GList   *history);
+static gchar        *xfrun_history_completion_func       (gpointer  item);
+static GCompletion  *xfrun_history_initialize_completion (GList    *history);
 
 
 
@@ -100,7 +101,8 @@ xfrun_history_finalize (GObject *gobject)
 static void
 xfrun_history_free_list (gpointer item, gpointer user_data)
 {
-  g_free (item);
+  g_free (((XfrunHistoryItem *)item)->command);
+  g_slice_free (XfrunHistoryItem, item);
 }
 
 static gchar
@@ -126,6 +128,11 @@ static gchar
   return lines;
 }
 
+/**
+ * History lines are:
+ * 1:command for in_terminal == TRUE
+ * 0:command for in_terminal == FALSE
+ **/
 static GList
 *xfrun_history_initialize_history (void)
 {
@@ -143,16 +150,25 @@ static GList
   /* Add the lines to the history list */
   for (i = 0; history_contents[i] != NULL; i++)
     {
-      GString *string;
+      XfrunHistoryItem *item;
+
+      /* Don't add empty lines or empty commands */
+      if (strlen (history_contents[i]) <= 2)
+        continue;
 
-      /* Remove leading and trailing white spaces */
-      string = g_string_new (g_strstrip (history_contents[i]));
+      /* Only accept UTF-8 */
+      if (!g_utf8_validate (history_contents[i], -1, NULL))
+        continue;
 
-      /* Only add non-empty lines */
-      if (G_LIKELY (string->len > 0))
-        list = g_list_append (list, history_contents[i]);
+      item = g_slice_new (XfrunHistoryItem);
 
-      g_string_free (string, FALSE);
+      /* 1 stands for TRUE, 0 for FALSE. Consider anything
+       * else as FALSE */
+      item->in_terminal = (history_contents[i][0] == '1');
+
+      item->command = g_strdup (history_contents[i] + 2);
+
+      list = g_list_append (list, item);
     }
 
   g_strfreev (history_contents);
@@ -160,6 +176,12 @@ static GList
   return list;
 }
 
+static gchar
+*xfrun_history_completion_func (gpointer item)
+{
+  return ((XfrunHistoryItem *)item)->command;
+}
+
 static GCompletion
 *xfrun_history_initialize_completion (GList *history)
 {
@@ -167,7 +189,7 @@ static GCompletion
 
   /* Create the completion and add the whole history list as
    * completion items */
-  completion = g_completion_new (NULL);
+  completion = g_completion_new (xfrun_history_completion_func);
   g_completion_add_items (completion, history);
 
   return completion;
@@ -184,9 +206,9 @@ static GCompletion
  *
  * When called for the first time, returns the first element of the list.
  *
- * Return value: a string that should not be modified/freed or %NULL.
+ * Return value: a #XfrunHistoryItem that should not be modified/freed or %NULL.
  **/
-gchar
+XfrunHistoryItem
 *xfrun_history_get_next (XfrunHistory *history)
 {
   g_return_val_if_fail (XFRUN_IS_HISTORY (history), NULL);
@@ -219,9 +241,9 @@ gchar
  * Returns the previous item in history, the same item if we are at the
  * beginning of the history list or %NULL if the history is empty.
  *
- * Return value: a string that should not be modified/freed or %NULL.
+ * Return value: a #XfrunHistoryItem that should not be modified/freed or %NULL.
  **/
-gchar
+XfrunHistoryItem
 *xfrun_history_get_previous (XfrunHistory *history)
 {
   g_return_val_if_fail (XFRUN_IS_HISTORY (history), NULL);
@@ -239,9 +261,12 @@ gchar
       previous = g_list_previous (history->priv->current_history);
       if (previous)
         history->priv->current_history = previous;
+
+      return history->priv->current_history->data;
     }
 
-  return history->priv->current_history->data;
+  /* No history cycling was started */
+  return NULL;
 }
 
 /**
@@ -276,7 +301,7 @@ xfrun_history_reset (XfrunHistory *history)
  *
  * Return value: a string that should not be modified/freed or %NULL.
  **/
-gchar
+XfrunHistoryItem
 *xfrun_history_completion_start (XfrunHistory *history,
                                  const gchar  *prefix)
 {
@@ -315,9 +340,9 @@ gchar
  * xfrun_history_completion_start must be called once before using this
  * function.
  *
- * Return value: a string that should not be modified/freed or %NULL.
+ * Return value: a #XfrunHistoryItem that should not be modified/freed or %NULL.
  **/
-gchar
+XfrunHistoryItem
 *xfrun_history_completion_next (XfrunHistory *history)
 {
   GList *next;
@@ -351,9 +376,9 @@ gchar
  * xfrun_history_completion_start must be called once before using this
  * function.
  *
- * Return value: a string that should not be modified/freed or %NULL.
+ * Return value: a #XfrunHistoryItem that should not be modified/freed or %NULL.
  **/
-gchar
+XfrunHistoryItem
 *xfrun_history_completion_previous (XfrunHistory *history)
 {
   GList *previous;
@@ -406,19 +431,27 @@ xfrun_history_completion_reset (XfrunHistory *history)
  * Add @command at the end of the history list.
  *
  **/
-void xfrun_history_add (XfrunHistory *history, gchar *command)
+void xfrun_history_add (XfrunHistory *history,
+                        gchar        *command,
+                        gboolean      in_terminal)
 {
-  GList *add;
+  XfrunHistoryItem *item;
+  GList            *add;
 
   g_return_if_fail (XFRUN_IS_HISTORY (history));
+  g_return_if_fail (command != NULL);
+
+  item = g_slice_new (XfrunHistoryItem);
+  item->command = command;
+  item->in_terminal = in_terminal;
 
   /* Add the command to the history list */
   history->priv->history =
-    g_list_append (history->priv->history, command);
+    g_list_prepend (history->priv->history, item);
 
   /* Add the command to the completion */
   add = NULL;
-  add = g_list_append (add, command);
+  add = g_list_append (add, item);
   g_completion_add_items (history->priv->full_completion,
                           add);
 
@@ -448,7 +481,11 @@ void xfrun_history_save (XfrunHistory *history)
 
   for (l = history->priv->history; l != NULL; l = g_list_next (l))
     {
-      contents = g_strconcat (contents, l->data, "\n", NULL);
+      contents = g_strconcat (contents,
+                              ((XfrunHistoryItem *)l->data)->command ? "1" : "0",
+                              ":",
+                              ((XfrunHistoryItem *)l->data)->command,
+                              "\n", NULL);
     }
 
   if (!g_file_set_contents (history_file, contents, -1, &error))
@@ -463,3 +500,16 @@ void xfrun_history_save (XfrunHistory *history)
   g_free (history_file);
 }
 
+/**
+ * xfrun_history_new
+ *
+ * Create a new #XfrunHistory
+ *
+ * Return value: a #XfrunHistory
+ **/
+XfrunHistory
+*xfrun_history_new (void)
+{
+  return g_object_new (XFRUN_TYPE_HISTORY, NULL);
+}
+
diff --git a/xfrun/xfrun-history.h b/xfrun/xfrun-history.h
index 84e53bd..64191fe 100644
--- a/xfrun/xfrun-history.h
+++ b/xfrun/xfrun-history.h
@@ -51,21 +51,30 @@ struct _XfrunHistoryClass
   GObjectClass parent_class;
 };
 
+typedef struct
+{
+  gchar    *command;
+  gboolean  in_terminal;
+} XfrunHistoryItem;
+
 GType xfrun_history_get_type (void);
 
-gchar *xfrun_history_get_next            (XfrunHistory *history);
-gchar *xfrun_history_get_previous        (XfrunHistory *history);
-void   xfrun_history_reset               (XfrunHistory *history);
+XfrunHistoryItem *xfrun_history_get_next            (XfrunHistory *history);
+XfrunHistoryItem *xfrun_history_get_previous        (XfrunHistory *history);
+void              xfrun_history_reset               (XfrunHistory *history);
+
+XfrunHistoryItem *xfrun_history_completion_start    (XfrunHistory *history,
+                                                     const gchar  *prefix);
+XfrunHistoryItem *xfrun_history_completion_next     (XfrunHistory *history);
+XfrunHistoryItem *xfrun_history_completion_previous (XfrunHistory *history);
+void              xfrun_history_completion_reset    (XfrunHistory *history);
 
-gchar *xfrun_history_completion_start    (XfrunHistory *history,
-                                          const gchar  *prefix);
-gchar *xfrun_history_completion_next     (XfrunHistory *history);
-gchar *xfrun_history_completion_previous (XfrunHistory *history);
-void   xfrun_history_completion_reset    (XfrunHistory *history);
+void              xfrun_history_add                 (XfrunHistory *history,
+                                                     gchar        *command,
+                                                     gboolean      in_terminal);
+void              xfrun_history_save                (XfrunHistory *history);
+XfrunHistory     *xfrun_history_new                 (void);
 
-void   xfrun_history_add                 (XfrunHistory *history,
-                                          gchar        *command);
-void   xfrun_history_save                (XfrunHistory *history);
 
 
 #endif /* __XFRUN_HISTORY_H__ */



More information about the Xfce4-commits mailing list