pub struct StyleTable { /* private fields */ }Expand description
Styles, stored once each. Runs refer to them by StyleId.
Interning hashes a style’s public parts (colours, attributes and link)
with a fast non-cryptographic hash and confirms a match with ==, so
metadata still tells styles apart. A style made of two plain colours can
be interned by the colours alone (intern_rgb),
without building a Style unless it is new.
The table only grows while it is used; a table kept between frames (a
Screen’s) drops what its cells no longer
use with retain.
Implementations§
Source§impl StyleTable
impl StyleTable
pub fn new() -> Self
Sourcepub fn intern(&mut self, style: Option<&Style>) -> StyleId
pub fn intern(&mut self, style: Option<&Style>) -> StyleId
The id of style, adding it if it is new. None is StyleId::NONE.
Sourcepub fn intern_rgb(&mut self, fg: Option<Rgb>, bg: Option<Rgb>) -> StyleId
pub fn intern_rgb(&mut self, fg: Option<Rgb>, bg: Option<Rgb>) -> StyleId
The id of a style of foreground fg and background bg in true
colour (either missing), as Style::from_color with
Color::from_rgb would make it; neither is
StyleId::NONE. A pixel’s cell: the style is built only the first
time the pair is seen.
use rich::{Color, Style};
use rich_ext::frame::StyleTable;
let mut table = StyleTable::new();
let id = table.intern_rgb(Some([255, 0, 0]), None);
let red = Style::from_color(Some(Color::from_rgb(255, 0, 0)), None);
assert_eq!(table.intern(Some(&red)), id);
assert_eq!(table.rgb(id), Some((Some([255, 0, 0]), None)));Sourcepub fn rgb(&self, id: StyleId) -> Option<(Option<Rgb>, Option<Rgb>)>
pub fn rgb(&self, id: StyleId) -> Option<(Option<Rgb>, Option<Rgb>)>
The colours of a style interned with intern_rgb
(or found by it): what a painter can write without asking the style.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of entries, counting StyleId::NONE.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Always false: the table always holds StyleId::NONE.
Sourcepub fn piled_up(&self) -> bool
pub fn piled_up(&self) -> bool
Whether the table has at least doubled since the last
retain and holds more than a few hundred styles:
when dropping the unused ones is worth a pass over the cells.
Sourcepub fn retain(&mut self, keep: &[bool]) -> StyleRemap
pub fn retain(&mut self, keep: &[bool]) -> StyleRemap
Keep only the styles keep marks (by index; StyleId::NONE is
always kept, and ids past keep’s end are dropped), renumbered in
their order. The result maps the old ids to the new: everything
holding an old id must be moved over with it.
use rich::Style;
use rich_ext::frame::{StyleId, StyleTable};
let mut table = StyleTable::new();
let bold = table.intern(Some(&Style::parse("bold").unwrap()));
let red = table.intern(Some(&Style::parse("red").unwrap()));
let mut keep = vec![false; table.len()];
keep[red.index()] = true;
let remap = table.retain(&keep);
assert_eq!(table.len(), 2);
assert_eq!(remap.get(bold), None);
let red = remap.get(red).unwrap();
assert_eq!(table.get(red), Some(&Style::parse("red").unwrap()));
assert_eq!(remap.get(StyleId::NONE), Some(StyleId::NONE));