From 1eb955b474bce1268f273239ddac5f8dbfd69f34 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 5 Mar 2023 16:48:50 -0600 Subject: [PATCH] some more line movements --- src/line.rs | 15 +++++++++++++++ src/main.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/line.rs b/src/line.rs index eb4d7c2..d01c6c2 100644 --- a/src/line.rs +++ b/src/line.rs @@ -68,6 +68,21 @@ impl Line { } } + /// moves the cursor to the start of the edit line, returning the old cursor value + pub fn seek_left(&mut self) -> usize { + let n = self.cursor; + self.cursor = 0; + self.show_debug(); + n + } + + pub fn seek_right(&mut self) -> usize { + let n = self.chars.len() - self.cursor; + self.cursor = self.chars.len(); + self.show_debug(); + n + } + pub fn forward(&mut self) -> bool { if self.cursor < self.chars.len() { self.cursor += 1; diff --git a/src/main.rs b/src/main.rs index 845c852..b98f554 100644 --- a/src/main.rs +++ b/src/main.rs @@ -388,7 +388,7 @@ fn main() -> Result<()> { // CTRL-U to erase to the beginning of the line if event.ctrl && event.code == key::U { - info!("» clear-left"); + info!("» clear left"); let n = line.clear_left(); if n > 0 { // move left by the number of elements removed @@ -411,6 +411,34 @@ fn main() -> Result<()> { continue; } + // CTRL-A to move to the beginning of the line + if event.ctrl && event.code == key::A { + info!("» seek left"); + let n = line.seek_left(); + if n > 0 { + // move left by the distance seeked + let text = format!("\x1b[{}D", n); + unsafe { + Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; + } + } + continue; + } + + // CTRL-E to move to the end of the line + if event.ctrl && event.code == key::E { + info!("» seek right"); + let n = line.seek_right(); + if n > 0 { + // move right by the distance seeked + let text = format!("\x1b[{}C", n); + unsafe { + Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; + } + } + continue; + } + // TODO: something better here, this is crappy. I should be checking characters // based on their unicode categories, not this garbo if !event.char.is_control() {