diff --git a/src/alarm.rs b/src/alarm.rs index 363ddc4..f592a36 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -109,7 +109,7 @@ impl AlarmRoster { if let Some(i) = input.find(DELIMITER) { label = input[(i + 1)..].to_string(); // Truncate label. - unicode_truncate(&mut label, LABEL_SIZE_LIMIT); + grapheme_truncate(&mut label, LABEL_SIZE_LIMIT, '…'); time_str = &input[..i].trim(); } else { label = input.clone(); @@ -210,7 +210,7 @@ impl AlarmRoster { let mut col = layout.roster.col + 3 - + UnicodeWidthStr::width(&alarm.label[..]) as u16; + + UnicodeWidthStr::width(alarm.label.as_str()) as u16; let mut line = layout.roster.line + index as u16; // Compensate for "hidden" items in the alarm roster. @@ -318,7 +318,7 @@ impl AlarmRoster { pub fn width(&self) -> u16 { let mut width: u16 = 0; for alarm in &self.list { - let length = UnicodeWidthStr::width(&alarm.label[..]) as u16; + let length = UnicodeWidthStr::width(alarm.label.as_str()) as u16; if length > width { width = length }; } // Actual width is 4 columns wider if it's not 0. diff --git a/src/buffer.rs b/src/buffer.rs index 0da0ad6..4ab3a03 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -4,6 +4,7 @@ use std::io::Write; use termion::{clear, cursor, color}; use termion::raw::RawTerminal; use crate::layout::Layout; +use unicode_width::UnicodeWidthStr; use unicode_segmentation::UnicodeSegmentation; const PROMPT: &str = "Add alarm: "; @@ -74,7 +75,7 @@ impl Buffer { // Draw input buffer. pub fn draw( - &self, + &mut self, stdout: &mut RawTerminal, layout: &mut Layout, ) -> Result<(), std::io::Error> @@ -93,7 +94,22 @@ impl Buffer { return Ok(()); } - if !self.content.is_empty() { + if self.content.is_empty() { + // Clear buffer display. + write!(stdout, + "{}{}{}", + cursor::Goto(layout.buffer.col, layout.buffer.line), + clear::CurrentLine, + cursor::Hide)?; + } else { + // Check if buffer exceeds limits. + while UnicodeWidthStr::width(self.content.as_str()) + + UnicodeWidthStr::width(PROMPT) + > layout.width as usize + { + self.content.pop(); + } + write!(stdout, "{}{}{}{}{}", cursor::Goto(layout.buffer.col, layout.buffer.line), @@ -101,13 +117,6 @@ impl Buffer { PROMPT, cursor::Show, &self.content)?; - } else { - // Clear buffer display. - write!(stdout, - "{}{}{}", - cursor::Goto(layout.buffer.col, layout.buffer.line), - clear::CurrentLine, - cursor::Hide)?; } Ok(()) } diff --git a/src/utils.rs b/src/utils.rs index 66416ed..354f934 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,11 @@ extern crate unicode_segmentation; use unicode_segmentation::UnicodeSegmentation; -pub fn unicode_truncate(input: &mut String, limit: usize) { +pub fn grapheme_truncate(input: &mut String, limit: usize, ellipse: char) { match UnicodeSegmentation::grapheme_indices(input.as_str(), true).nth(limit) { Some((i, _)) => { input.truncate(i); - input.push('…'); + input.push(ellipse); }, None => (), }