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

simplify code a bit

This commit is contained in:
Jfreegman 2013-12-11 03:29:31 -05:00
parent 83a81f6db6
commit 629041d465
6 changed files with 24 additions and 31 deletions

View File

@ -370,7 +370,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
else if (key == KEY_UP) { /* fetches previous item in history */
if (ctx->hst_pos >= 0) {
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot,
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
&ctx->hst_pos, LN_HIST_MV_UP);
mv_curs_end(self->window, ctx->len, y2, x2);
}
@ -378,7 +378,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
else if (key == KEY_DOWN) { /* fetches next item in history */
if (ctx->hst_pos < ctx->hst_tot) {
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot,
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
&ctx->hst_pos, LN_HIST_MV_DWN);
mv_curs_end(self->window, ctx->len, y2, x2);
}

View File

@ -281,9 +281,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
else if (key == KEY_END) { /* END key: move cursor to end of line */
if (ctx->pos != ctx->len) {
ctx->pos = ctx->len;
int end_y = (ctx->len / x2) + (y2 - CURS_Y_OFFSET);
int end_x = ctx->len % x2;
wmove(self->window, end_y, end_x);
mv_curs_end(self->window, ctx->len, y2, x2);
}
}
@ -311,7 +309,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
else if (key == KEY_UP) { /* fetches previous item in history */
if (ctx->hst_pos >= 0) {
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot,
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
&ctx->hst_pos, LN_HIST_MV_UP);
mv_curs_end(self->window, ctx->len, y2, x2);
}
@ -319,7 +317,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
else if (key == KEY_DOWN) { /* fetches next item in history */
if (ctx->hst_pos < ctx->hst_tot) {
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, &ctx->hst_tot,
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
&ctx->hst_pos, LN_HIST_MV_DWN);
mv_curs_end(self->window, ctx->len, y2, x2);
}

View File

@ -192,4 +192,4 @@ void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x)
int end_y = (len / max_x) + (max_y - CURS_Y_OFFSET);
int end_x = len % max_x;
wmove(w, end_y, end_x);
}
}

View File

@ -168,7 +168,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
else if (key == KEY_UP) { /* fetches previous item in history */
if (prt->hst_pos >= 0) {
wmove(self->window, prt->orig_y, X_OFST);
fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, &prt->hst_tot,
fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot,
&prt->hst_pos, LN_HIST_MV_UP);
}
}
@ -176,7 +176,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key)
else if (key == KEY_DOWN) { /* fetches next item in history */
if (prt->hst_pos < prt->hst_tot) {
wmove(self->window, prt->orig_y, X_OFST);
fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, &prt->hst_tot,
fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot,
&prt->hst_pos, LN_HIST_MV_DWN);
}
}

View File

@ -89,45 +89,41 @@ 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)
/* 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,
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);
wmemcpy(hst[i], hst[i+1], MAX_STR_SIZE);
} else {
++(*hst_tot);
}
*hst_pos = *hst_tot;
wmemcpy(&hst[(*hst_tot - 1) * MAX_STR_SIZE], buf, len + 1);
wmemcpy(hst[*hst_tot-1], 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)
hst_pos is decremented or incremented depending on key_dir. */
void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE],
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)
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];
const wchar_t *hst_line = hst[*hst_pos];
size_t h_len = wcslen(hst_line);
wmemcpy(buf, hst_line, h_len + 1);

View File

@ -35,12 +35,11 @@ enum {
LN_HIST_MV_DWN,
};
/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to last history item.
Assumes entries are of size MAX_STR_SIZE */
void add_line_to_hist(const wchar_t *buf, size_t len, void *hst, int *hst_tot, int *hst_pos);
/* adds a line to the ln_history buffer at hst_pos and sets hst_pos to last history item. */
void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot,
int *hst_pos);
/* 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);
hst_pos is decremented or incremented depending on key_dir. */
void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE],
int hst_tot, int *hst_pos, int key_dir);