summaryrefslogtreecommitdiffstats
path: root/src/history.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2022-02-26 00:41:29 +0100
committerLouis Burda <quent.burda@gmail.com>2022-02-26 00:41:47 +0100
commit53cb5a2a0d1540a37e6e5d1c1673e8354d5208a5 (patch)
tree60664a2db1d96c3269578d2a3e0f9b6fff439845 /src/history.c
parent3dad446ab7a6e207229b56af552dd3304a9ab11b (diff)
downloadtmus-53cb5a2a0d1540a37e6e5d1c1673e8354d5208a5.tar.gz
tmus-53cb5a2a0d1540a37e6e5d1c1673e8354d5208a5.zip
Migrate away from wchar_t, default to utf8
Diffstat (limited to 'src/history.c')
-rw-r--r--src/history.c149
1 files changed, 82 insertions, 67 deletions
diff --git a/src/history.c b/src/history.c
index 031e0da..5bb6288 100644
--- a/src/history.c
+++ b/src/history.c
@@ -1,61 +1,25 @@
+#define _GNU_SOURCE
+
#include "history.h"
#include "util.h"
#include <string.h>
-#include <wchar.h>
-void
-history_init(struct history *history)
-{
- list_init(&history->list);
- history->input = inputln_alloc();
- history->sel = history->input;
-}
+static struct inputln *
+history_list_prev(struct inputln *cur, const char *search);
-void
-history_submit(struct history *history)
-{
- /* if chose from history free input */
- if (history->sel != history->input) {
- link_pop(LINK(history->input));
- inputln_free(history->input);
- }
-
- /* pop first in case already in history */
- link_pop(LINK(history->sel));
- history_add(history, history->sel);
-
- /* create new input buf and add to hist */
- history->input = inputln_alloc();
- history->sel = history->input;
- history_add(history, history->sel);
-}
-
-void
-history_free(struct history *history)
-{
- struct link *iter, *next;
- struct link *ln;
-
- ln = link_pop(LINK(history->input));
- inputln_free(UPCAST(ln, struct inputln));
-
- list_free(&history->list, (link_free_func) inputln_free,
- LINK_OFFSET(struct inputln, link));
-
- history->input = NULL;
- history->sel = NULL;
-}
+static struct inputln *
+history_list_next(struct inputln *cur, const char *search);
struct inputln *
-history_list_prev(struct inputln *cur, const wchar_t *search)
+history_list_prev(struct inputln *cur, const char *search)
{
struct link *iter;
struct inputln *ln;
for (iter = cur->link.prev; iter && iter->prev; iter = iter->prev) {
ln = UPCAST(iter, struct inputln);
- if (!search || !*search || wcscasestr(ln->buf, search))
+ if (!search || !*search || strcasestr(ln->buf, search))
return ln;
}
@@ -63,7 +27,7 @@ history_list_prev(struct inputln *cur, const wchar_t *search)
}
struct inputln *
-history_list_next(struct inputln *cur, const wchar_t *search)
+history_list_next(struct inputln *cur, const char *search)
{
struct link *iter;
struct inputln *ln;
@@ -71,7 +35,7 @@ history_list_next(struct inputln *cur, const wchar_t *search)
iter = cur->link.next;
while (LIST_INNER(iter)) {
ln = UPCAST(iter, struct inputln);
- if (!search || !*search || wcscasestr(ln->buf, search))
+ if (!search || !*search || strcasestr(ln->buf, search))
return ln;
iter = iter->next;
}
@@ -80,6 +44,50 @@ history_list_next(struct inputln *cur, const wchar_t *search)
}
void
+history_init(struct history *history)
+{
+ list_init(&history->list);
+ history->input = inputln_alloc();
+ history->sel = history->input;
+}
+
+void
+history_deinit(struct history *history)
+{
+ struct link *link;
+ struct inputln *ln;
+
+ link = link_pop(LINK(history->input));
+ ln = UPCAST(link, struct inputln);
+ inputln_free(ln);
+ history->input = NULL;
+
+ list_free(&history->list, (link_free_func) inputln_free,
+ LINK_OFFSET(struct inputln, link));
+
+ history->sel = NULL;
+}
+
+void
+history_submit(struct history *history)
+{
+ /* if chose from history free input */
+ if (history->sel != history->input) {
+ link_pop(LINK(history->input));
+ inputln_free(history->input);
+ }
+
+ /* pop first in case already in history */
+ link_pop(LINK(history->sel));
+ history_add(history, history->sel);
+
+ /* create new input buf and add to hist */
+ history->input = inputln_alloc();
+ history->sel = history->input;
+ history_add(history, history->sel);
+}
+
+void
history_prev(struct history *history)
{
history->sel = history_list_prev(history->sel, history->input->buf);
@@ -100,7 +108,8 @@ history_add(struct history *history, struct inputln *line)
if (list_len(&history->list) == HISTORY_MAX) {
/* pop last item to make space */
back = list_pop_back(&history->list);
- inputln_free(UPCAST(back, struct inputln));
+ ln = UPCAST(back, struct inputln);
+ inputln_free(ln);
}
list_push_front(&history->list, LINK(line));
@@ -114,9 +123,16 @@ inputln_init(struct inputln *ln)
ln->cap = 0;
ln->cur = 0;
ln->link = LINK_EMPTY;
+
inputln_resize(ln, 128);
}
+void
+inputln_deinit(struct inputln *ln)
+{
+ free(ln->buf);
+}
+
struct inputln *
inputln_alloc(void)
{
@@ -132,11 +148,23 @@ inputln_alloc(void)
void
inputln_free(struct inputln *ln)
{
- free(ln->buf);
+ inputln_deinit(ln);
free(ln);
}
void
+inputln_resize(struct inputln *ln, size_t size)
+{
+ ASSERT(size != 0);
+
+ ln->cap = size;
+ ln->buf = realloc(ln->buf, ln->cap * sizeof(char));
+ OOM_CHECK(ln->buf);
+ ln->len = MIN(ln->len, ln->cap-1);
+ ln->buf[ln->len] = '\0';
+}
+
+void
inputln_left(struct inputln *ln)
{
ln->cur = MAX(0, ln->cur-1);
@@ -149,14 +177,13 @@ inputln_right(struct inputln *ln)
}
void
-inputln_addch(struct inputln *line, wchar_t c)
+inputln_addch(struct inputln *line, char c)
{
int i;
if (line->len + 1 >= line->cap) {
line->cap *= 2;
- line->buf = realloc(line->buf,
- line->cap * sizeof(wchar_t));
+ line->buf = realloc(line->buf, line->cap);
}
for (i = line->len; i > line->cur; i--)
@@ -195,31 +222,19 @@ inputln_copy(struct inputln *dst, struct inputln *src)
dst->buf = NULL;
}
dst->len = src->len;
- dst->buf = wcsdup(src->buf);
+ dst->buf = strdup(src->buf);
OOM_CHECK(dst->buf);
dst->cap = src->len + 1;
dst->cur = dst->len;
}
void
-inputln_replace(struct inputln *line, const wchar_t *str)
+inputln_replace(struct inputln *line, const char *str)
{
- line->len = wcslen(str);
+ line->len = strlen(str);
if (line->cap <= line->len)
inputln_resize(line, line->len + 1);
- line->buf = wcscpy(line->buf, str);
+ strncpy(line->buf, str, line->len);
line->cur = line->len;
}
-void
-inputln_resize(struct inputln *ln, size_t size)
-{
- ASSERT(size != 0);
-
- ln->cap = size;
- ln->buf = realloc(ln->buf, ln->cap * sizeof(wchar_t));
- OOM_CHECK(ln->buf);
- ln->len = MIN(ln->len, ln->cap-1);
- ln->buf[ln->len] = '\0';
-}
-