Use crate unicode-width to determin width if chars.

This commit is contained in:
shy 2021-04-16 06:45:45 +02:00
parent 2a336236e1
commit e33cc4eb93
4 changed files with 12 additions and 7 deletions

7
Cargo.lock generated
View file

@ -13,6 +13,7 @@ dependencies = [
"signal-hook", "signal-hook",
"termion", "termion",
"unicode-segmentation", "unicode-segmentation",
"unicode-width",
] ]
[[package]] [[package]]
@ -81,3 +82,9 @@ name = "unicode-segmentation"
version = "1.7.1" version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"

View file

@ -10,3 +10,4 @@ edition = "2018"
termion = "1.5.6" termion = "1.5.6"
signal-hook = "0.3.8" signal-hook = "0.3.8"
unicode-segmentation = "1.7.1" unicode-segmentation = "1.7.1"
unicode-width = "0.1.8"

View file

@ -2,6 +2,7 @@ use std::io::Write;
use std::process::{Command, Stdio, Child}; use std::process::{Command, Stdio, Child};
use termion::{color, cursor, style}; use termion::{color, cursor, style};
use termion::raw::RawTerminal; use termion::raw::RawTerminal;
use unicode_width::UnicodeWidthStr;
use crate::Config; use crate::Config;
use crate::clock::Clock; use crate::clock::Clock;
use crate::layout::{Layout, Position}; use crate::layout::{Layout, Position};
@ -208,7 +209,7 @@ impl AlarmRoster {
let mut col = let mut col =
layout.roster.col layout.roster.col
+ 3 + 3
+ unicode_length(&alarm.label); + UnicodeWidthStr::width(&alarm.label[..]) as u16;
let mut line = layout.roster.line + index as u16; let mut line = layout.roster.line + index as u16;
// Compensate for "hidden" items in the alarm roster. // Compensate for "hidden" items in the alarm roster.
@ -316,7 +317,7 @@ impl AlarmRoster {
pub fn width(&self) -> u16 { pub fn width(&self) -> u16 {
let mut width: u16 = 0; let mut width: u16 = 0;
for alarm in &self.list { for alarm in &self.list {
let length = unicode_length(&alarm.label); let length = UnicodeWidthStr::width(&alarm.label[..]) as u16;
if length > width { width = length }; if length > width { width = length };
} }
// Actual width is 4 columns wider if it's not 0. // 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) { pub fn time_travel(&mut self, time: u32) {
for alarm in self.list.iter_mut() { for alarm in self.list.iter_mut() {
alarm.exceeded = alarm.time <= time; alarm.exceeded = alarm.time <= time;

View file

@ -1,11 +1,6 @@
extern crate unicode_segmentation; extern crate unicode_segmentation;
use unicode_segmentation::UnicodeSegmentation; 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) { pub fn unicode_truncate(input: &mut String, limit: usize) {
match UnicodeSegmentation::grapheme_indices(input.as_str(), true).nth(limit) { match UnicodeSegmentation::grapheme_indices(input.as_str(), true).nth(limit) {
Some((i, _)) => { Some((i, _)) => {