1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
use crate::widget::Viewport;
use crate::word::{find_word_start_backward, find_word_start_forward};
#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
use std::cmp;
/// Specify how to move the cursor.
///
/// This type is marked as `#[non_exhaustive]` since more variations may be supported in the future.
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub enum CursorMove {
/// Move cursor forward by one character. When the cursor is at the end of line, it moves to the head of next line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["abc"]);
///
/// textarea.move_cursor(CursorMove::Forward);
/// assert_eq!(textarea.cursor(), (0, 1));
/// textarea.move_cursor(CursorMove::Forward);
/// assert_eq!(textarea.cursor(), (0, 2));
/// ```
Forward,
/// Move cursor backward by one character. When the cursor is at the head of line, it moves to the end of previous
/// line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["abc"]);
///
/// textarea.move_cursor(CursorMove::Forward);
/// textarea.move_cursor(CursorMove::Forward);
/// textarea.move_cursor(CursorMove::Back);
/// assert_eq!(textarea.cursor(), (0, 1));
/// ```
Back,
/// Move cursor up by one line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["a", "b", "c"]);
///
/// textarea.move_cursor(CursorMove::Down);
/// textarea.move_cursor(CursorMove::Down);
/// textarea.move_cursor(CursorMove::Up);
/// assert_eq!(textarea.cursor(), (1, 0));
/// ```
Up,
/// Move cursor down by one line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["a", "b", "c"]);
///
/// textarea.move_cursor(CursorMove::Down);
/// assert_eq!(textarea.cursor(), (1, 0));
/// textarea.move_cursor(CursorMove::Down);
/// assert_eq!(textarea.cursor(), (2, 0));
/// ```
Down,
/// Move cursor to the head of line. When the cursor is at the head of line, it moves to the end of previous line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["abc"]);
///
/// textarea.move_cursor(CursorMove::Forward);
/// textarea.move_cursor(CursorMove::Forward);
/// textarea.move_cursor(CursorMove::Head);
/// assert_eq!(textarea.cursor(), (0, 0));
/// ```
Head,
/// Move cursor to the end of line. When the cursor is at the end of line, it moves to the head of next line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["abc"]);
///
/// textarea.move_cursor(CursorMove::End);
/// assert_eq!(textarea.cursor(), (0, 3));
/// ```
End,
/// Move cursor to the top of lines.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["a", "b", "c"]);
///
/// textarea.move_cursor(CursorMove::Down);
/// textarea.move_cursor(CursorMove::Down);
/// textarea.move_cursor(CursorMove::Top);
/// assert_eq!(textarea.cursor(), (0, 0));
/// ```
Top,
/// Move cursor to the bottom of lines.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["a", "b", "c"]);
///
/// textarea.move_cursor(CursorMove::Bottom);
/// assert_eq!(textarea.cursor(), (2, 0));
/// ```
Bottom,
/// Move cursor forward by one word. Word boundary appears at spaces, punctuations, and others. For example
/// `fn foo(a)` consists of words `fn`, `foo`, `(`, `a`, `)`. When the cursor is at the end of line, it moves to the
/// head of next line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["aaa bbb ccc"]);
///
/// textarea.move_cursor(CursorMove::WordForward);
/// assert_eq!(textarea.cursor(), (0, 4));
/// textarea.move_cursor(CursorMove::WordForward);
/// assert_eq!(textarea.cursor(), (0, 8));
/// ```
WordForward,
/// Move cursor backward by one word. Word boundary appears at spaces, punctuations, and others. For example
/// `fn foo(a)` consists of words `fn`, `foo`, `(`, `a`, `)`.When the cursor is at the head of line, it moves to
/// the end of previous line.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["aaa bbb ccc"]);
///
/// textarea.move_cursor(CursorMove::End);
/// textarea.move_cursor(CursorMove::WordBack);
/// assert_eq!(textarea.cursor(), (0, 8));
/// textarea.move_cursor(CursorMove::WordBack);
/// assert_eq!(textarea.cursor(), (0, 4));
/// textarea.move_cursor(CursorMove::WordBack);
/// assert_eq!(textarea.cursor(), (0, 0));
/// ```
WordBack,
/// Move cursor down by one paragraph. Paragraph is a chunk of non-empty lines. Cursor moves to the first line of paragraph.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// // aaa
/// //
/// // bbb
/// //
/// // ccc
/// // ddd
/// let mut textarea = TextArea::from(["aaa", "", "bbb", "", "ccc", "ddd"]);
///
/// textarea.move_cursor(CursorMove::ParagraphForward);
/// assert_eq!(textarea.cursor(), (2, 0));
/// textarea.move_cursor(CursorMove::ParagraphForward);
/// assert_eq!(textarea.cursor(), (4, 0));
/// ```
ParagraphForward,
/// Move cursor up by one paragraph. Paragraph is a chunk of non-empty lines. Cursor moves to the first line of paragraph.
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// // aaa
/// //
/// // bbb
/// //
/// // ccc
/// // ddd
/// let mut textarea = TextArea::from(["aaa", "", "bbb", "", "ccc", "ddd"]);
///
/// textarea.move_cursor(CursorMove::Bottom);
/// textarea.move_cursor(CursorMove::ParagraphBack);
/// assert_eq!(textarea.cursor(), (4, 0));
/// textarea.move_cursor(CursorMove::ParagraphBack);
/// assert_eq!(textarea.cursor(), (2, 0));
/// textarea.move_cursor(CursorMove::ParagraphBack);
/// assert_eq!(textarea.cursor(), (0, 0));
/// ```
ParagraphBack,
/// Move cursor to (row, col) position. When the position points outside the text, the cursor position is made fit
/// within the text. Note that row and col are 0-based. (0, 0) means the first character of the first line.
///
/// When there are 10 lines, jumping to row 15 moves the cursor to the last line (row is 9 in the case). When there
/// are 10 characters in the line, jumping to col 15 moves the cursor to end of the line (col is 10 in the case).
/// ```
/// use tui_textarea::{TextArea, CursorMove};
///
/// let mut textarea = TextArea::from(["aaaa", "bbbb", "cccc"]);
///
/// textarea.move_cursor(CursorMove::Jump(1, 2));
/// assert_eq!(textarea.cursor(), (1, 2));
///
/// textarea.move_cursor(CursorMove::Jump(10, 10));
/// assert_eq!(textarea.cursor(), (2, 4));
/// ```
Jump(u16, u16),
/// Move cursor to keep it within the viewport. For example, when a viewport displays line 8 to line 16:
///
/// - cursor at line 4 is moved to line 8
/// - cursor at line 20 is moved to line 16
/// - cursor at line 12 is not moved
///
/// This is useful when you moved a cursor but you don't want to move the viewport.
/// ```
/// # use tui::buffer::Buffer;
/// # use tui::layout::Rect;
/// # use tui::widgets::Widget;
/// use tui_textarea::{TextArea, CursorMove};
///
/// // Let's say terminal height is 8.
///
/// // Create textarea with 20 lines "0", "1", "2", "3", ...
/// // The viewport is displaying from line 1 to line 8.
/// let mut textarea: TextArea = (0..20).into_iter().map(|i| i.to_string()).collect();
/// # // Call `render` at least once to populate terminal size
/// # let r = Rect { x: 0, y: 0, width: 24, height: 8 };
/// # let mut b = Buffer::empty(r.clone());
/// # textarea.widget().render(r, &mut b);
///
/// // Move cursor to the end of lines (line 20). It is outside the viewport (line 1 to line 8)
/// textarea.move_cursor(CursorMove::Bottom);
/// assert_eq!(textarea.cursor(), (19, 0));
///
/// // Cursor is moved to line 8 to enter the viewport
/// textarea.move_cursor(CursorMove::InViewport);
/// assert_eq!(textarea.cursor(), (7, 0));
/// ```
InViewport,
}
impl CursorMove {
pub(crate) fn next_cursor(
&self,
(row, col): (usize, usize),
lines: &[String],
viewport: &Viewport,
) -> Option<(usize, usize)> {
use CursorMove::*;
fn fit_col(col: usize, line: &str) -> usize {
cmp::min(col, line.chars().count())
}
match self {
Forward if col >= lines[row].chars().count() => {
(row + 1 < lines.len()).then(|| (row + 1, 0))
}
Forward => Some((row, col + 1)),
Back if col == 0 => {
let row = row.checked_sub(1)?;
Some((row, lines[row].chars().count()))
}
Back => Some((row, col - 1)),
Up => {
let row = row.checked_sub(1)?;
Some((row, fit_col(col, &lines[row])))
}
Down => Some((row + 1, fit_col(col, lines.get(row + 1)?))),
Head => Some((row, 0)),
End => Some((row, lines[row].chars().count())),
Top => Some((0, fit_col(col, &lines[0]))),
Bottom => {
let row = lines.len() - 1;
Some((row, fit_col(col, &lines[row])))
}
WordForward => {
if let Some(col) = find_word_start_forward(&lines[row], col) {
Some((row, col))
} else if row + 1 < lines.len() {
Some((row + 1, 0))
} else {
Some((row, lines[row].chars().count()))
}
}
WordBack => {
if let Some(col) = find_word_start_backward(&lines[row], col) {
Some((row, col))
} else if row > 0 {
Some((row - 1, lines[row - 1].chars().count()))
} else {
Some((row, 0))
}
}
ParagraphForward => {
let mut prev_is_empty = lines[row].is_empty();
for row in row + 1..lines.len() {
let line = &lines[row];
let is_empty = line.is_empty();
if !is_empty && prev_is_empty {
return Some((row, fit_col(col, line)));
}
prev_is_empty = is_empty;
}
let row = lines.len() - 1;
Some((row, fit_col(col, &lines[row])))
}
ParagraphBack => {
let row = row.checked_sub(1)?;
let mut prev_is_empty = lines[row].is_empty();
for row in (0..row).rev() {
let is_empty = lines[row].is_empty();
if is_empty && !prev_is_empty {
return Some((row + 1, fit_col(col, &lines[row + 1])));
}
prev_is_empty = is_empty;
}
Some((0, fit_col(col, &lines[0])))
}
Jump(row, col) => {
let row = cmp::min(*row as usize, lines.len() - 1);
let col = fit_col(*col as usize, &lines[row]);
Some((row, col))
}
InViewport => {
let (row_top, col_top, row_bottom, col_bottom) = viewport.position();
let row = row.clamp(row_top as usize, row_bottom as usize);
let row = cmp::min(row, lines.len() - 1);
let col = col.clamp(col_top as usize, col_bottom as usize);
let col = fit_col(col, &lines[row]);
Some((row, col))
}
}
}
}
#[cfg(test)]
mod tests {
// Seaparate tests for ratatui support
#[test]
fn in_viewport() {
use crate::tui::buffer::Buffer;
use crate::tui::layout::Rect;
use crate::tui::widgets::Widget;
use crate::{CursorMove, TextArea};
let mut textarea: TextArea = (0..20).into_iter().map(|i| i.to_string()).collect();
let r = Rect {
x: 0,
y: 0,
width: 24,
height: 8,
};
let mut b = Buffer::empty(r.clone());
textarea.widget().render(r, &mut b);
textarea.move_cursor(CursorMove::Bottom);
assert_eq!(textarea.cursor(), (19, 0));
textarea.move_cursor(CursorMove::InViewport);
assert_eq!(textarea.cursor(), (7, 0));
}
}