From 8112ecadb22a1b7f088d18e5ced67e4e61df8203 Mon Sep 17 00:00:00 2001 From: shy Date: Mon, 12 Apr 2021 20:01:14 +0200 Subject: [PATCH] Refactored buffer module. --- src/buffer.rs | 13 +++---------- src/lib.rs | 19 ++++++++----------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index a6a409a..0da0ad6 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -14,27 +14,25 @@ pub struct Buffer { content: String, // Used for error messages. message: Option<&'static str>, - pub altered: bool, + pub visible: bool, } impl Buffer { pub fn new() -> Buffer { Buffer { content: String::new(), - altered: false, message: None, + visible: false, } } // Return reference to buffer content. pub fn read(&mut self) -> &String { - self.altered = false; &self.content } // Append char to buffer. pub fn push(&mut self, value: char) { - self.altered = true; // Reset error message. self.message = None; self.content.push(value); @@ -44,9 +42,7 @@ impl Buffer { pub fn strip_char(&mut self) { // Reset error message. self.message = None; - if self.content.pop().is_some() { - self.altered = true; - } + self.content.pop(); } // Remove last word. @@ -58,7 +54,6 @@ impl Buffer { if let Some((index, _)) = iter.last() { self.content.truncate(index); - self.altered = true; } } @@ -68,7 +63,6 @@ impl Buffer { // Clear input. pub fn clear(&mut self) { - self.altered = true; self.content.clear(); } @@ -124,6 +118,5 @@ impl Buffer { msg: &'static str, ) { self.message = Some(msg); - self.altered = true; } } diff --git a/src/lib.rs b/src/lib.rs index 86f47e1..a03f24e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,9 +43,6 @@ pub fn run( let stdout = std::io::stdout(); let mut stdout = stdout.lock().into_raw_mode()?; - // Are we in insert mode? - let mut insert_mode = false; - // Register signals. let mut signals = Signals::new(&[ SIGTSTP, @@ -147,7 +144,7 @@ pub fn run( style::Faint, // Switch menu bars. Use a compressed version or none at // all if necessary. - match insert_mode { + match buffer.visible { true if layout.can_hold(MENUBAR_INS) => MENUBAR_INS, false if layout.can_hold(MENUBAR) => MENUBAR, false if layout.can_hold(MENUBAR_SHORT) => MENUBAR_SHORT, @@ -200,7 +197,7 @@ pub fn run( } // Update buffer whenever the cursor should be visible. - if insert_mode || buffer.altered { + if buffer.visible { buffer.draw(&mut stdout, &mut layout)?; stdout.flush()?; } @@ -219,21 +216,21 @@ pub fn run( layout.set_roster_width(alarm_roster.width()); } buffer.clear(); - insert_mode = false; + buffer.visible = false; layout.force_redraw = true; } }, // Escape and ^U clear input buffer. Key::Esc | Key::Ctrl('u') => { buffer.reset(); - insert_mode = false; + buffer.visible = false; layout.force_redraw = true; }, // ^W removes last word. Key::Ctrl('w') => { buffer.strip_word(); if buffer.is_empty() { - insert_mode = false; + buffer.visible = false; layout.force_redraw = true; } }, @@ -242,12 +239,12 @@ pub fn run( // Delete last char in buffer. buffer.strip_char(); if buffer.is_empty() { - insert_mode = false; + buffer.visible = false; layout.force_redraw = true; } }, // Forward every char if in insert mode. - Key::Char(c) if insert_mode => { + Key::Char(c) if buffer.visible => { buffer.push(c); }, // Reset clock on 'r'. @@ -294,7 +291,7 @@ pub fn run( Key::Char(c) => { if c.is_ascii_digit() { buffer.push(c); - insert_mode = true; + buffer.visible = true; layout.force_redraw = true; } else if !buffer.is_empty() && c == ':' { buffer.push(':');