Changeset 2bc6bb16


Ignore:
Timestamp:
Jun 25, 2011, 2:28:29 AM (6 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Children:
1c01cdf7
Parents:
32c331d
git-author:
Anders Kaseorg <andersk@mit.edu> (06/22/11 02:36:29)
git-committer:
Anders Kaseorg <andersk@mit.edu> (06/25/11 02:28:29)
Message:
history: Store history in a GQueue

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • history.c

    r32c331d r2bc6bb16  
    33void owl_history_init(owl_history *h)
    44{
    5   owl_list_create(&(h->hist));
    6   h->cur=0;                     /* current position in history */
     5  g_queue_init(&h->hist);
     6  h->cur = h->hist.tail;        /* current position in history */
    77  h->partial = false;           /* is the 0th element is partially composed? */
    88}
     
    1010const char *owl_history_get_prev(owl_history *h)
    1111{
    12 
    1312  if (!h) return NULL;
    1413
    15   if (owl_list_get_size(&(h->hist))==0) return(NULL);
     14  if (h->cur == NULL || g_list_previous(h->cur) == NULL) return NULL;
    1615
    17   if (h->cur == owl_list_get_size(&(h->hist))-1) {
    18     return(NULL);
    19   }
    20 
    21   h->cur++;
    22 
    23   return(owl_list_get_element(&(h->hist), h->cur));
     16  h->cur = g_list_previous(h->cur);
     17  return h->cur->data;
    2418}
    2519
     
    2721{
    2822  if (!h) return NULL;
    29   if (owl_list_get_size(&(h->hist))==0) return(NULL);
    30   if (h->cur==0) {
    31     return(NULL);
    32   }
    3323
    34   h->cur--;
    35   return(owl_list_get_element(&(h->hist), h->cur));
     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;
    3628}
    3729
    3830void owl_history_store(owl_history *h, const char *line, bool partial)
    3931{
    40   int size;
    41 
    4232  if (!h) return;
    43   size=owl_list_get_size(&(h->hist));
    4433
    4534  owl_history_reset(h);
    4635
    4736  /* check if the line is the same as the last */
    48   if (!partial && owl_list_get_size(&(h->hist)) > 0 &&
    49       strcmp(line, owl_list_get_element(&(h->hist), 0)) == 0)
     37  if (!partial && !g_queue_is_empty(&h->hist) &&
     38      strcmp(line, g_queue_peek_tail(&h->hist)) == 0)
    5039    return;
    5140
    5241  /* if we've reached the max history size, pop off the last element */
    53   if (size>OWL_HISTORYSIZE) {
    54     g_free(owl_list_get_element(&(h->hist), size-1));
    55     owl_list_remove_element(&(h->hist), size-1);
    56   }
     42  if (g_queue_get_length(&h->hist) > OWL_HISTORYSIZE)
     43    g_free(g_queue_pop_head(&h->hist));
    5744
    5845  /* add the new line */
    59   owl_list_prepend_element(&(h->hist), g_strdup(line));
     46  g_queue_push_tail(&h->hist, g_strdup(line));
    6047  h->partial = partial;
     48  h->cur = h->hist.tail;
    6149}
    6250
     
    6755  /* if partial is set, remove the first entry first */
    6856  if (h->partial) {
    69     g_free(owl_list_get_element(&(h->hist), 0));
    70     owl_list_remove_element(&(h->hist), 0);
     57    g_free(g_queue_pop_tail(&h->hist));
    7158    h->partial = false;
    7259  }
    7360
    74   h->cur=0;
     61  h->cur = h->hist.tail;
    7562}
    7663
     
    7865{
    7966  if (!h) return(0);
    80   return h->cur > 0;
     67  return h->cur != NULL && g_list_next(h->cur) != NULL;
    8168}
  • owl.h

    r7c80ea42 r2bc6bb16  
    455455
    456456typedef struct _owl_history {
    457   owl_list hist;
    458   int cur;
     457  GQueue hist;
     458  GList *cur;
    459459  bool partial;
    460460} owl_history;
Note: See TracChangeset for help on using the changeset viewer.