| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use std::collections::HashMap; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 15 |
pub enum Color { |
| 16 |
Default, |
| 17 |
Named(u8), |
| 18 |
Indexed(u8), |
| 19 |
Rgb(u8, u8, u8), |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] |
| 24 |
pub struct Attrs { |
| 25 |
pub bold: bool, |
| 26 |
pub italic: bool, |
| 27 |
pub underline: bool, |
| 28 |
pub reverse: bool, |
| 29 |
pub dim: bool, |
| 30 |
pub strikethrough: bool, |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
| 57 |
#[repr(C)] |
| 58 |
pub struct Cell { |
| 59 |
pub(crate) c_raw: u32, |
| 60 |
pub(crate) fg_word: u32, |
| 61 |
pub(crate) bg_word: u32, |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
const CHAR_MASK: u32 = (1 << 21) - 1; |
| 66 |
const MARKS_SHIFT: u32 = 21; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
const MARKS_LIMIT: u32 = (1 << 11) - 1; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
pub(crate) const MAX_MARKS: usize = 8; |
| 79 |
|
| 80 |
const SRC_DEFAULT: u32 = 0; |
| 81 |
const SRC_NAMED: u32 = 1; |
| 82 |
const SRC_INDEXED: u32 = 2; |
| 83 |
const SRC_RGB: u32 = 3; |
| 84 |
|
| 85 |
const SRC_SHIFT: u32 = 24; |
| 86 |
const SRC_MASK: u32 = 0b11 << SRC_SHIFT; |
| 87 |
const RGB_MASK: u32 = 0x00FF_FFFF; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
const FLAG_WIDE: u32 = 1 << 26; |
| 98 |
const FLAG_SPACER: u32 = 1 << 27; |
| 99 |
const FLAG_WIDTH_MASK: u32 = FLAG_WIDE | FLAG_SPACER; |
| 100 |
|
| 101 |
const ATTR_BOLD: u32 = 1 << 26; |
| 102 |
const ATTR_ITALIC: u32 = 1 << 27; |
| 103 |
const ATTR_UNDERLINE: u32 = 1 << 28; |
| 104 |
const ATTR_REVERSE: u32 = 1 << 29; |
| 105 |
const ATTR_DIM: u32 = 1 << 30; |
| 106 |
const ATTR_STRIKE: u32 = 1 << 31; |
| 107 |
|
| 108 |
pub(crate) fn encode_color(c: Color) -> u32 { |
| 109 |
match c { |
| 110 |
Color::Default => SRC_DEFAULT << SRC_SHIFT, |
| 111 |
Color::Named(i) => (SRC_NAMED << SRC_SHIFT) | i as u32, |
| 112 |
Color::Indexed(i) => (SRC_INDEXED << SRC_SHIFT) | i as u32, |
| 113 |
Color::Rgb(r, g, b) => { |
| 114 |
(SRC_RGB << SRC_SHIFT) | ((r as u32) << 16) | ((g as u32) << 8) | b as u32 |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
fn decode_color(word: u32) -> Color { |
| 120 |
let payload = word & RGB_MASK; |
| 121 |
match (word & SRC_MASK) >> SRC_SHIFT { |
| 122 |
SRC_NAMED => Color::Named(payload as u8), |
| 123 |
SRC_INDEXED => Color::Indexed(payload as u8), |
| 124 |
SRC_RGB => Color::Rgb((payload >> 16) as u8, (payload >> 8) as u8, payload as u8), |
| 125 |
_ => Color::Default, |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
pub(crate) fn encode_attrs(a: Attrs) -> u32 { |
| 130 |
let mut bits = 0u32; |
| 131 |
if a.bold { |
| 132 |
bits |= ATTR_BOLD; |
| 133 |
} |
| 134 |
if a.italic { |
| 135 |
bits |= ATTR_ITALIC; |
| 136 |
} |
| 137 |
if a.underline { |
| 138 |
bits |= ATTR_UNDERLINE; |
| 139 |
} |
| 140 |
if a.reverse { |
| 141 |
bits |= ATTR_REVERSE; |
| 142 |
} |
| 143 |
if a.dim { |
| 144 |
bits |= ATTR_DIM; |
| 145 |
} |
| 146 |
if a.strikethrough { |
| 147 |
bits |= ATTR_STRIKE; |
| 148 |
} |
| 149 |
bits |
| 150 |
} |
| 151 |
|
| 152 |
impl Cell { |
| 153 |
pub fn new(c: char, fg: Color, bg: Color, attrs: Attrs) -> Self { |
| 154 |
Self { |
| 155 |
c_raw: c as u32, |
| 156 |
fg_word: encode_color(fg) | encode_attrs(attrs), |
| 157 |
bg_word: encode_color(bg), |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#[inline] |
| 167 |
pub fn c(&self) -> char { |
| 168 |
|
| 169 |
char::from_u32(self.c_raw & CHAR_MASK).unwrap_or('\u{FFFD}') |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
#[inline] |
| 174 |
pub fn marks_id(&self) -> u16 { |
| 175 |
((self.c_raw >> MARKS_SHIFT) & MARKS_LIMIT) as u16 |
| 176 |
} |
| 177 |
|
| 178 |
#[inline] |
| 179 |
pub(crate) fn with_marks(self, id: u16) -> Self { |
| 180 |
Self { |
| 181 |
c_raw: (self.c_raw & CHAR_MASK) | (u32::from(id) << MARKS_SHIFT), |
| 182 |
..self |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
#[inline] |
| 187 |
pub fn fg(&self) -> Color { |
| 188 |
decode_color(self.fg_word) |
| 189 |
} |
| 190 |
|
| 191 |
#[inline] |
| 192 |
pub fn bg(&self) -> Color { |
| 193 |
decode_color(self.bg_word) |
| 194 |
} |
| 195 |
|
| 196 |
pub fn attrs(&self) -> Attrs { |
| 197 |
let b = self.fg_word; |
| 198 |
Attrs { |
| 199 |
bold: b & ATTR_BOLD != 0, |
| 200 |
italic: b & ATTR_ITALIC != 0, |
| 201 |
underline: b & ATTR_UNDERLINE != 0, |
| 202 |
reverse: b & ATTR_REVERSE != 0, |
| 203 |
dim: b & ATTR_DIM != 0, |
| 204 |
strikethrough: b & ATTR_STRIKE != 0, |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
#[inline] |
| 211 |
pub fn reverse(&self) -> bool { |
| 212 |
self.fg_word & ATTR_REVERSE != 0 |
| 213 |
} |
| 214 |
|
| 215 |
#[inline] |
| 216 |
pub fn underline(&self) -> bool { |
| 217 |
self.fg_word & ATTR_UNDERLINE != 0 |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
#[inline] |
| 223 |
pub fn has_bg(&self) -> bool { |
| 224 |
(self.bg_word & SRC_MASK) != 0 || self.reverse() |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
#[inline] |
| 229 |
pub fn is_wide(&self) -> bool { |
| 230 |
self.bg_word & FLAG_WIDE != 0 |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
#[inline] |
| 239 |
pub fn is_spacer(&self) -> bool { |
| 240 |
self.bg_word & FLAG_SPACER != 0 |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
#[inline] |
| 247 |
pub fn cols(&self) -> u16 { |
| 248 |
if self.is_wide() { 2 } else { 1 } |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
pub(crate) fn pad() -> Self { |
| 256 |
Self { |
| 257 |
bg_word: FLAG_SPACER, |
| 258 |
..Self::default() |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
pub(crate) fn wide_pair(c: char, fg_word: u32, bg_word: u32) -> (Self, Self) { |
| 264 |
let bg_word = bg_word & !FLAG_WIDTH_MASK; |
| 265 |
( |
| 266 |
Self { |
| 267 |
c_raw: c as u32, |
| 268 |
fg_word, |
| 269 |
bg_word: bg_word | FLAG_WIDE, |
| 270 |
}, |
| 271 |
Self { |
| 272 |
c_raw: ' ' as u32, |
| 273 |
fg_word, |
| 274 |
bg_word: bg_word | FLAG_SPACER, |
| 275 |
}, |
| 276 |
) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
#[derive(Debug, Default)] |
| 288 |
pub(crate) struct MarkTable { |
| 289 |
|
| 290 |
|
| 291 |
seqs: Vec<Box<[char]>>, |
| 292 |
ids: HashMap<Box<[char]>, u16>, |
| 293 |
} |
| 294 |
|
| 295 |
impl MarkTable { |
| 296 |
|
| 297 |
pub(crate) fn get(&self, id: u16) -> &[char] { |
| 298 |
match id.checked_sub(1) { |
| 299 |
Some(i) => self.seqs.get(i as usize).map_or(&[], |s| s), |
| 300 |
None => &[], |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
pub(crate) fn intern(&mut self, seq: &[char]) -> Option<u16> { |
| 310 |
if let Some(id) = self.ids.get(seq) { |
| 311 |
return Some(*id); |
| 312 |
} |
| 313 |
let id = u16::try_from(self.seqs.len() + 1).ok()?; |
| 314 |
if u32::from(id) > MARKS_LIMIT { |
| 315 |
return None; |
| 316 |
} |
| 317 |
let seq: Box<[char]> = seq.into(); |
| 318 |
self.seqs.push(seq.clone()); |
| 319 |
self.ids.insert(seq, id); |
| 320 |
Some(id) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
pub(crate) fn char_cols(c: char) -> u16 { |
| 335 |
match unicode_width::UnicodeWidthChar::width(c) { |
| 336 |
Some(2) => 2, |
| 337 |
Some(0) => 0, |
| 338 |
_ => 1, |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
impl Default for Cell { |
| 343 |
fn default() -> Self { |
| 344 |
Self { |
| 345 |
c_raw: ' ' as u32, |
| 346 |
fg_word: 0, |
| 347 |
bg_word: 0, |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
const _: () = assert!(std::mem::size_of::<Cell>() == 12); |
| 353 |
|
| 354 |
#[cfg(test)] |
| 355 |
mod tests { |
| 356 |
use crate::*; |
| 357 |
|
| 358 |
#[test] |
| 359 |
fn a_cell_is_still_twelve_bytes_and_still_copy() { |
| 360 |
|
| 361 |
|
| 362 |
assert_eq!(std::mem::size_of::<Cell>(), 12); |
| 363 |
let a = Cell::default(); |
| 364 |
let b = a; |
| 365 |
assert_eq!(a, b); |
| 366 |
} |
| 367 |
} |
| 368 |
|