Skip to content

Commit

Permalink
Improve handling of IRC color code (0x03) without colors afterwards (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
osa1 authored Nov 20, 2024
1 parent 0d7681b commit eaed50d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unreleased

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

# 2024/01/01: 0.12.0

Expand Down
6 changes: 5 additions & 1 deletion crates/libtiny_tui/src/msg_area/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ impl Line {
self.set_message_style(SegStyle::Fixed(Style { fg: bg, bg: fg }));
}
}
IrcFormatEvent::Reset => {
// TODO: ResetColors should only reset the colors and not the formatting.
// However we don't store the current formatting and we can't apply formatting to
// a color from the current color scheme (e.g. `SegStyle::UserMsg`). For now reset
// the colors and formatting.
IrcFormatEvent::ResetColors | IrcFormatEvent::Reset => {
self.set_message_style(SegStyle::UserMsg);
}
}
Expand Down
21 changes: 13 additions & 8 deletions crates/libtiny_wire/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ pub enum IrcFormatEvent<'a> {
bg: Option<Color>,
},

/// Reverse current background and foreground
/// Reverse current background and foreground colors.
ReverseColor,

/// Reset formatting to the default
/// Reset background and foreground colors to the defaults.
ResetColors,

/// Reset formatting to the default.
Reset,
}

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

CHAR_COLOR => {
self.bump(1);
match self.parse_color() {
Some((fg, bg)) => return Some(IrcFormatEvent::Color { fg, bg }),
None => {
// Just skip the control char
}
}
return match self.parse_color() {
Some((fg, bg)) => Some(IrcFormatEvent::Color { fg, bg }),
None => Some(IrcFormatEvent::ResetColors),
};
}

CHAR_HEX_COLOR => {
Expand Down Expand Up @@ -389,6 +390,7 @@ pub fn remove_irc_control_chars(str: &str) -> String {
| IrcFormatEvent::Monospace
| IrcFormatEvent::Color { .. }
| IrcFormatEvent::ReverseColor
| IrcFormatEvent::ResetColors
| IrcFormatEvent::Reset => {}
IrcFormatEvent::Text(text) => s.push_str(text),
}
Expand Down Expand Up @@ -431,6 +433,7 @@ fn test_parse_text_2() {
let s = "a\x03";
let mut parser = parse_irc_formatting(s);
assert_eq!(parser.next(), Some(IrcFormatEvent::Text("a")));
assert_eq!(parser.next(), Some(IrcFormatEvent::ResetColors));
assert_eq!(parser.next(), None);
}

Expand All @@ -439,6 +442,7 @@ fn test_parse_text_3() {
let s = "a\x03b";
let mut parser = parse_irc_formatting(s);
assert_eq!(parser.next(), Some(IrcFormatEvent::Text("a")));
assert_eq!(parser.next(), Some(IrcFormatEvent::ResetColors));
assert_eq!(parser.next(), Some(IrcFormatEvent::Text("b")));
assert_eq!(parser.next(), None);
}
Expand Down Expand Up @@ -511,6 +515,7 @@ fn test_parse_text_5() {

let s = "\x03,a";
let mut parser = parse_irc_formatting(s);
assert_eq!(parser.next(), Some(IrcFormatEvent::ResetColors));
assert_eq!(parser.next(), Some(IrcFormatEvent::Text(",a")));
assert_eq!(parser.next(), None);
}
Expand Down

0 comments on commit eaed50d

Please sign in to comment.