kitchentimer/src/buffer.rs

136 lines
3.3 KiB
Rust
Raw Normal View History

2021-04-11 16:38:19 +02:00
extern crate unicode_segmentation;
use std::io::Write;
use termion::{clear, cursor, color};
use termion::raw::RawTerminal;
use crate::layout::Layout;
2021-04-11 16:38:19 +02:00
use unicode_segmentation::UnicodeSegmentation;
2021-04-11 14:06:00 +02:00
const PROMPT: &str = "Add alarm: ";
2021-04-11 16:38:19 +02:00
// Input buffer.
pub struct Buffer {
content: String,
// Used for error messages.
message: Option<&'static str>,
pub altered: bool,
}
impl Buffer {
pub fn new() -> Buffer {
Buffer {
content: String::new(),
altered: false,
message: None,
}
}
2021-04-11 16:38:19 +02:00
// Return reference to buffer content.
pub fn read(&mut self) -> &String {
self.altered = false;
&self.content
}
2021-04-11 16:38:19 +02:00
// Append char to buffer.
pub fn push(&mut self, value: char) {
self.altered = true;
2021-04-11 16:38:19 +02:00
// Reset error message.
self.message = None;
self.content.push(value);
}
2021-04-11 16:38:19 +02:00
// Remove last char. Return true if a char was removed.
pub fn strip_char(&mut self) -> bool {
// Reset error message.
self.message = None;
if self.content.pop().is_some() {
self.altered = true;
true
} else {
false
}
}
// Remove last word. Return true if a word was removed.
pub fn strip_word(&mut self) -> bool {
// Reset error message.
self.message = None;
2021-04-11 16:38:19 +02:00
let iter = UnicodeSegmentation::split_word_bound_indices(
self.content.as_str().trim_end());
if let Some((index, _)) = iter.last() {
self.content.truncate(index);
self.altered = true;
true
} else {
false
}
}
pub fn is_empty(&self) -> bool {
self.content.is_empty()
}
// Clear input.
pub fn clear(&mut self) {
self.altered = true;
self.content.clear();
}
// Clear input and message.
pub fn reset(&mut self) {
self.message = None;
self.clear();
}
// Draw input buffer.
pub fn draw<W: Write>(
&self,
stdout: &mut RawTerminal<W>,
layout: &mut Layout,
) -> Result<(), std::io::Error>
{
2021-04-11 16:38:19 +02:00
// Write error message if present and return.
if let Some(msg) = self.message {
write!(stdout,
2021-04-11 18:09:26 +02:00
"{}{}{}{}{}{}{}",
cursor::Hide,
2021-04-11 18:09:26 +02:00
cursor::Goto( layout.buffer.col, layout.buffer.line),
clear::CurrentLine,
PROMPT,
color::Fg(color::LightRed),
&msg,
color::Fg(color::Reset))?;
return Ok(());
}
if !self.content.is_empty() {
write!(stdout,
2021-04-11 14:06:00 +02:00
"{}{}{}{}{}",
cursor::Goto(layout.buffer.col, layout.buffer.line),
2021-04-11 18:09:26 +02:00
clear::CurrentLine,
2021-04-11 14:06:00 +02:00
PROMPT,
cursor::Show,
&self.content)?;
} else {
// Clear buffer display.
write!(stdout,
"{}{}{}",
cursor::Goto(layout.buffer.col, layout.buffer.line),
clear::CurrentLine,
cursor::Hide)?;
}
Ok(())
}
// Draw error message at input buffer position.
pub fn message(
&mut self,
msg: &'static str,
) {
self.message = Some(msg);
self.altered = true;
}
}