From b7c9e8ca23dee372ddf8ad5d1e845950ec15a2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Fri, 15 Jan 2016 03:49:24 +0100 Subject: [PATCH] degesch: make backlog limit configurable --- NEWS | 2 ++ degesch.c | 45 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/NEWS b/NEWS index ebe7f7f..dc455df 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ * degesch: added a basic last.fm "now playing" plugin + * degesch: backlog limit was made configurable + * Various bugfixes diff --git a/degesch.c b/degesch.c index 92669fd..0886df2 100644 --- a/degesch.c +++ b/degesch.c @@ -88,9 +88,6 @@ enum /// Some arbitrary limit for the history file #define HISTORY_LIMIT 10000 -/// How many lines of backlog to store in memory -#define BACKLOG_LIMIT 1000 - /// Characters that separate words #define WORD_BREAKING_CHARS " \f\n\r\t\v" @@ -1547,6 +1544,7 @@ struct app_context // TODO: make buffer names fully unique like weechat does struct str_map buffers_by_name; ///< Buffers by name + unsigned backlog_limit; ///< Limit for buffer lines time_t last_displayed_msg_time; ///< Time of last displayed message // Terminal: @@ -1616,6 +1614,8 @@ app_context_init (struct app_context *self) str_map_init (&self->buffers_by_name); self->buffers_by_name.key_xfrm = tolower_ascii_strxfrm; + // So that we don't lose the logo shortly after startup + self->backlog_limit = 1000; self->last_displayed_msg_time = time (NULL); char *encoding = nl_langinfo (CODESET); @@ -1701,6 +1701,7 @@ on_config_show_all_prefixes_change (struct config_item *item) refresh_prompt (ctx); } +static void on_config_backlog_limit_change (struct config_item *item); static void on_config_attribute_change (struct config_item *item); static void on_config_logging_change (struct config_item *item); @@ -1903,6 +1904,12 @@ static struct config_schema g_config_behaviour[] = // You can use the -r switch, however that makes `less` very confused // about line wrapping, and the result is suboptimal. + { .name = "backlog_limit", + .comment = "Maximum number of lines stored in the backlog", + .type = CONFIG_ITEM_INTEGER, + .validate = config_validate_nonnegative, + .default_ = "1000", + .on_change = on_config_backlog_limit_change }, { .name = "backlog_helper", .comment = "Shell command to display a buffer's history", .type = CONFIG_ITEM_STRING, @@ -2991,6 +2998,29 @@ formatter_flush (struct formatter *self, FILE *stream, bool raw_attributes) // --- Buffers ----------------------------------------------------------------- +static void +buffer_pop_excess_lines (struct app_context *ctx, struct buffer *self) +{ + int to_delete = (int) self->lines_count - (int) ctx->backlog_limit; + while (to_delete-- > 0 && self->lines) + { + struct buffer_line *excess = self->lines; + LIST_UNLINK_WITH_TAIL (self->lines, self->lines_tail, excess); + buffer_line_destroy (excess); + self->lines_count--; + } +} + +static void +on_config_backlog_limit_change (struct config_item *item) +{ + struct app_context *ctx = item->user_data; + ctx->backlog_limit = MAX (item->value.integer, INT_MAX); + + LIST_FOR_EACH (struct buffer, iter, ctx->buffers) + buffer_pop_excess_lines (ctx, iter); +} + static void buffer_update_time (struct app_context *ctx, time_t now) { @@ -3114,14 +3144,6 @@ log_formatter (struct app_context *ctx, if (!buffer) buffer = ctx->global_buffer; - if (buffer->lines_count >= BACKLOG_LIMIT) - { - struct buffer_line *popped = buffer->lines; - LIST_UNLINK_WITH_TAIL (buffer->lines, buffer->lines_tail, popped); - buffer_line_destroy (popped); - buffer->lines_count--; - } - struct buffer_line *line = buffer_line_new (); line->flags = flags; line->when = time (NULL); @@ -3130,6 +3152,7 @@ log_formatter (struct app_context *ctx, line->formatter = xmalloc (sizeof *line->formatter); *line->formatter = *f; + buffer_pop_excess_lines (ctx, buffer); LIST_APPEND_WITH_TAIL (buffer->lines, buffer->lines_tail, line); buffer->lines_count++;