use ratatui::style::{Color as RtColor, Modifier, Style as RtStyle};
use uncurses::cell::Cell as CzCell;
use uncurses::color::Color as CzColor;
use uncurses::style::{AttrFlags, Style as CzStyle, UnderlineStyle};
pub fn to_uncurses_color(c: RtColor) -> Option<CzColor> {
Some(match c {
RtColor::Reset => return None,
RtColor::Black => CzColor::Black,
RtColor::Red => CzColor::Red,
RtColor::Green => CzColor::Green,
RtColor::Yellow => CzColor::Yellow,
RtColor::Blue => CzColor::Blue,
RtColor::Magenta => CzColor::Magenta,
RtColor::Cyan => CzColor::Cyan,
RtColor::Gray => CzColor::White,
RtColor::DarkGray => CzColor::BrightBlack,
RtColor::LightRed => CzColor::BrightRed,
RtColor::LightGreen => CzColor::BrightGreen,
RtColor::LightYellow => CzColor::BrightYellow,
RtColor::LightBlue => CzColor::BrightBlue,
RtColor::LightMagenta => CzColor::BrightMagenta,
RtColor::LightCyan => CzColor::BrightCyan,
RtColor::White => CzColor::BrightWhite,
RtColor::Rgb(r, g, b) => CzColor::Rgb(r, g, b),
RtColor::Indexed(i) => CzColor::Indexed(i),
})
}
fn to_attrs(m: Modifier) -> AttrFlags {
let mut a = AttrFlags::empty();
if m.contains(Modifier::BOLD) {
a |= AttrFlags::BOLD;
}
if m.contains(Modifier::DIM) {
a |= AttrFlags::FAINT;
}
if m.contains(Modifier::ITALIC) {
a |= AttrFlags::ITALIC;
}
if m.contains(Modifier::SLOW_BLINK) {
a |= AttrFlags::SLOW_BLINK;
}
if m.contains(Modifier::RAPID_BLINK) {
a |= AttrFlags::RAPID_BLINK;
}
if m.contains(Modifier::REVERSED) {
a |= AttrFlags::REVERSE;
}
if m.contains(Modifier::HIDDEN) {
a |= AttrFlags::CONCEAL;
}
if m.contains(Modifier::CROSSED_OUT) {
a |= AttrFlags::STRIKETHROUGH;
}
a
}
pub fn to_uncurses_style(s: RtStyle) -> CzStyle {
let attrs = to_attrs(s.add_modifier);
let underline = if s.add_modifier.contains(Modifier::UNDERLINED) {
UnderlineStyle::Single
} else {
UnderlineStyle::None
};
let mut style = CzStyle::default().underline_style(underline);
if let Some(fg) = s.fg.and_then(to_uncurses_color) {
style = style.fg(fg);
}
if let Some(bg) = s.bg.and_then(to_uncurses_color) {
style = style.bg(bg);
}
if let Some(uc) = s.underline_color.and_then(to_uncurses_color) {
style = style.underline_color(uc);
}
style.attrs(attrs)
}
const HALFWIDTH_KATAKANA_VOICED_SOUND_MARK: char = '\u{FF9E}';
const HALFWIDTH_KATAKANA_SEMI_VOICED_SOUND_MARK: char = '\u{FF9F}';
fn str_cell_width(s: &str) -> usize {
use unicode_width::UnicodeWidthStr;
if s.len() == 1 {
1
} else {
let width = s.width();
let extra = s
.chars()
.filter(|c| {
matches!(
*c,
HALFWIDTH_KATAKANA_VOICED_SOUND_MARK
| HALFWIDTH_KATAKANA_SEMI_VOICED_SOUND_MARK
)
})
.count();
width + extra
}
}
pub(crate) fn cell_from_ratatui(rc: &ratatui::buffer::Cell) -> CzCell {
let style = RtStyle {
fg: Some(rc.fg),
bg: Some(rc.bg),
underline_color: Some(rc.underline_color),
add_modifier: rc.modifier,
sub_modifier: Modifier::empty(),
};
let style = to_uncurses_style(style);
let symbol = rc.symbol();
let width = str_cell_width(symbol).clamp(1, u8::MAX as usize) as u8;
let cell = CzCell::new(symbol, width);
cell.style(style)
}
#[cfg(test)]
mod tests {
use super::cell_from_ratatui;
fn width_of(symbol: &str) -> u8 {
let mut rc = ratatui::buffer::Cell::default();
rc.set_symbol(symbol);
cell_from_ratatui(&rc).width()
}
#[test]
fn a_converted_cell_keeps_the_columns_its_symbol_measures() {
assert_eq!(width_of("a"), 1);
assert_eq!(width_of("世"), 2);
assert_eq!(width_of("abc"), 3);
}
#[test]
fn a_symbol_too_wide_for_a_cell_keeps_the_widest_count_one_can_hold() {
let huge = "\u{4e16}".repeat(32_768);
assert_eq!(super::str_cell_width(&huge), 65_536);
assert_eq!(width_of(&huge), u8::MAX);
}
#[test]
fn a_symbol_that_measures_nothing_still_holds_its_column() {
assert_eq!(width_of("\u{200b}"), 1);
}
}