From 8b6a5813e630744321cdf3be13064329468af232 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 6 Jul 2014 01:46:07 -0400 Subject: [PATCH] fix line_info_reset_start() bugs, a few other small fixes/cleanup --- src/help.c | 10 +++++----- src/line_info.c | 29 ++++++++++++++++------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/help.c b/src/help.c index 3977135..e1ca85d 100644 --- a/src/help.c +++ b/src/help.c @@ -203,11 +203,11 @@ static void help_draw_keys(ToxWindow *self) wprintw(win, "Key bindings:\n"); wattroff(win, A_BOLD | COLOR_PAIR(RED)); - wprintw(win, " Ctrl+O / Ctrl+P : Navigate through the windows/tabs\n"); - wprintw(win, " Page Up / Page Down : Scroll window history one line\n"); - wprintw(win, " Ctrl+F / Ctrl+V : Scroll window history half a page\n"); - wprintw(win, " Ctrl+H : Move to the bottom of window history\n"); - wprintw(win, " Ctrl+[ / Ctrl+] : Scroll peer list in groupchats\n"); + wprintw(win, " Ctrl+O and Ctrl+P : Navigate through the tabs\n"); + wprintw(win, " Page Up and Page Down : Scroll window history one line\n"); + wprintw(win, " Ctrl+F and Ctrl+V : Scroll window history half a page\n"); + wprintw(win, " Ctrl+H : Move to the bottom of window history\n"); + wprintw(win, " Ctrl+[ and Ctrl+] : Scroll peer list in groupchats\n"); help_draw_bottom_menu(win); diff --git a/src/line_info.c b/src/line_info.c index 32d1725..f60a35a 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -34,34 +34,39 @@ extern struct user_settings *user_settings; void line_info_init(struct history *hst) { - hst->line_root = malloc(sizeof(struct line_info)); + hst->line_root = calloc(1, sizeof(struct line_info)); if (hst->line_root == NULL) exit_toxic_err("failed in line_info_init", FATALERR_MEMORY); - memset(hst->line_root, 0, sizeof(struct line_info)); hst->line_start = hst->line_root; hst->line_end = hst->line_start; hst->queue_sz = 0; } -/* resets line_start */ +/* resets line_start (page end) */ static void line_info_reset_start(ToxWindow *self, struct history *hst) { + struct line_info *line = hst->line_end; + + if (line->prev == NULL) + return; + int y2, x2; getmaxyx(self->window, y2, x2); - struct line_info *line = hst->line_end; - - uint16_t lncnt = 0; int side_offst = self->is_groupchat ? SIDEBAR_WIDTH : 0; - int top_offst = self->is_chat ? 2 : 0; + int top_offst = self->is_chat || self->is_prompt ? 2 : 0; int max_y = (y2 - CHATBOX_HEIGHT - top_offst); - while (line->prev && lncnt < max_y) { - lncnt += (1 + line->newlines) +( line->len / (x2 - side_offst)); + int curlines = 0; + int nxtlines = line->newlines + (line->len / (x2 - side_offst)); + + do { + curlines += 1 + nxtlines; line = line->prev; - } + nxtlines = line->newlines + (line->len / (x2 - side_offst)); + } while (line->prev && curlines + nxtlines < max_y); hst->line_start = line; } @@ -125,13 +130,11 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na uint8_t type, uint8_t bold, uint8_t colour) { struct history *hst = self->chatwin->hst; - struct line_info *new_line = malloc(sizeof(struct line_info)); + struct line_info *new_line = calloc(1, sizeof(struct line_info)); if (new_line == NULL) exit_toxic_err("failed in line_info_add", FATALERR_MEMORY); - memset(new_line, 0, sizeof(struct line_info)); - int len = 1; /* there will always be a newline */ /* for type-specific formatting in print function */