From 6ad8a190532fdec58547b3237576fe0c3f30e9e8 Mon Sep 17 00:00:00 2001 From: Jabber Developer Date: Tue, 7 Oct 2025 15:12:20 +0200 Subject: [PATCH] fix(buffer): use dynamic buffer size to prevent unnecessary buffer cleanups Refactor buffer trimming logic to use dynamic line counts (_lines per entry) against PAD_SIZE (10k) instead of fixed entry count (MAX_BUFFER_SIZE=200). This prevents premature cleanups for short messages, reduces scrolling disruptions during history loads, and scales better with rendered content size. Delete oldest/newest entry from opposite end only when total lines approach ncurses pad limit, minimizing unnecessary deletions. Simplify overflow warning log by removing unused MAX_BUFFER_SIZE reference. Update buffer.c header for improved DX: - Add detailed module overview explaining role (in-memory UI buffer vs. DB persistence), key features (trimming, metadata), and integration (ncurses). - Shorten copyright/license boilerplate to concise pointer (LICENSE ref). - Add fork notice; preserve original copyrights per GPLv3. Partially addresses #36 (stuck scroll mitigation via fewer cleanups) Related to #36 --- src/ui/buffer.c | 56 ++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/ui/buffer.c b/src/ui/buffer.c index 5eda4438..9ba489b5 100644 --- a/src/ui/buffer.c +++ b/src/ui/buffer.c @@ -1,37 +1,32 @@ /* * buffer.c - * vim: expandtab:ts=4:sts=4:sw=4 + * + * Message buffer implementation for CProof. + * + * This module provides an in-memory buffer for managing active chat entries in the + * console-based XMPP client. Separate from persistent SQLite storage, it holds + * messages and metadata solely for UI rendering, ensuring responsive scrolling and + * display without exceeding ncurses pad limits (10k lines). By tracking rendered + * line counts per entry, it proactively trims content to prevent overflow, enabling + * seamless integration with ncurses for position-aware rendering. + * + * Key features and operations: + * - Append/prepend entries (messages) with automatic line-based cleanup. + * - Track cumulative lines for overflow prevention and efficient buffer sizing. + * - Remove entries by ID or index; mark delivery receipts as received. + * - Retrieve entries by index or ID for rendering and history queries. + * - Store metadata including timestamps, senders, themes, and ncurses y-positions. + * + * CProof. Fork of Profanity (since 2025). * * Copyright (C) 2012 - 2019 James Booth * Copyright (C) 2019 - 2025 Michael Vetter + * Copyright (C) 2025 CProof Developers * - * This file is part of Profanity. - * - * Profanity is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Profanity is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Profanity. If not, see . - * - * In addition, as a special exception, the copyright holders give permission to - * link the code of portions of this program with the OpenSSL library under - * certain conditions as described in each individual source file, and - * distribute linked combinations including the two. - * - * You must obey the GNU General Public License in all respects for all of the - * code used other than OpenSSL. If you modify file(s) with this exception, you - * may extend this exception to your version of the file(s), but you are not - * obligated to do so. If you do not wish to do so, delete this exception - * statement from your version. If you delete this exception statement from all - * source files in the program, then also delete it here. + * Licensed under the GNU General Public License, version 3, + * with the OpenSSL exception. See LICENSE for details. * + * vim: expandtab:ts=4:sts=4:sw=4 */ #include "config.h" @@ -54,7 +49,6 @@ #include "ui/window.h" #include "ui/buffer.h" -#define MAX_BUFFER_SIZE 200 #define STRDUP_OR_NULL(str) ((str) ? strdup(str) : NULL) struct prof_buff_t @@ -108,7 +102,7 @@ _buffer_add(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime* t buffer->lines += e->_lines; - while (g_slist_length(buffer->entries) >= MAX_BUFFER_SIZE) { + while (buffer->lines >= PAD_SIZE) { // Delete message from the opposite size to free buffer GSList* buffer_entry_to_delete = append ? buffer->entries : g_slist_last(buffer->entries); ProfBuffEntry* entry_to_delete = (ProfBuffEntry*)buffer_entry_to_delete->data; @@ -118,8 +112,8 @@ _buffer_add(ProfBuff buffer, const char* show_char, int pad_indent, GDateTime* t } if (from_jid && y_end_pos == y_start_pos) { - log_warning("Ncurses Overflow! From: %s, position: %d, ID: %s, append: %s, used message buffer size: %d, message buffer size: %d, rendered lines buffer size: %d", - from_jid, y_start_pos, id, append ? "TRUE" : "FALSE", g_slist_length(buffer->entries), MAX_BUFFER_SIZE, PAD_SIZE); + log_warning("Ncurses Overflow! From: %s, position: %d, ID: %s, append: %s, used message buffer size: %d, rendered lines buffer size: %d", + from_jid, y_start_pos, id, append ? "TRUE" : "FALSE", g_slist_length(buffer->entries), PAD_SIZE); // At this point we have a message that caused overflow of the render buffer. // Ideally, we want to clean other messages to rerender everything properly. To do so, // first we need to determine the size of the message that we are trying to display, -- 2.49.1