Minor code cleanup

This commit is contained in:
Timothy Warren 2019-09-19 16:45:48 -04:00
parent 4d1560717e
commit c3dee47b46
3 changed files with 14 additions and 20 deletions

View File

@ -1,7 +1,7 @@
# Oxidized Kilo # Oxidized Kilo
An experimental reimplementation of the [Kilo](https://viewsourcecode.org/snaptoken/kilo/index.html) A reimplementation of the [Kilo](https://viewsourcecode.org/snaptoken/kilo/index.html)
tutorial with a Rust implementation. tutorial in Rust.
## Implementation notes: ## Implementation notes:
* The `editor` prefix has been removed from all the editor methods. Since this implementation * 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. 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. * 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). 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: ### Additions / Changes
* Tab key does not expand to an indent * 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

View File

@ -146,6 +146,7 @@ pub enum KeyCode<T = char> {
EndKey, EndKey,
PageUp, PageUp,
PageDown, PageDown,
Tab,
/// Control key chords /// Control key chords
Ctrl(T), Ctrl(T),
/// Function keys (F1, etc.) T holds the index /// Function keys (F1, etc.) T holds the index
@ -285,6 +286,7 @@ impl Editor {
'\x08' => return Some(Backspace), '\x08' => return Some(Backspace),
'\x7f' => return Some(Backspace), '\x7f' => return Some(Backspace),
'\r' => return Some(Enter), '\r' => return Some(Enter),
'\t' => return Some(Tab),
ch => { ch => {
if ch.is_ascii_control() { if ch.is_ascii_control() {
return Some(Ctrl(ctrl_to_letter(ch))); return Some(Ctrl(ctrl_to_letter(ch)));
@ -790,6 +792,7 @@ impl Editor {
self.cursor_x = self.rows[self.cursor_y].chars.len(); self.cursor_x = self.rows[self.cursor_y].chars.len();
} }
} }
Tab => self.insert_char('\t'),
Ctrl(c) => match c { Ctrl(c) => match c {
'f' => self.find(), 'f' => self.find(),
's' => { 's' => {

View File

@ -23,10 +23,8 @@ lazy_static! {
} }
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
// 'Access' the saved termios instance, to make sure it is set // Save existing Termios settings
// before you enable raw mode. lazy_static::initialize(&ORIGINAL_TERMIOS);
let mutex = Arc::clone(&ORIGINAL_TERMIOS);
let _ = mutex.lock().unwrap();
// Disable canonical/"cooked" terminal mode // Disable canonical/"cooked" terminal mode
enable_raw_mode(); enable_raw_mode();
@ -48,18 +46,8 @@ fn main() -> Result<(), Error> {
loop { loop {
editor.refresh_screen(); editor.refresh_screen();
match editor.process_keypress() { if editor.process_keypress().is_none() {
Some(key) => { break;
match key {
editor::KeyCode::OtherKey('\0') => (),
_ => {
//println!("{:?}\r\n", key)
()
}
}
}
None => break,
} }
} }