Limit input buffer length.

This commit is contained in:
shy 2021-04-16 16:01:43 +02:00
parent 59612a516d
commit d236c791a1
3 changed files with 23 additions and 14 deletions

View file

@ -109,7 +109,7 @@ impl AlarmRoster {
if let Some(i) = input.find(DELIMITER) { if let Some(i) = input.find(DELIMITER) {
label = input[(i + 1)..].to_string(); label = input[(i + 1)..].to_string();
// Truncate label. // Truncate label.
unicode_truncate(&mut label, LABEL_SIZE_LIMIT); grapheme_truncate(&mut label, LABEL_SIZE_LIMIT, '…');
time_str = &input[..i].trim(); time_str = &input[..i].trim();
} else { } else {
label = input.clone(); label = input.clone();
@ -210,7 +210,7 @@ impl AlarmRoster {
let mut col = let mut col =
layout.roster.col layout.roster.col
+ 3 + 3
+ UnicodeWidthStr::width(&alarm.label[..]) as u16; + UnicodeWidthStr::width(alarm.label.as_str()) as u16;
let mut line = layout.roster.line + index as u16; let mut line = layout.roster.line + index as u16;
// Compensate for "hidden" items in the alarm roster. // Compensate for "hidden" items in the alarm roster.
@ -318,7 +318,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 = UnicodeWidthStr::width(&alarm.label[..]) as u16; let length = UnicodeWidthStr::width(alarm.label.as_str()) as u16;
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

@ -4,6 +4,7 @@ use std::io::Write;
use termion::{clear, cursor, color}; use termion::{clear, cursor, color};
use termion::raw::RawTerminal; use termion::raw::RawTerminal;
use crate::layout::Layout; use crate::layout::Layout;
use unicode_width::UnicodeWidthStr;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
const PROMPT: &str = "Add alarm: "; const PROMPT: &str = "Add alarm: ";
@ -74,7 +75,7 @@ impl Buffer {
// Draw input buffer. // Draw input buffer.
pub fn draw<W: Write>( pub fn draw<W: Write>(
&self, &mut self,
stdout: &mut RawTerminal<W>, stdout: &mut RawTerminal<W>,
layout: &mut Layout, layout: &mut Layout,
) -> Result<(), std::io::Error> ) -> Result<(), std::io::Error>
@ -93,7 +94,22 @@ impl Buffer {
return Ok(()); 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, write!(stdout,
"{}{}{}{}{}", "{}{}{}{}{}",
cursor::Goto(layout.buffer.col, layout.buffer.line), cursor::Goto(layout.buffer.col, layout.buffer.line),
@ -101,13 +117,6 @@ impl Buffer {
PROMPT, PROMPT,
cursor::Show, cursor::Show,
&self.content)?; &self.content)?;
} else {
// Clear buffer display.
write!(stdout,
"{}{}{}",
cursor::Goto(layout.buffer.col, layout.buffer.line),
clear::CurrentLine,
cursor::Hide)?;
} }
Ok(()) Ok(())
} }

View file

@ -1,11 +1,11 @@
extern crate unicode_segmentation; extern crate unicode_segmentation;
use unicode_segmentation::UnicodeSegmentation; 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) { match UnicodeSegmentation::grapheme_indices(input.as_str(), true).nth(limit) {
Some((i, _)) => { Some((i, _)) => {
input.truncate(i); input.truncate(i);
input.push('…'); input.push(ellipse);
}, },
None => (), None => (),
} }