From d264d8540d7c17068bb4ecb055fe3a37067003e3 Mon Sep 17 00:00:00 2001 From: shy Date: Fri, 9 Apr 2021 19:15:31 +0200 Subject: [PATCH] Limit label length. --- src/alarm.rs | 9 ++++++--- src/common.rs | 11 ++++++++++- src/main.rs | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/alarm.rs b/src/alarm.rs index 8575b34..4e32821 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -3,7 +3,8 @@ use std::process::{Command, Stdio, Child}; use termion::{color, cursor, style}; use termion::raw::RawTerminal; use crate::{Clock, Layout, Position}; -use crate::common::{COLOR, Config, str_length}; +use crate::common::{COLOR, LABEL_SIZE_LIMIT}; +use crate::common::{Config, unicode_length, unicode_truncate}; pub struct Countdown { @@ -86,6 +87,8 @@ impl AlarmRoster { if let Some(i) = input.find('/') { label = input[(i + 1)..].trim().to_string(); + // Truncate label. + unicode_truncate(&mut label, LABEL_SIZE_LIMIT); time_str = &input[..i].trim(); } else { label = input.clone(); @@ -189,7 +192,7 @@ impl AlarmRoster { let mut col = layout.roster.col + 3 - + str_length(&alarm.label); + + unicode_length(&alarm.label); let mut line = layout.roster.line + index; // Compensate for "hidden" items in the alarm roster. @@ -266,7 +269,7 @@ impl AlarmRoster { pub fn width(&self) -> u16 { let mut width: u16 = 0; for alarm in &self.list { - let length = str_length(&alarm.label); + let length = unicode_length(&alarm.label); if length > width { width = length }; } // Actual width is 4 columns wider if it's not 0. diff --git a/src/common.rs b/src/common.rs index 9ef7777..732bb30 100644 --- a/src/common.rs +++ b/src/common.rs @@ -8,11 +8,18 @@ pub struct Config { pub command: Option>, } -pub fn str_length(input: &str) -> u16 { +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, _)) => input.truncate(i), + None => (), + } +} + pub const COLOR: [&dyn color::Color; 6] = [ &color::Cyan, &color::Magenta, @@ -21,6 +28,8 @@ pub const COLOR: [&dyn color::Color; 6] = [ &color::Blue, &color::Red, ]; +// Maximum length of labels. +pub const LABEL_SIZE_LIMIT: usize = 48; pub const DIGIT_HEIGHT: u16 = 5; pub const DIGIT_WIDTH: u16 = 5; pub const DIGITS: [[&str; DIGIT_HEIGHT as usize]; 10] = [ diff --git a/src/main.rs b/src/main.rs index f9f0053..56ddb68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use termion::input::TermRead; use clock::Clock; use alarm::{Countdown, AlarmRoster, exec_command}; use layout::{Layout, Position}; -use common::{Config, str_length}; +use common::{Config, unicode_length}; const NAME: &str = env!("CARGO_PKG_NAME"); @@ -542,7 +542,7 @@ fn draw_buffer( cursor::Show, buffer) .unwrap(); - layout.cursor.col = layout.buffer.col + 11 + str_length(buffer); + layout.cursor.col = layout.buffer.col + 11 + unicode_length(buffer); } else { // Clear buffer display. write!(stdout,