From 15815bf4bbb41adb1b0ddf8f1bc137e02a1183f6 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 31 Mar 2014 21:26:09 -0400 Subject: [PATCH] split big function up --- src/line_info.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/line_info.c b/src/line_info.c index 34a3580..a95501c 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -91,6 +91,22 @@ void line_info_cleanup(struct history *hst) } } +/* moves root forward and frees previous root */ +static void line_info_root_fwd(struct history *hst) +{ + struct line_info *tmp = hst->line_root->next; + tmp->prev = NULL; + + if (hst->line_start->prev == NULL) { /* if line_start is root move it forward as well */ + hst->line_start = hst->line_start->next; + hst->line_start->prev = NULL; + ++hst->start_id; + } + + free(hst->line_root); + hst->line_root = tmp; +} + void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, uint8_t type, uint8_t bold, uint8_t colour) { @@ -141,19 +157,9 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na hst->line_end->next = new_line; hst->line_end = new_line; - /* If chat history exceeds limit move root forward and free old root */ if (++hst->line_items > MAX_HISTORY) { - struct line_info *tmp = hst->line_root->next; - tmp->prev = NULL; - - if (hst->line_start->prev == NULL) { /* if line_start is root move it forward as well */ - hst->line_start = hst->line_start->next; - hst->line_start->prev = NULL; - ++hst->start_id; - } - - free(hst->line_root); - hst->line_root = tmp; + --hst->line_items; + line_info_root_fwd(hst); } int newlines = 0; @@ -180,11 +186,11 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na /* move line_start forward proportionate to the number of new lines */ if (y + hst->queue_lns - hst->queue >= max_y) { - while (lines > 0 && hst->line_start->next) { + while (lines > 0) { ++hst->start_id; lines -= (1 + hst->line_start->len / (x2 - offst)); - if (!hst->scroll_mode) + if (!hst->scroll_mode && hst->line_start->next) hst->line_start = hst->line_start->next; } }