From 21423d483b33f62fa3087852a178771e98998b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Mon, 23 Sep 2024 17:15:00 +0200 Subject: [PATCH] Remove msg_area::Line::force_recalculation Invalidate the line layout on every modification, instead of requiring the caller to do it via `force_recalculation`. Remove `force_recalculation`. --- crates/libtiny_tui/src/msg_area/line.rs | 20 ++++++++++---------- crates/libtiny_tui/src/msg_area/mod.rs | 2 -- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/crates/libtiny_tui/src/msg_area/line.rs b/crates/libtiny_tui/src/msg_area/line.rs index ca5c786e..30bc2f1c 100644 --- a/crates/libtiny_tui/src/msg_area/line.rs +++ b/crates/libtiny_tui/src/msg_area/line.rs @@ -124,7 +124,11 @@ impl Line { } } - fn add_text_inner(&mut self, str: &str) { + /// Add the text to the line. The text can contain IRC formatting characters. The text should + /// not contain other control characters like '\n' or '\r'. + pub(crate) fn add_text(&mut self, str: &str, style: SegStyle) { + self.set_message_style(style); + for format_event in parse_irc_formatting(str) { match format_event { IrcFormatEvent::Bold => self.add_attr(termbox_simple::TB_BOLD), @@ -157,20 +161,16 @@ impl Line { } } } - } - pub(crate) fn add_text(&mut self, str: &str, style: SegStyle) { - self.set_message_style(style); - self.add_text_inner(str) + self.line_data.set_dirty(); } + /// Add a single character to the line. The character should not be an IRC formatting character + /// or other control characters like '\n' and '\r'. pub(crate) fn add_char(&mut self, char: char, style: SegStyle) { self.set_message_style(style); self.current_seg.string.push(char); - } - - pub(crate) fn force_recalculation(&mut self) { - self.line_data.set_dirty() + self.line_data.set_dirty(); } /// Calculates the number of lines that this line will be. @@ -363,7 +363,7 @@ fn align_test() { 67 8 */ - line.add_text_inner("12345678"); + line.add_text("12345678", SegStyle::UserMsg); assert_eq!(line.rendered_height(3), 4); } diff --git a/crates/libtiny_tui/src/msg_area/mod.rs b/crates/libtiny_tui/src/msg_area/mod.rs index 094126a4..bafe76ea 100644 --- a/crates/libtiny_tui/src/msg_area/mod.rs +++ b/crates/libtiny_tui/src/msg_area/mod.rs @@ -229,8 +229,6 @@ impl MsgArea { F: Fn(&mut Line), { f(&mut self.lines[idx]); - // Line was modified so we need to invalidate its height - self.lines[idx].force_recalculation(); } pub(crate) fn clear(&mut self) {