about summary refs log tree commit diff stats
path: root/prof_history.c
diff options
context:
space:
mode:
Diffstat (limited to 'prof_history.c')
-rw-r--r--prof_history.c56
1 files changed, 44 insertions, 12 deletions
diff --git a/prof_history.c b/prof_history.c
index cf82fafd..75c22cb3 100644
--- a/prof_history.c
+++ b/prof_history.c
@@ -16,6 +16,7 @@ struct p_history_t {
 };
 
 static void _replace_history_with_session(PHistory history);
+static gboolean _adding_new(PHistory history);
 
 PHistory p_history_new(unsigned int size)
 {
@@ -32,6 +33,8 @@ PHistory p_history_new(unsigned int size)
 
 void p_history_append(PHistory history, char *item)
 {
+    printf("\n");
+    // copy input, default to ""
     char *copied = "";
     if (item != NULL) {
         copied = strdup(item);
@@ -60,22 +63,41 @@ void p_history_append(PHistory history, char *item)
 
     // if editing history (session exists with possible changes)
     } else {
+        
+        // update the current item with the input
+        history->sess_curr->data = copied;
 
-        // if adding a new item, copy the session over the history
-        if (history->sess_curr == history->sess_new) {
-            history->sess_curr->data = copied;
+        // if current points to last we're just adding the new item, 
+        // so replace items with session removing the last element if its ""
+        if (_adding_new(history)) {
+            
+            // remove last if ""
+            if (strcmp(history->sess_curr->data, "") == 0) {
+                history->session = g_list_reverse(history->session);
+                GList *first = g_list_first(history->session);
+                history->session = g_list_remove(history->session, first->data);
+                history->session = g_list_reverse(history->session);
+            }
+            
             _replace_history_with_session(history);
 
         // otherwise, adding edited history item
         } else {
-            if (history->sess_new != NULL) {
-                // copy the current string to the last element in session
-                history->sess_new->data = copied;
-            } else {
-                g_list_append(history->session, copied);
-            }
             
-            // replace the edited version with the data from the history
+            // remove the last element, its either "" or some new data
+            // we want to discard
+            history->session = g_list_reverse(history->session);
+            GList *first = g_list_first(history->session);
+            history->session = g_list_remove(history->session, first->data);
+            history->session = g_list_reverse(history->session);
+            
+            // copy the data at the current position and append it to the 
+            // session
+            char *new = strdup(history->sess_curr->data);
+            history->session = g_list_append(history->session, new);
+            
+            // replace the edited version with the data from the original 
+            // history
             history->sess_curr->data = strdup(history->items_curr->data);
             
             // rewrite history from the session
@@ -84,6 +106,11 @@ void p_history_append(PHistory history, char *item)
     }
 }
 
+static gboolean _adding_new(PHistory history)
+{
+    return (history->sess_curr == g_list_last(history->session));
+}
+
 static void _replace_history_with_session(PHistory history)
 {
     g_list_free(history->items);
@@ -110,6 +137,7 @@ char * p_history_previous(PHistory history, char *item)
         return NULL;
     }
 
+    // copy input, default to ""
     char *copied = "";
     if (item != NULL) {
         copied = strdup(item);
@@ -135,7 +163,10 @@ char * p_history_previous(PHistory history, char *item)
         
         // move to previous
         history->sess_curr = g_list_previous(history->sess_curr);
-        history->items_curr = g_list_previous(history->items_curr);
+        if (history->items_curr == NULL)
+            history->items_curr = g_list_last(history->items);
+        else 
+            history->items_curr = g_list_previous(history->items_curr);
 
         // set to first if rolled over beginning
         if (history->sess_curr == NULL) {
@@ -151,11 +182,12 @@ char * p_history_previous(PHistory history, char *item)
 char * p_history_next(PHistory history, char *item)
 {
 
-    // no history, or no session, return item
+    // no history, or no session, return NULL
     if ((history->items == NULL) || (history->session == NULL)) {
         return NULL;
     }
 
+    // copy input, default to ""
     char *copied = "";
     if (item != NULL) {
         copied = strdup(item);
ooth <boothj5@gmail.com> 2015-12-22 00:45:15 +0000 committer James Booth <boothj5@gmail.com> 2015-12-22 00:45:15 +0000 Uncommented functional tests' href='/danisanti/profani-tty/commit/tests/functionaltests/functionaltests.c?id=377a63d038be41986106c115e658ab199c570109'>377a63d0 ^
f189dbc6 ^




377a63d0 ^














2b7894cc ^
377a63d0 ^

373f47c7 ^
52e2917b ^
6303e0e2 ^


377a63d0 ^











fca59a31 ^
377a63d0 ^


a978bb12 ^
377a63d0 ^












377a63d0 ^







ee664bf1 ^

69ac8097 ^


e1e0fda8 ^

97c5072f ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114