forked from barnowl/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory.c
74 lines (57 loc) · 1.65 KB
/
history.c
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
#include "owl.h"
void owl_history_init(owl_history *h)
{
g_queue_init(&h->hist);
h->cur = h->hist.tail; /* current position in history */
h->partial = false; /* is the 0th element is partially composed? */
}
void owl_history_cleanup(owl_history *h)
{
g_queue_foreach(&h->hist, (GFunc)g_free, NULL);
g_queue_clear(&h->hist);
}
const char *owl_history_get_prev(owl_history *h)
{
if (!h) return NULL;
if (h->cur == NULL || g_list_previous(h->cur) == NULL) return NULL;
h->cur = g_list_previous(h->cur);
return h->cur->data;
}
const char *owl_history_get_next(owl_history *h)
{
if (!h) return NULL;
if (h->cur == NULL || g_list_next(h->cur) == NULL) return NULL;
h->cur = g_list_next(h->cur);
return h->cur->data;
}
void owl_history_store(owl_history *h, const char *line, bool partial)
{
if (!h) return;
owl_history_reset(h);
/* check if the line is the same as the last */
if (!partial && !g_queue_is_empty(&h->hist) &&
strcmp(line, g_queue_peek_tail(&h->hist)) == 0)
return;
/* if we've reached the max history size, pop off the last element */
if (g_queue_get_length(&h->hist) >= OWL_HISTORYSIZE)
g_free(g_queue_pop_head(&h->hist));
/* add the new line */
g_queue_push_tail(&h->hist, g_strdup(line));
h->partial = partial;
h->cur = h->hist.tail;
}
void owl_history_reset(owl_history *h)
{
if (!h) return;
/* if partial is set, remove the first entry first */
if (h->partial) {
g_free(g_queue_pop_tail(&h->hist));
h->partial = false;
}
h->cur = h->hist.tail;
}
int owl_history_is_touched(const owl_history *h)
{
if (!h) return(0);
return h->cur != NULL && g_list_next(h->cur) != NULL;
}