1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 06:03:04 +01:00

improve line history functionality

This commit is contained in:
Jfreegman 2013-12-14 00:36:06 -05:00
parent 34cc4314a5
commit 10ae3865ca
3 changed files with 21 additions and 11 deletions

View File

@ -3,6 +3,7 @@
*/ */
// #define MIN(x, y) (((x) < (y)) ? (x) : (y)) // #define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
/* convert a hex string to binary */ /* convert a hex string to binary */
unsigned char *hex_string_to_bin(char hex_string[]); unsigned char *hex_string_to_bin(char hex_string[]);

View File

@ -89,6 +89,20 @@ void reset_buf(wchar_t *buf, size_t *pos, size_t *len)
*len = 0; *len = 0;
} }
#define HIST_PURGE MAX_LINE_HIST / 4
/* shifts hist items back and makes room for HIST_PURGE new entries */
static void shift_hist_back(wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot)
{
int i;
int n = MAX_LINE_HIST - HIST_PURGE;
for (i = 0; i < n; ++i)
wmemcpy(hst[i], hst[i+HIST_PURGE], MAX_STR_SIZE);
*hst_tot = n;
}
/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history. */ /* adds a line to the ln_history buffer at hst_pos and sets hst_pos to end of history. */
void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot, void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot,
int *hst_pos) int *hst_pos)
@ -96,17 +110,12 @@ void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZ
if (len > MAX_STR_SIZE) if (len > MAX_STR_SIZE)
return; return;
/* If history is full make room for newest entry and don't increment hst_tot */ if (*hst_tot >= MAX_LINE_HIST)
if (*hst_tot == MAX_LINE_HIST) { shift_hist_back(hst, hst_tot);
int i;
for (i = 0; i < MAX_LINE_HIST - 1; ++i)
wmemcpy(hst[i], hst[i+1], MAX_STR_SIZE);
} else {
++(*hst_tot);
}
++(*hst_tot);
*hst_pos = *hst_tot; *hst_pos = *hst_tot;
wmemcpy(hst[*hst_tot-1], buf, len + 1); wmemcpy(hst[*hst_tot-1], buf, len + 1);
} }

View File

@ -34,7 +34,7 @@
#define T_KEY_KILL 0xB /* ctrl-k */ #define T_KEY_KILL 0xB /* ctrl-k */
#define T_KEY_DISCARD 0x15 /* ctrl-u */ #define T_KEY_DISCARD 0x15 /* ctrl-u */
#define T_KEY_NEXT 0x10 /* ctrl-p */ #define T_KEY_NEXT 0x10 /* ctrl-p */
#define T_KEY_PREV 0x0f /* ctrl-o */ #define T_KEY_PREV 0x0F /* ctrl-o */
/* Curses foreground colours (background is black) */ /* Curses foreground colours (background is black) */
enum { enum {
@ -111,7 +111,7 @@ struct StatusBar {
bool is_online; bool is_online;
}; };
#define MAX_LINE_HIST 64 #define MAX_LINE_HIST 128
/* chat and groupchat window/buffer holder */ /* chat and groupchat window/buffer holder */
struct ChatContext { struct ChatContext {