Skip to content

Commit 2d55501

Browse files
authored
feat : add funtions of cursor moving (#10)
* add funtions of cursor moving
1 parent 55c6e59 commit 2d55501

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/utils/ui/mode/mode.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,23 @@ impl Command {
373373
ui.cursor.move_to(prev_word_pos as u16, y)?;
374374
return Ok(WarpUiCallBackType::None);
375375
}
376+
377+
pub fn move_to_nlines_of_screen(
378+
&self,
379+
ui: &mut MutexGuard<UiCore>,
380+
n: usize,
381+
) -> io::Result<()> {
382+
let y = ui.cursor.y() as usize;
383+
384+
let offset = ui.buffer.offset();
385+
386+
let new_y = ui.buffer.goto_line(offset + n);
387+
ui.render_content(0, CONTENT_WINSIZE.read().unwrap().rows as usize)?;
388+
ui.cursor.move_to_row(new_y)?;
389+
ui.cursor.highlight(Some(y as u16))?;
390+
391+
Ok(())
392+
}
376393
}
377394

378395
impl KeyEventCallback for Command {
@@ -455,19 +472,10 @@ impl KeyEventCallback for Command {
455472
// 向右
456473
b"l" => self.right(ui),
457474

475+
// 移动到当前屏幕最后一行
458476
b"L" => {
459-
// 设置当前行lock
460-
let flag = ui.buffer.line_flags(ui.cursor.y());
461-
let offset = ui.buffer.offset();
462-
if flag.contains(LineState::LOCKED) {
463-
ui.buffer
464-
.remove_line_flags(offset + ui.cursor.y() as usize, LineState::LOCKED);
465-
} else {
466-
ui.buffer
467-
.add_line_flags(offset + ui.cursor.y() as usize, LineState::LOCKED);
468-
}
469-
let y = ui.cursor.y();
470-
ui.render_content(y, 1)?;
477+
let win_size = CONTENT_WINSIZE.read().unwrap().rows as usize;
478+
self.move_to_nlines_of_screen(ui, win_size - 1)?;
471479
return Ok(WarpUiCallBackType::None);
472480
}
473481

@@ -549,6 +557,17 @@ impl KeyEventCallback for Command {
549557
return Ok(WarpUiCallBackType::None);
550558
}
551559

560+
b"H" => {
561+
self.move_to_nlines_of_screen(ui, 0)?;
562+
return Ok(WarpUiCallBackType::None);
563+
}
564+
565+
b"M" => {
566+
let win_size = CONTENT_WINSIZE.read().unwrap().rows as usize;
567+
self.move_to_nlines_of_screen(ui, win_size / 2)?;
568+
return Ok(WarpUiCallBackType::None);
569+
}
570+
552571
_ => {
553572
return Ok(WarpUiCallBackType::None);
554573
}

0 commit comments

Comments
 (0)