source: history.c @ e6d7e4e

release-1.10release-1.8release-1.9
Last change on this file since e6d7e4e was 95b52d1, checked in by Anders Kaseorg <andersk@mit.edu>, 13 years ago
history: Store history in a GQueue Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 1.5 KB
Line 
1#include "owl.h"
2
3void owl_history_init(owl_history *h)
4{
5  g_queue_init(&h->hist);
6  h->cur = h->hist.tail;        /* current position in history */
7  h->partial = false;           /* is the 0th element is partially composed? */
8}
9
10const char *owl_history_get_prev(owl_history *h)
11{
12  if (!h) return NULL;
13
14  if (h->cur == NULL || g_list_previous(h->cur) == NULL) return NULL;
15
16  h->cur = g_list_previous(h->cur);
17  return h->cur->data;
18}
19
20const char *owl_history_get_next(owl_history *h)
21{
22  if (!h) return NULL;
23
24  if (h->cur == NULL || g_list_next(h->cur) == NULL) return NULL;
25
26  h->cur = g_list_next(h->cur);
27  return h->cur->data;
28}
29
30void owl_history_store(owl_history *h, const char *line, bool partial)
31{
32  if (!h) return;
33
34  owl_history_reset(h);
35
36  /* check if the line is the same as the last */
37  if (!partial && !g_queue_is_empty(&h->hist) &&
38      strcmp(line, g_queue_peek_tail(&h->hist)) == 0)
39    return;
40
41  /* if we've reached the max history size, pop off the last element */
42  if (g_queue_get_length(&h->hist) > OWL_HISTORYSIZE)
43    g_free(g_queue_pop_head(&h->hist));
44
45  /* add the new line */
46  g_queue_push_tail(&h->hist, g_strdup(line));
47  h->partial = partial;
48  h->cur = h->hist.tail;
49}
50
51void owl_history_reset(owl_history *h)
52{
53  if (!h) return;
54
55  /* if partial is set, remove the first entry first */
56  if (h->partial) {
57    g_free(g_queue_pop_tail(&h->hist));
58    h->partial = false;
59  }
60
61  h->cur = h->hist.tail;
62}
63
64int owl_history_is_touched(const owl_history *h)
65{
66  if (!h) return(0);
67  return h->cur != NULL && g_list_next(h->cur) != NULL;
68}
Note: See TracBrowser for help on using the repository browser.