Limit input buffer length.
This commit is contained in:
parent
59612a516d
commit
d236c791a1
3 changed files with 23 additions and 14 deletions
|
@ -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.
|
||||
|
|
|
@ -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<W: Write>(
|
||||
&self,
|
||||
&mut self,
|
||||
stdout: &mut RawTerminal<W>,
|
||||
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(())
|
||||
}
|
||||
|
|
|
@ -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 => (),
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue