From f848ddc81dac7bb6cf69b0e9d4c5f47c51381a5e Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Mon, 24 Jul 2023 22:57:25 +0200 Subject: [PATCH 1/4] Fix cursor horizontal move on long lines --- src/usr/editor.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/usr/editor.rs b/src/usr/editor.rs index 074169c45..b41281845 100644 --- a/src/usr/editor.rs +++ b/src/usr/editor.rs @@ -344,8 +344,8 @@ impl Editor { csi = false; continue } else if self.cursor.x == self.cols() - 1 { - self.cursor.x = self.offset.x; self.offset.x += self.cols(); + self.cursor.x -= self.cols() - 1; self.print_screen(); } else { self.cursor.x += 1; @@ -358,8 +358,8 @@ impl Editor { csi = false; continue; } else if self.cursor.x == 0 { - self.cursor.x = self.offset.x - 1; self.offset.x -= self.cols(); + self.cursor.x += self.cols() - 1; self.print_screen(); self.cursor.x = self.next_pos(self.cursor.x, self.cursor.y); } else { From a56dc8153a31ed2cc6e4331b3436539067928f41 Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Mon, 24 Jul 2023 23:27:31 +0200 Subject: [PATCH 2/4] Fix cursor alignment on long lines --- src/usr/editor.rs | 50 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/usr/editor.rs b/src/usr/editor.rs index b41281845..6f6a4758c 100644 --- a/src/usr/editor.rs +++ b/src/usr/editor.rs @@ -236,6 +236,36 @@ impl Editor { self.highlighted.clear(); } + // Align cursor that is past the end of line, + // to the end of line or to the left of the screen. + // + // If the cursor is at the end of the long line + // that takes two screens in the following diagram: + // + // +----------------------+----------------------+ + // | | | + // | This is a short line | | + // | | | + // | This is a loooooooooo|oooooong line | + // | | | + // +----------------------+----------------------+ + // + // Going up should move the cursor to the left of + // the second screen, then going left should move + // to the left of the first screen. If going up + // another time before going left, the cursor + // should be at the end of the short line. + fn align_cursor(&mut self) { + let eol = self.lines[self.offset.y + self.cursor.y].chars().count(); + if eol <= self.offset.x + self.cursor.x { + if eol <= self.offset.x { + self.cursor.x = 0; + } else { + self.cursor.x = eol - self.offset.x - 1; + } + } + } + pub fn run(&mut self) -> Result<(), ExitCode> { print!("\x1b[2J\x1b[1;1H"); // Clear screen and move cursor to top self.print_screen(); @@ -319,7 +349,7 @@ impl Editor { self.offset.y -= 1; self.print_screen(); } - self.cursor.x = self.next_pos(self.cursor.x, self.cursor.y); + self.align_cursor(); }, 'B' if csi => { // Arrow Down let is_eof = self.offset.y + self.cursor.y == self.lines.len() - 1; @@ -333,7 +363,7 @@ impl Editor { } else { self.cursor.y += 1; } - self.cursor.x = self.next_pos(self.cursor.x, self.cursor.y); + self.align_cursor(); } }, 'C' if csi => { // Arrow Right @@ -361,7 +391,7 @@ impl Editor { self.offset.x -= self.cols(); self.cursor.x += self.cols() - 1; self.print_screen(); - self.cursor.x = self.next_pos(self.cursor.x, self.cursor.y); + self.align_cursor(); } else { self.cursor.x -= 1; } @@ -521,20 +551,6 @@ impl Editor { Ok(()) } - // Move cursor past end of line to end of line or left of the screen - fn next_pos(&self, x: usize, y: usize) -> usize { - let eol = self.lines[self.offset.y + y].chars().count(); - if eol <= self.offset.x + x { - if eol <= self.offset.x { - 0 - } else { - eol - 1 - } - } else { - x - } - } - fn rows(&self) -> usize { sys::console::rows() - 1 // Leave out one line for status line } From 04cf1dd6630cbec795414dc213ffb51fb1c8affb Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Mon, 24 Jul 2023 23:33:08 +0200 Subject: [PATCH 3/4] Increase diagram size in comment --- src/usr/editor.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/usr/editor.rs b/src/usr/editor.rs index 6f6a4758c..d7b0d7379 100644 --- a/src/usr/editor.rs +++ b/src/usr/editor.rs @@ -236,25 +236,24 @@ impl Editor { self.highlighted.clear(); } - // Align cursor that is past the end of line, - // to the end of line or to the left of the screen. + // Align cursor that is past the end of the line, to the end + // of the line or to the left of the screen. // - // If the cursor is at the end of the long line - // that takes two screens in the following diagram: + // If the cursor is at the end of the long line that takes + // two screens in the following diagram: // - // +----------------------+----------------------+ - // | | | - // | This is a short line | | - // | | | - // | This is a loooooooooo|oooooong line | - // | | | - // +----------------------+----------------------+ + // +----------------------------+----------------------------+ + // | | | + // | This is a short line | | + // | | | + // | This is a loooooooooooooooo|oooooong line | + // | | ^ | + // +----------------------------+----------------------------+ // - // Going up should move the cursor to the left of - // the second screen, then going left should move - // to the left of the first screen. If going up - // another time before going left, the cursor - // should be at the end of the short line. + // Going up should move the cursor to the left of the second + // screen, then going left should move to the left of the + // first screen. If going up another time before going left, + // the cursor should end up at the end of the short line. fn align_cursor(&mut self) { let eol = self.lines[self.offset.y + self.cursor.y].chars().count(); if eol <= self.offset.x + self.cursor.x { From 7e81348933f094339bd7899898af9162979b38dc Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Tue, 25 Jul 2023 09:54:22 +0200 Subject: [PATCH 4/4] Fix insertion --- src/usr/editor.rs | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/usr/editor.rs b/src/usr/editor.rs index d7b0d7379..eb39d2935 100644 --- a/src/usr/editor.rs +++ b/src/usr/editor.rs @@ -237,31 +237,27 @@ impl Editor { } // Align cursor that is past the end of the line, to the end - // of the line or to the left of the screen. + // of the line. // - // If the cursor is at the end of the long line that takes - // two screens in the following diagram: + // If the cursor is somewhere on the long line on the second + // screen in the following diagram, going down should move + // the cursor to the end of the short line and display the + // first screen instead of the second screen. // // +----------------------------+----------------------------+ // | | | - // | This is a short line | | - // | | | // | This is a loooooooooooooooo|oooooong line | - // | | ^ | + // | This is a short line | ^ | + // | ^ | | // +----------------------------+----------------------------+ - // - // Going up should move the cursor to the left of the second - // screen, then going left should move to the left of the - // first screen. If going up another time before going left, - // the cursor should end up at the end of the short line. fn align_cursor(&mut self) { - let eol = self.lines[self.offset.y + self.cursor.y].chars().count(); - if eol <= self.offset.x + self.cursor.x { - if eol <= self.offset.x { - self.cursor.x = 0; - } else { - self.cursor.x = eol - self.offset.x - 1; - } + let x = self.offset.x + self.cursor.x; + let y = self.offset.y + self.cursor.y; + let eol = self.lines[y].chars().count(); + if x > eol { + let n = self.cols(); + self.offset.x = (eol / n) * n; + self.cursor.x = eol % n; } } @@ -346,9 +342,9 @@ impl Editor { self.cursor.y -= 1 } else if self.offset.y > 0 { self.offset.y -= 1; - self.print_screen(); } self.align_cursor(); + self.print_screen(); }, 'B' if csi => { // Arrow Down let is_eof = self.offset.y + self.cursor.y == self.lines.len() - 1; @@ -357,12 +353,12 @@ impl Editor { if is_bottom || is_eof { if !is_eof { self.offset.y += 1; - self.print_screen(); } } else { self.cursor.y += 1; } self.align_cursor(); + self.print_screen(); } }, 'C' if csi => { // Arrow Right @@ -389,8 +385,8 @@ impl Editor { } else if self.cursor.x == 0 { self.offset.x -= self.cols(); self.cursor.x += self.cols() - 1; - self.print_screen(); self.align_cursor(); + self.print_screen(); } else { self.cursor.x -= 1; }