Skip to content

Commit eaed50d

Browse files
authored
Improve handling of IRC color code (0x03) without colors afterwards (#435)
0x03 without a color code following should reset the current bg and fg colors. We can't reset just the colors without also resetting the formatting, for now reset colors + formatting.
1 parent 0d7681b commit eaed50d

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Unreleased
22

33
- Improve nick matching to avoid highlighting message incorrectly. (#430)
4+
- Fix resetting message color when a color prefix (0x03) is not followed by a
5+
color code. (#434)
46

57
# 2024/01/01: 0.12.0
68

crates/libtiny_tui/src/msg_area/line.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ impl Line {
156156
self.set_message_style(SegStyle::Fixed(Style { fg: bg, bg: fg }));
157157
}
158158
}
159-
IrcFormatEvent::Reset => {
159+
// TODO: ResetColors should only reset the colors and not the formatting.
160+
// However we don't store the current formatting and we can't apply formatting to
161+
// a color from the current color scheme (e.g. `SegStyle::UserMsg`). For now reset
162+
// the colors and formatting.
163+
IrcFormatEvent::ResetColors | IrcFormatEvent::Reset => {
160164
self.set_message_style(SegStyle::UserMsg);
161165
}
162166
}

crates/libtiny_wire/src/formatting.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ pub enum IrcFormatEvent<'a> {
3030
bg: Option<Color>,
3131
},
3232

33-
/// Reverse current background and foreground
33+
/// Reverse current background and foreground colors.
3434
ReverseColor,
3535

36-
/// Reset formatting to the default
36+
/// Reset background and foreground colors to the defaults.
37+
ResetColors,
38+
39+
/// Reset formatting to the default.
3740
Reset,
3841
}
3942

@@ -325,12 +328,10 @@ impl<'a> Iterator for FormatEventParser<'a> {
325328

326329
CHAR_COLOR => {
327330
self.bump(1);
328-
match self.parse_color() {
329-
Some((fg, bg)) => return Some(IrcFormatEvent::Color { fg, bg }),
330-
None => {
331-
// Just skip the control char
332-
}
333-
}
331+
return match self.parse_color() {
332+
Some((fg, bg)) => Some(IrcFormatEvent::Color { fg, bg }),
333+
None => Some(IrcFormatEvent::ResetColors),
334+
};
334335
}
335336

336337
CHAR_HEX_COLOR => {
@@ -389,6 +390,7 @@ pub fn remove_irc_control_chars(str: &str) -> String {
389390
| IrcFormatEvent::Monospace
390391
| IrcFormatEvent::Color { .. }
391392
| IrcFormatEvent::ReverseColor
393+
| IrcFormatEvent::ResetColors
392394
| IrcFormatEvent::Reset => {}
393395
IrcFormatEvent::Text(text) => s.push_str(text),
394396
}
@@ -431,6 +433,7 @@ fn test_parse_text_2() {
431433
let s = "a\x03";
432434
let mut parser = parse_irc_formatting(s);
433435
assert_eq!(parser.next(), Some(IrcFormatEvent::Text("a")));
436+
assert_eq!(parser.next(), Some(IrcFormatEvent::ResetColors));
434437
assert_eq!(parser.next(), None);
435438
}
436439

@@ -439,6 +442,7 @@ fn test_parse_text_3() {
439442
let s = "a\x03b";
440443
let mut parser = parse_irc_formatting(s);
441444
assert_eq!(parser.next(), Some(IrcFormatEvent::Text("a")));
445+
assert_eq!(parser.next(), Some(IrcFormatEvent::ResetColors));
442446
assert_eq!(parser.next(), Some(IrcFormatEvent::Text("b")));
443447
assert_eq!(parser.next(), None);
444448
}
@@ -511,6 +515,7 @@ fn test_parse_text_5() {
511515

512516
let s = "\x03,a";
513517
let mut parser = parse_irc_formatting(s);
518+
assert_eq!(parser.next(), Some(IrcFormatEvent::ResetColors));
514519
assert_eq!(parser.next(), Some(IrcFormatEvent::Text(",a")));
515520
assert_eq!(parser.next(), None);
516521
}

0 commit comments

Comments
 (0)