From f258c840948fa67e7ce624331fcca15ec004302f Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 26 Sep 2022 16:55:37 +0200 Subject: [PATCH] Expose Widthdb directly via Frame --- examples/overlapping_graphemes.rs | 2 +- src/frame.rs | 20 ++------------------ src/lib.rs | 2 +- src/widthdb.rs | 16 +++++++++++----- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/examples/overlapping_graphemes.rs b/examples/overlapping_graphemes.rs index adf610c..e87dff7 100644 --- a/examples/overlapping_graphemes.rs +++ b/examples/overlapping_graphemes.rs @@ -43,7 +43,7 @@ fn draw(f: &mut Frame) { Pos::new(0, 13), "scientist emoji as a woman and a microscope: 👩‍🔬", ); - for i in 0..(f.width(scientist) + 4) { + for i in 0..(f.widthdb().width(scientist) + 4) { f.write(Pos::new(2, 15 + i as i32), (scientist, under)); f.write(Pos::new(i as i32, 15 + i as i32), ("x", over)); } diff --git a/src/frame.rs b/src/frame.rs index 38fab3f..235c7dd 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -45,24 +45,8 @@ impl Frame { self.set_cursor(None); } - /// Determine the width of a grapheme. - /// - /// If the grapheme is a tab, the column is used to determine its width. - /// - /// If the width has not been measured yet, it is estimated using the - /// Unicode Standard Annex #11. - pub fn grapheme_width(&mut self, grapheme: &str, col: usize) -> u8 { - self.widthdb.grapheme_width(grapheme, col) - } - - /// Determine the width of a string based on its graphemes. - /// - /// If a grapheme is a tab, its column is used to determine its width. - /// - /// If the width of a grapheme has not been measured yet, it is estimated - /// using the Unicode Standard Annex #11. - pub fn width(&mut self, s: &str) -> usize { - self.widthdb.width(s) + pub fn widthdb(&mut self) -> &mut WidthDB { + &mut self.widthdb } pub fn wrap(&mut self, text: &str, width: usize) -> Vec { diff --git a/src/lib.rs b/src/lib.rs index d73aeed..0afea1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,5 +2,5 @@ mod buffer; pub mod frame; pub mod styled; pub mod terminal; -mod widthdb; +pub mod widthdb; mod wrap; diff --git a/src/widthdb.rs b/src/widthdb.rs index 00a5995..d926c26 100644 --- a/src/widthdb.rs +++ b/src/widthdb.rs @@ -8,22 +8,24 @@ use crossterm::QueueableCommand; use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; +use crate::wrap; + /// Measures and stores the with (in terminal coordinates) of graphemes. #[derive(Debug)] pub struct WidthDB { - pub active: bool, + pub(crate) active: bool, + pub(crate) tab_width: u8, known: HashMap, requested: HashSet, - pub(crate) tab_width: u8, } impl Default for WidthDB { fn default() -> Self { Self { active: false, + tab_width: 8, known: Default::default(), requested: Default::default(), - tab_width: 8, } } } @@ -70,9 +72,13 @@ impl WidthDB { total } + pub fn wrap(&mut self, text: &str, width: usize) -> Vec { + wrap::wrap(self, text, width) + } + /// Whether any new graphemes have been seen since the last time /// [`Self::measure_widths`] was called. - pub fn measuring_required(&self) -> bool { + pub(crate) fn measuring_required(&self) -> bool { self.active && !self.requested.is_empty() } @@ -82,7 +88,7 @@ impl WidthDB { /// This function measures the actual width of graphemes by writing them to /// the terminal. After it finishes, the terminal's contents should be /// assumed to be garbage and a full redraw should be performed. - pub fn measure_widths(&mut self, out: &mut impl Write) -> io::Result<()> { + pub(crate) fn measure_widths(&mut self, out: &mut impl Write) -> io::Result<()> { if !self.active { return Ok(()); }