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::{color, cursor, style};
use termion::raw::RawTerminal; use termion::raw::RawTerminal;
use crate::{Clock, Layout, Position}; 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 { pub struct Countdown {
@ -86,6 +87,8 @@ impl AlarmRoster {
if let Some(i) = input.find('/') { if let Some(i) = input.find('/') {
label = input[(i + 1)..].trim().to_string(); label = input[(i + 1)..].trim().to_string();
// Truncate label.
unicode_truncate(&mut label, LABEL_SIZE_LIMIT);
time_str = &input[..i].trim(); time_str = &input[..i].trim();
} else { } else {
label = input.clone(); label = input.clone();
@ -189,7 +192,7 @@ impl AlarmRoster {
let mut col = let mut col =
layout.roster.col layout.roster.col
+ 3 + 3
+ str_length(&alarm.label); + unicode_length(&alarm.label);
let mut line = layout.roster.line + index; let mut line = layout.roster.line + index;
// Compensate for "hidden" items in the alarm roster. // Compensate for "hidden" items in the alarm roster.
@ -266,7 +269,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 = str_length(&alarm.label); let length = unicode_length(&alarm.label);
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.

View file

@ -8,11 +8,18 @@ pub struct Config {
pub command: Option<Vec<String>>, 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(); let length = UnicodeSegmentation::graphemes(input, true).count();
length as u16 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] = [ pub const COLOR: [&dyn color::Color; 6] = [
&color::Cyan, &color::Cyan,
&color::Magenta, &color::Magenta,
@ -21,6 +28,8 @@ pub const COLOR: [&dyn color::Color; 6] = [
&color::Blue, &color::Blue,
&color::Red, &color::Red,
]; ];
// Maximum length of labels.
pub const LABEL_SIZE_LIMIT: usize = 48;
pub const DIGIT_HEIGHT: u16 = 5; pub const DIGIT_HEIGHT: u16 = 5;
pub const DIGIT_WIDTH: u16 = 5; pub const DIGIT_WIDTH: u16 = 5;
pub const DIGITS: [[&str; DIGIT_HEIGHT as usize]; 10] = [ pub const DIGITS: [[&str; DIGIT_HEIGHT as usize]; 10] = [

View file

@ -20,7 +20,7 @@ use termion::input::TermRead;
use clock::Clock; use clock::Clock;
use alarm::{Countdown, AlarmRoster, exec_command}; use alarm::{Countdown, AlarmRoster, exec_command};
use layout::{Layout, Position}; use layout::{Layout, Position};
use common::{Config, str_length}; use common::{Config, unicode_length};
const NAME: &str = env!("CARGO_PKG_NAME"); const NAME: &str = env!("CARGO_PKG_NAME");
@ -542,7 +542,7 @@ fn draw_buffer<W: Write>(
cursor::Show, cursor::Show,
buffer) buffer)
.unwrap(); .unwrap();
layout.cursor.col = layout.buffer.col + 11 + str_length(buffer); layout.cursor.col = layout.buffer.col + 11 + unicode_length(buffer);
} else { } else {
// Clear buffer display. // Clear buffer display.
write!(stdout, write!(stdout,