Skip to content

Commit

Permalink
Implement bold, italic, underline, strikethrough rendering
Browse files Browse the repository at this point in the history
Closes #364
  • Loading branch information
znschaffer authored Jan 28, 2023
1 parent 2544793 commit dc9deec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
25 changes: 20 additions & 5 deletions crates/libtiny_tui/src/msg_area/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,29 @@ impl Line {
}
}

fn add_attr(&mut self, attr: u16) {
if let SegStyle::Fixed(style) = self.current_seg.style {
self.set_message_style(SegStyle::Fixed(Style {
fg: style.fg | attr,
bg: style.bg,
}))
} else {
let style = SegStyle::Fixed(Style {
fg: termbox_simple::TB_DEFAULT | attr,
bg: termbox_simple::TB_DEFAULT,
});
self.set_message_style(style)
}
}

fn add_text_inner(&mut self, str: &str) {
for format_event in parse_irc_formatting(str) {
match format_event {
IrcFormatEvent::Bold
| IrcFormatEvent::Italic
| IrcFormatEvent::Underline
| IrcFormatEvent::Strikethrough
| IrcFormatEvent::Monospace => {
IrcFormatEvent::Bold => self.add_attr(termbox_simple::TB_BOLD),
IrcFormatEvent::Italic => self.add_attr(termbox_simple::TB_ITALIC),
IrcFormatEvent::Underline => self.add_attr(termbox_simple::TB_UNDERLINE),
IrcFormatEvent::Strikethrough => self.add_attr(termbox_simple::TB_STRIKETHROUGH),
IrcFormatEvent::Monospace => {
// TODO
}
IrcFormatEvent::Text(text) => {
Expand Down
14 changes: 14 additions & 0 deletions crates/termbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use unicode_width::UnicodeWidthChar;
pub const TB_DEFAULT: u16 = 0x0000;
pub const TB_BOLD: u16 = 0x0100;
pub const TB_UNDERLINE: u16 = 0x0200;
pub const TB_ITALIC: u16 = 0x0400;
pub const TB_STRIKETHROUGH: u16 = 0x0800;

pub struct Termbox {
// Not available in test instances
Expand Down Expand Up @@ -396,6 +398,8 @@ impl Termbox {

let bold = fg & TB_BOLD != 0;
let underline = fg & TB_UNDERLINE != 0;
let italic = fg & TB_ITALIC != 0;
let strikethrough = fg & TB_STRIKETHROUGH != 0;

self.last_fg = fg;
self.last_bg = bg;
Expand All @@ -416,6 +420,16 @@ impl Termbox {
.extend_from_slice(termion::style::Bold.as_ref());
}

if italic {
self.output_buffer
.extend_from_slice(termion::style::Italic.as_ref());
}

if strikethrough {
self.output_buffer
.extend_from_slice(termion::style::CrossedOut.as_ref());
}

if fg != 0 {
write!(
self.output_buffer,
Expand Down

0 comments on commit dc9deec

Please sign in to comment.