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
According to [1], a `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 committed Sep 17, 2024
1 parent e665b5a commit fb9811d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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 @@ -152,7 +152,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 fb9811d

Please sign in to comment.