From e33cc4eb933182656d149199cf4618daf7b8a35a Mon Sep 17 00:00:00 2001 From: shy Date: Fri, 16 Apr 2021 06:45:45 +0200 Subject: [PATCH] Use crate unicode-width to determin width if chars. --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/alarm.rs | 6 ++++-- src/utils.rs | 5 ----- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c43208..ad2f6c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,7 @@ dependencies = [ "signal-hook", "termion", "unicode-segmentation", + "unicode-width", ] [[package]] @@ -81,3 +82,9 @@ name = "unicode-segmentation" version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" diff --git a/Cargo.toml b/Cargo.toml index 4882fb8..c062cf6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ edition = "2018" termion = "1.5.6" signal-hook = "0.3.8" unicode-segmentation = "1.7.1" +unicode-width = "0.1.8" diff --git a/src/alarm.rs b/src/alarm.rs index e310575..6dde088 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -2,6 +2,7 @@ use std::io::Write; use std::process::{Command, Stdio, Child}; use termion::{color, cursor, style}; use termion::raw::RawTerminal; +use unicode_width::UnicodeWidthStr; use crate::Config; use crate::clock::Clock; use crate::layout::{Layout, Position}; @@ -208,7 +209,7 @@ impl AlarmRoster { let mut col = layout.roster.col + 3 - + unicode_length(&alarm.label); + + UnicodeWidthStr::width(&alarm.label[..]) as u16; let mut line = layout.roster.line + index as u16; // Compensate for "hidden" items in the alarm roster. @@ -316,7 +317,7 @@ impl AlarmRoster { pub fn width(&self) -> u16 { let mut width: u16 = 0; for alarm in &self.list { - let length = unicode_length(&alarm.label); + let length = UnicodeWidthStr::width(&alarm.label[..]) as u16; if length > width { width = length }; } // Actual width is 4 columns wider if it's not 0. @@ -330,6 +331,7 @@ impl AlarmRoster { } } + // Call when time jumps backwards. pub fn time_travel(&mut self, time: u32) { for alarm in self.list.iter_mut() { alarm.exceeded = alarm.time <= time; diff --git a/src/utils.rs b/src/utils.rs index 0637108..66416ed 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,6 @@ extern crate unicode_segmentation; use unicode_segmentation::UnicodeSegmentation; -pub fn unicode_length(input: &str) -> u16 { - let length = UnicodeSegmentation::graphemes(input, true).count(); - length as u16 -} - pub fn unicode_truncate(input: &mut String, limit: usize) { match UnicodeSegmentation::grapheme_indices(input.as_str(), true).nth(limit) { Some((i, _)) => {