1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-07-01 10:46:45 +02:00

add line history with up/down keys

This commit is contained in:
Jfreegman
2013-12-11 00:10:09 -05:00
parent ccc0640dab
commit 9a5a598c5a
8 changed files with 148 additions and 5 deletions

View File

@ -7,6 +7,7 @@
#include "toxic_windows.h"
#include "misc_tools.h"
#include "toxic_strings.h"
/* Adds char to buffer at pos */
void add_char_to_buf(wchar_t *buf, size_t *pos, size_t *len, wint_t ch)
@ -88,6 +89,53 @@ void reset_buf(wchar_t *buf, size_t *pos, size_t *len)
*len = 0;
}
/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history.
Assumes entries are of size MAX_STR_SIZE */
void add_line_to_hist(const wchar_t *buf, size_t len, void *ln_history, int *hst_tot, int *hst_pos)
{
if (len > MAX_STR_SIZE)
return;
wchar_t *hst = (wchar_t *) ln_history;
/* If history is full make room for newest entry and don't increment hst_tot */
if (*hst_tot == MAX_LINE_HIST) {
int i;
for (i = 0; i < MAX_LINE_HIST - 1; ++i)
wmemcpy(&hst[MAX_STR_SIZE * i], &hst[MAX_STR_SIZE * (i + 1)], MAX_STR_SIZE);
} else {
++(*hst_tot);
}
*hst_pos = *hst_tot;
wmemcpy(&hst[(*hst_tot - 1) * MAX_STR_SIZE], buf, len + 1);
}
/* copies history item at hst_pos to buf. Sets pos and len to the len of the history item.
hst_pos is decremented or incremented depending on key_dir.
Assumes history entries are of size MAX_STR_SIZE */
void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, const void *ln_history, int *hst_tot,
int *hst_pos, int key_dir)
{
if (key_dir == LN_HIST_MV_UP) {
if (--(*hst_pos) < 0)
++(*hst_pos);
} else {
if (++(*hst_pos) == *hst_tot)
--(*hst_pos);
}
wchar_t *hst = (wchar_t *) ln_history;
const wchar_t *hst_line = &hst[*hst_pos * MAX_STR_SIZE];
size_t h_len = wcslen(hst_line);
wmemcpy(buf, hst_line, h_len + 1);
*pos = h_len;
*len = h_len;
}
/* looks for the first instance in list that begins with the last entered word in buf according to pos,
then fills buf with the complete word. e.g. "Hello jo" would complete the buffer
with "Hello john".
@ -160,7 +208,7 @@ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int
if (char_to_wcs_buf(newbuf, ubuf, MAX_STR_SIZE) == -1)
return -1;
wmemcpy(buf, newbuf, MAX_STR_SIZE);
wcscpy(buf, newbuf);
*len += (size_t) diff;
*pos += (size_t) diff;