about summary refs log tree commit diff stats
path: root/prof_history.c
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-04-30 01:09:42 +0100
committerJames Booth <boothj5@gmail.com>2012-04-30 01:09:42 +0100
commitb6c5fef45eaadce4463ee60e60eb0a0252b61255 (patch)
tree2ddb6ce1f874413648a7dcd74fb03f7c41ec95da /prof_history.c
parentc511d7c99f792823fd2727d3a5a1f1fd722b8a33 (diff)
downloadprofani-tty-b6c5fef45eaadce4463ee60e60eb0a0252b61255.tar.gz
Started work on bash style history
Diffstat (limited to 'prof_history.c')
-rw-r--r--prof_history.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/prof_history.c b/prof_history.c
new file mode 100644
index 00000000..3aca0150
--- /dev/null
+++ b/prof_history.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <glib.h>
+
+#include "prof_history.h"
+
+struct p_history_t {
+    GList *items; // the history
+    GList *session; // a copy of the history for edits
+    GList *session_current; // pointer to the current item in the session
+    guint max_size;
+};
+
+PHistory p_history_new(unsigned int size)
+{
+    PHistory new_history = malloc(sizeof(struct p_history_t));
+    new_history->items = NULL;
+    new_history->session = NULL;
+    new_history->max_size = size;
+
+    return new_history;
+}
+
+void p_history_append(PHistory history, char *item)
+{
+    char *copied = strdup(item);
+    // if already at max size
+    if (g_list_length(history->items) == history->max_size) {
+
+        // remove first element
+        GList *first = g_list_first(history->items);
+        const char *first_item = (char *) first->data;
+        history->items = g_list_remove(history->items, first_item);
+    }
+    
+    // append the new item onto the history
+    history->items = g_list_append(history->items, copied);
+
+    // delete the current session
+    if (history->session != NULL) {
+        g_list_free(history->session);
+        history->session = NULL;
+    }
+}
+
+char * p_history_previous(PHistory history)
+{
+    if (history->items == NULL) {
+        return NULL;
+    }
+
+    if (history->session == NULL) {
+        history->session = g_list_copy(history->items);
+        history->session_current = g_list_last(history->session);
+    } else {
+        history->session_current = g_list_previous(history->session_current);
+    }
+
+    // set to first if rolled over beginning
+    if (history->session_current == NULL) {
+        history->session_current = g_list_first(history->session);
+    }
+
+    char *item = (char *) history->session_current->data;
+    char *result = malloc((strlen(item) + 1) * sizeof(char));
+    strcpy(result, item);
+
+    return result;
+}
+
+char * p_history_next(PHistory history)
+{
+    if (history->session == NULL) {
+        return NULL;
+    } else {
+        history->session_current = g_list_next(history->session_current);
+    }
+
+    // set to last if rolled over end
+    if (history->session_current == NULL) {
+        history->session_current = g_list_last(history->session);
+    }
+
+    char *item = (char *) history->session_current->data;
+    char *result = malloc((strlen(item) + 1) * sizeof(char));
+    strcpy(result, item);
+
+    return result;
+}