1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-09-08 16:35:49 +03:00

refactor logic to move cursor with *::checked_sub

Этот коммит содержится в:
rhysd
2022-06-13 20:26:23 +09:00
родитель 35b4fb3fd9
Коммит f592f59f71
2 изменённых файлов: 13 добавлений и 9 удалений

Просмотреть файл

@@ -34,12 +34,16 @@ impl CursorMove {
(row + 1 < lines.len()).then(|| (row + 1, 0)) (row + 1 < lines.len()).then(|| (row + 1, 0))
} }
Forward => Some((row, col + 1)), Forward => Some((row, col + 1)),
Back if col == 0 => (row > 0).then(|| (row - 1, lines[row - 1].chars().count())), Back if col == 0 => {
let row = row.checked_sub(1)?;
Some((row, lines[row].chars().count()))
}
Back => Some((row, col - 1)), Back => Some((row, col - 1)),
Up if row == 0 => None, Up => {
Up => Some((row - 1, fit_col(col, &lines[row - 1]))), let row = row.checked_sub(1)?;
Down if row + 1 >= lines.len() => None, Some((row, fit_col(col, &lines[row])))
Down => (Some((row + 1, fit_col(col, &lines[row + 1])))), }
Down => (Some((row + 1, fit_col(col, lines.get(row + 1)?)))),
Head => Some((row, 0)), Head => Some((row, 0)),
End => Some((row, lines[row].chars().count())), End => Some((row, lines[row].chars().count())),
Top => Some((0, fit_col(col, &lines[0]))), Top => Some((0, fit_col(col, &lines[0]))),
@@ -78,10 +82,10 @@ impl CursorMove {
let row = lines.len() - 1; let row = lines.len() - 1;
Some((row, fit_col(col, &lines[row]))) Some((row, fit_col(col, &lines[row])))
} }
ParagraphBack if row == 0 => None,
ParagraphBack => { ParagraphBack => {
let mut prev_is_empty = lines[row - 1].is_empty(); let row = row.checked_sub(1)?;
for row in (0..row - 1).rev() { let mut prev_is_empty = lines[row].is_empty();
for row in (0..row).rev() {
let is_empty = lines[row].is_empty(); let is_empty = lines[row].is_empty();
if is_empty && !prev_is_empty { if is_empty && !prev_is_empty {
return Some((row + 1, fit_col(col, &lines[row + 1]))); return Some((row + 1, fit_col(col, &lines[row + 1])));

Просмотреть файл

@@ -21,7 +21,7 @@ pub enum Key {
Null, Null,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Input { pub struct Input {
pub key: Key, pub key: Key,
pub ctrl: bool, pub ctrl: bool,