From c3dee47b46d182cdaaed5f83de4581a76217e6a4 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 19 Sep 2019 16:45:48 -0400 Subject: [PATCH] Minor code cleanup --- README.md | 11 +++++++---- src/editor.rs | 3 +++ src/main.rs | 20 ++++---------------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index f7f0cc2..f2ab3ed 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Oxidized Kilo -An experimental reimplementation of the [Kilo](https://viewsourcecode.org/snaptoken/kilo/index.html) -tutorial with a Rust implementation. +A reimplementation of the [Kilo](https://viewsourcecode.org/snaptoken/kilo/index.html) +tutorial in Rust. ## Implementation notes: * The `editor` prefix has been removed from all the editor methods. Since this implementation @@ -10,6 +10,9 @@ uses `impl`s on a shared `Editor` struct, the prefix is redundant implemented in a more idiomatic Rust fashion. * Row structs are referenced by their index in the Editor struct, rather than as a direct reference in method calls. This generally simplifies dealing with the rules of Rust (borrow checker). +* The `prompt` method of the editor can not take an arbitrary formatting string, due to Rust requiring a string literal +for string formatting macros. -### Known issues: -* Tab key does not expand to an indent +### Additions / Changes +* Reverse coloring for search results, so comment types can have different colors +* Two separate vectors are used to define the two types of keywords, rather than the weird pipe suffix \ No newline at end of file diff --git a/src/editor.rs b/src/editor.rs index 32afa5b..9f24736 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -146,6 +146,7 @@ pub enum KeyCode { EndKey, PageUp, PageDown, + Tab, /// Control key chords Ctrl(T), /// Function keys (F1, etc.) T holds the index @@ -285,6 +286,7 @@ impl Editor { '\x08' => return Some(Backspace), '\x7f' => return Some(Backspace), '\r' => return Some(Enter), + '\t' => return Some(Tab), ch => { if ch.is_ascii_control() { return Some(Ctrl(ctrl_to_letter(ch))); @@ -790,6 +792,7 @@ impl Editor { self.cursor_x = self.rows[self.cursor_y].chars.len(); } } + Tab => self.insert_char('\t'), Ctrl(c) => match c { 'f' => self.find(), 's' => { diff --git a/src/main.rs b/src/main.rs index a6e16c9..9c88b73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,10 +23,8 @@ lazy_static! { } fn main() -> Result<(), Error> { - // 'Access' the saved termios instance, to make sure it is set - // before you enable raw mode. - let mutex = Arc::clone(&ORIGINAL_TERMIOS); - let _ = mutex.lock().unwrap(); + // Save existing Termios settings + lazy_static::initialize(&ORIGINAL_TERMIOS); // Disable canonical/"cooked" terminal mode enable_raw_mode(); @@ -48,18 +46,8 @@ fn main() -> Result<(), Error> { loop { editor.refresh_screen(); - match editor.process_keypress() { - Some(key) => { - match key { - editor::KeyCode::OtherKey('\0') => (), - _ => { - //println!("{:?}\r\n", key) - - () - } - } - } - None => break, + if editor.process_keypress().is_none() { + break; } }