Expose Widthdb directly via Frame
This commit is contained in:
parent
24fd0050fb
commit
f258c84094
4 changed files with 15 additions and 25 deletions
|
|
@ -43,7 +43,7 @@ fn draw(f: &mut Frame) {
|
||||||
Pos::new(0, 13),
|
Pos::new(0, 13),
|
||||||
"scientist emoji as a woman and a microscope: 👩🔬",
|
"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(2, 15 + i as i32), (scientist, under));
|
||||||
f.write(Pos::new(i as i32, 15 + i as i32), ("x", over));
|
f.write(Pos::new(i as i32, 15 + i as i32), ("x", over));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
src/frame.rs
20
src/frame.rs
|
|
@ -45,24 +45,8 @@ impl Frame {
|
||||||
self.set_cursor(None);
|
self.set_cursor(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine the width of a grapheme.
|
pub fn widthdb(&mut self) -> &mut WidthDB {
|
||||||
///
|
&mut self.widthdb
|
||||||
/// 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 wrap(&mut self, text: &str, width: usize) -> Vec<usize> {
|
pub fn wrap(&mut self, text: &str, width: usize) -> Vec<usize> {
|
||||||
|
|
|
||||||
|
|
@ -2,5 +2,5 @@ mod buffer;
|
||||||
pub mod frame;
|
pub mod frame;
|
||||||
pub mod styled;
|
pub mod styled;
|
||||||
pub mod terminal;
|
pub mod terminal;
|
||||||
mod widthdb;
|
pub mod widthdb;
|
||||||
mod wrap;
|
mod wrap;
|
||||||
|
|
|
||||||
|
|
@ -8,22 +8,24 @@ use crossterm::QueueableCommand;
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
|
use crate::wrap;
|
||||||
|
|
||||||
/// Measures and stores the with (in terminal coordinates) of graphemes.
|
/// Measures and stores the with (in terminal coordinates) of graphemes.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct WidthDB {
|
pub struct WidthDB {
|
||||||
pub active: bool,
|
pub(crate) active: bool,
|
||||||
|
pub(crate) tab_width: u8,
|
||||||
known: HashMap<String, u8>,
|
known: HashMap<String, u8>,
|
||||||
requested: HashSet<String>,
|
requested: HashSet<String>,
|
||||||
pub(crate) tab_width: u8,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for WidthDB {
|
impl Default for WidthDB {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
active: false,
|
active: false,
|
||||||
|
tab_width: 8,
|
||||||
known: Default::default(),
|
known: Default::default(),
|
||||||
requested: Default::default(),
|
requested: Default::default(),
|
||||||
tab_width: 8,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -70,9 +72,13 @@ impl WidthDB {
|
||||||
total
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn wrap(&mut self, text: &str, width: usize) -> Vec<usize> {
|
||||||
|
wrap::wrap(self, text, width)
|
||||||
|
}
|
||||||
|
|
||||||
/// Whether any new graphemes have been seen since the last time
|
/// Whether any new graphemes have been seen since the last time
|
||||||
/// [`Self::measure_widths`] was called.
|
/// [`Self::measure_widths`] was called.
|
||||||
pub fn measuring_required(&self) -> bool {
|
pub(crate) fn measuring_required(&self) -> bool {
|
||||||
self.active && !self.requested.is_empty()
|
self.active && !self.requested.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -82,7 +88,7 @@ impl WidthDB {
|
||||||
/// This function measures the actual width of graphemes by writing them to
|
/// This function measures the actual width of graphemes by writing them to
|
||||||
/// the terminal. After it finishes, the terminal's contents should be
|
/// the terminal. After it finishes, the terminal's contents should be
|
||||||
/// assumed to be garbage and a full redraw should be performed.
|
/// 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 {
|
if !self.active {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue