Skip to content

Commit

Permalink
feat : add funtions of cursor moving (#10)
Browse files Browse the repository at this point in the history
* add funtions of cursor moving
  • Loading branch information
laokengwt authored Aug 29, 2024
1 parent 55c6e59 commit 2d55501
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/utils/ui/mode/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UiCore>,
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 {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 2d55501

Please sign in to comment.