Make width measuring optional and disabled by default
This commit is contained in:
parent
33264b4aec
commit
333cf74fba
5 changed files with 47 additions and 27 deletions
|
|
@ -11,6 +11,7 @@ use unicode_width::UnicodeWidthStr;
|
|||
/// Measures and stores the with (in terminal coordinates) of graphemes.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct WidthDB {
|
||||
pub active: bool,
|
||||
known: HashMap<String, u8>,
|
||||
requested: HashSet<String>,
|
||||
}
|
||||
|
|
@ -22,6 +23,9 @@ impl WidthDB {
|
|||
/// Unicode Standard Annex #11.
|
||||
pub fn grapheme_width(&mut self, grapheme: &str) -> u8 {
|
||||
assert_eq!(Some(grapheme), grapheme.graphemes(true).next());
|
||||
if !self.active {
|
||||
return grapheme.width() as u8;
|
||||
}
|
||||
if let Some(width) = self.known.get(grapheme) {
|
||||
*width
|
||||
} else {
|
||||
|
|
@ -35,6 +39,9 @@ impl WidthDB {
|
|||
/// 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 {
|
||||
if !self.active {
|
||||
return s.width();
|
||||
}
|
||||
let mut total: usize = 0;
|
||||
for grapheme in s.graphemes(true) {
|
||||
total += if let Some(width) = self.known.get(grapheme) {
|
||||
|
|
@ -50,7 +57,7 @@ impl WidthDB {
|
|||
/// Whether any new graphemes have been seen since the last time
|
||||
/// [`Self::measure_widths`] was called.
|
||||
pub fn measuring_required(&self) -> bool {
|
||||
!self.requested.is_empty()
|
||||
self.active && !self.requested.is_empty()
|
||||
}
|
||||
|
||||
/// Measure the width of all new graphemes that have been seen since the
|
||||
|
|
@ -60,6 +67,9 @@ impl WidthDB {
|
|||
/// 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<()> {
|
||||
if !self.active {
|
||||
return Ok(());
|
||||
}
|
||||
for grapheme in self.requested.drain() {
|
||||
out.queue(Clear(ClearType::All))?
|
||||
.queue(MoveTo(0, 0))?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue