From 2d555019b1c939cd408dc2e6d3c47b750beee64d Mon Sep 17 00:00:00 2001 From: laokengwt <143977175+laokengwt@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:00:29 +0800 Subject: [PATCH] feat : add funtions of cursor moving (#10) * add funtions of cursor moving --- src/utils/ui/mode/mode.rs | 43 ++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/utils/ui/mode/mode.rs b/src/utils/ui/mode/mode.rs index c085a99..bd4ac12 100644 --- a/src/utils/ui/mode/mode.rs +++ b/src/utils/ui/mode/mode.rs @@ -373,6 +373,23 @@ impl Command { ui.cursor.move_to(prev_word_pos as u16, y)?; return Ok(WarpUiCallBackType::None); } + + pub fn move_to_nlines_of_screen( + &self, + ui: &mut MutexGuard, + n: usize, + ) -> io::Result<()> { + let y = ui.cursor.y() as usize; + + let offset = ui.buffer.offset(); + + let new_y = ui.buffer.goto_line(offset + n); + ui.render_content(0, CONTENT_WINSIZE.read().unwrap().rows as usize)?; + ui.cursor.move_to_row(new_y)?; + ui.cursor.highlight(Some(y as u16))?; + + Ok(()) + } } impl KeyEventCallback for Command { @@ -455,19 +472,10 @@ impl KeyEventCallback for Command { // 向右 b"l" => self.right(ui), + // 移动到当前屏幕最后一行 b"L" => { - // 设置当前行lock - let flag = ui.buffer.line_flags(ui.cursor.y()); - let offset = ui.buffer.offset(); - if flag.contains(LineState::LOCKED) { - ui.buffer - .remove_line_flags(offset + ui.cursor.y() as usize, LineState::LOCKED); - } else { - ui.buffer - .add_line_flags(offset + ui.cursor.y() as usize, LineState::LOCKED); - } - let y = ui.cursor.y(); - ui.render_content(y, 1)?; + let win_size = CONTENT_WINSIZE.read().unwrap().rows as usize; + self.move_to_nlines_of_screen(ui, win_size - 1)?; return Ok(WarpUiCallBackType::None); } @@ -549,6 +557,17 @@ impl KeyEventCallback for Command { return Ok(WarpUiCallBackType::None); } + b"H" => { + self.move_to_nlines_of_screen(ui, 0)?; + return Ok(WarpUiCallBackType::None); + } + + b"M" => { + let win_size = CONTENT_WINSIZE.read().unwrap().rows as usize; + self.move_to_nlines_of_screen(ui, win_size / 2)?; + return Ok(WarpUiCallBackType::None); + } + _ => { return Ok(WarpUiCallBackType::None); }