Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of IRC color code (0x03) without colors afterwards #435

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading