Refactored buffer module.

This commit is contained in:
shy 2021-04-12 20:01:14 +02:00
parent 55e9003f8c
commit 8112ecadb2
2 changed files with 11 additions and 21 deletions

View file

@ -14,27 +14,25 @@ pub struct Buffer {
content: String, content: String,
// Used for error messages. // Used for error messages.
message: Option<&'static str>, message: Option<&'static str>,
pub altered: bool, pub visible: bool,
} }
impl Buffer { impl Buffer {
pub fn new() -> Buffer { pub fn new() -> Buffer {
Buffer { Buffer {
content: String::new(), content: String::new(),
altered: false,
message: None, message: None,
visible: false,
} }
} }
// Return reference to buffer content. // Return reference to buffer content.
pub fn read(&mut self) -> &String { pub fn read(&mut self) -> &String {
self.altered = false;
&self.content &self.content
} }
// Append char to buffer. // Append char to buffer.
pub fn push(&mut self, value: char) { pub fn push(&mut self, value: char) {
self.altered = true;
// Reset error message. // Reset error message.
self.message = None; self.message = None;
self.content.push(value); self.content.push(value);
@ -44,9 +42,7 @@ impl Buffer {
pub fn strip_char(&mut self) { pub fn strip_char(&mut self) {
// Reset error message. // Reset error message.
self.message = None; self.message = None;
if self.content.pop().is_some() { self.content.pop();
self.altered = true;
}
} }
// Remove last word. // Remove last word.
@ -58,7 +54,6 @@ impl Buffer {
if let Some((index, _)) = iter.last() { if let Some((index, _)) = iter.last() {
self.content.truncate(index); self.content.truncate(index);
self.altered = true;
} }
} }
@ -68,7 +63,6 @@ impl Buffer {
// Clear input. // Clear input.
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.altered = true;
self.content.clear(); self.content.clear();
} }
@ -124,6 +118,5 @@ impl Buffer {
msg: &'static str, msg: &'static str,
) { ) {
self.message = Some(msg); self.message = Some(msg);
self.altered = true;
} }
} }

View file

@ -43,9 +43,6 @@ pub fn run(
let stdout = std::io::stdout(); let stdout = std::io::stdout();
let mut stdout = stdout.lock().into_raw_mode()?; let mut stdout = stdout.lock().into_raw_mode()?;
// Are we in insert mode?
let mut insert_mode = false;
// Register signals. // Register signals.
let mut signals = Signals::new(&[ let mut signals = Signals::new(&[
SIGTSTP, SIGTSTP,
@ -147,7 +144,7 @@ pub fn run(
style::Faint, style::Faint,
// Switch menu bars. Use a compressed version or none at // Switch menu bars. Use a compressed version or none at
// all if necessary. // all if necessary.
match insert_mode { match buffer.visible {
true if layout.can_hold(MENUBAR_INS) => MENUBAR_INS, true if layout.can_hold(MENUBAR_INS) => MENUBAR_INS,
false if layout.can_hold(MENUBAR) => MENUBAR, false if layout.can_hold(MENUBAR) => MENUBAR,
false if layout.can_hold(MENUBAR_SHORT) => MENUBAR_SHORT, false if layout.can_hold(MENUBAR_SHORT) => MENUBAR_SHORT,
@ -200,7 +197,7 @@ pub fn run(
} }
// Update buffer whenever the cursor should be visible. // Update buffer whenever the cursor should be visible.
if insert_mode || buffer.altered { if buffer.visible {
buffer.draw(&mut stdout, &mut layout)?; buffer.draw(&mut stdout, &mut layout)?;
stdout.flush()?; stdout.flush()?;
} }
@ -219,21 +216,21 @@ pub fn run(
layout.set_roster_width(alarm_roster.width()); layout.set_roster_width(alarm_roster.width());
} }
buffer.clear(); buffer.clear();
insert_mode = false; buffer.visible = false;
layout.force_redraw = true; layout.force_redraw = true;
} }
}, },
// Escape and ^U clear input buffer. // Escape and ^U clear input buffer.
Key::Esc | Key::Ctrl('u') => { Key::Esc | Key::Ctrl('u') => {
buffer.reset(); buffer.reset();
insert_mode = false; buffer.visible = false;
layout.force_redraw = true; layout.force_redraw = true;
}, },
// ^W removes last word. // ^W removes last word.
Key::Ctrl('w') => { Key::Ctrl('w') => {
buffer.strip_word(); buffer.strip_word();
if buffer.is_empty() { if buffer.is_empty() {
insert_mode = false; buffer.visible = false;
layout.force_redraw = true; layout.force_redraw = true;
} }
}, },
@ -242,12 +239,12 @@ pub fn run(
// Delete last char in buffer. // Delete last char in buffer.
buffer.strip_char(); buffer.strip_char();
if buffer.is_empty() { if buffer.is_empty() {
insert_mode = false; buffer.visible = false;
layout.force_redraw = true; layout.force_redraw = true;
} }
}, },
// Forward every char if in insert mode. // Forward every char if in insert mode.
Key::Char(c) if insert_mode => { Key::Char(c) if buffer.visible => {
buffer.push(c); buffer.push(c);
}, },
// Reset clock on 'r'. // Reset clock on 'r'.
@ -294,7 +291,7 @@ pub fn run(
Key::Char(c) => { Key::Char(c) => {
if c.is_ascii_digit() { if c.is_ascii_digit() {
buffer.push(c); buffer.push(c);
insert_mode = true; buffer.visible = true;
layout.force_redraw = true; layout.force_redraw = true;
} else if !buffer.is_empty() && c == ':' { } else if !buffer.is_empty() && c == ':' {
buffer.push(':'); buffer.push(':');