Limit label length.

This commit is contained in:
shy 2021-04-09 19:15:31 +02:00
parent a42e1130a1
commit d264d8540d
3 changed files with 18 additions and 6 deletions

View file

@ -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.

View file

@ -8,11 +8,18 @@ pub struct Config {
pub command: Option<Vec<String>>,
}
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] = [

View file

@ -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<W: Write>(
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,