diff options
Diffstat (limited to 'src/line.rs')
| -rw-r--r-- | src/line.rs | 93 |
1 files changed, 80 insertions, 13 deletions
diff --git a/src/line.rs b/src/line.rs index 1ae210a..613dffe 100644 --- a/src/line.rs +++ b/src/line.rs | |||
| @@ -43,27 +43,27 @@ impl Editor { | |||
| 43 | let guard = RawGuard::enable()?; | 43 | let guard = RawGuard::enable()?; |
| 44 | let mut buffer: Vec<char> = Vec::new(); | 44 | let mut buffer: Vec<char> = Vec::new(); |
| 45 | let mut cursor = 0usize; | 45 | let mut cursor = 0usize; |
| 46 | let mut rows = 1usize; | 46 | let mut state = DrawState::default(); |
| 47 | 47 | ||
| 48 | self.history_index = self.history.len(); | 48 | self.history_index = self.history.len(); |
| 49 | self.draft.clear(); | 49 | self.draft.clear(); |
| 50 | 50 | ||
| 51 | draw(prompt, &buffer, cursor, &mut rows)?; | 51 | draw(prompt, &buffer, cursor, &mut state)?; |
| 52 | 52 | ||
| 53 | loop { | 53 | loop { |
| 54 | let event = event::read()?; | 54 | let event = event::read()?; |
| 55 | match event { | 55 | match event { |
| 56 | Event::Paste(text) => { | 56 | Event::Paste(text) => { |
| 57 | insert(&mut buffer, &mut cursor, &text); | 57 | insert(&mut buffer, &mut cursor, &text); |
| 58 | draw(prompt, &buffer, cursor, &mut rows)?; | 58 | draw(prompt, &buffer, cursor, &mut state)?; |
| 59 | } | 59 | } |
| 60 | Event::Resize(_, _) => { | 60 | Event::Resize(_, _) => { |
| 61 | // The terminal reflowed the block we were tracking, so the | 61 | // The terminal reflowed the block we were tracking, so the |
| 62 | // old row count is meaningless. Start a fresh prompt line | 62 | // old row count is meaningless. Start a fresh prompt line |
| 63 | // rather than risk erasing output above it. | 63 | // rather than risk erasing output above it. |
| 64 | println!(); | 64 | println!(); |
| 65 | rows = 1; | 65 | state = DrawState::default(); |
| 66 | draw(prompt, &buffer, cursor, &mut rows)?; | 66 | draw(prompt, &buffer, cursor, &mut state)?; |
| 67 | } | 67 | } |
| 68 | Event::Key(key) if key.kind == KeyEventKind::Press => { | 68 | Event::Key(key) if key.kind == KeyEventKind::Press => { |
| 69 | match handle_key(key, &mut buffer, &mut cursor, self) { | 69 | match handle_key(key, &mut buffer, &mut cursor, self) { |
| @@ -71,7 +71,7 @@ impl Editor { | |||
| 71 | Action::Submit => { | 71 | Action::Submit => { |
| 72 | // Redraw with the caret at the end so the submitted | 72 | // Redraw with the caret at the end so the submitted |
| 73 | // line looks complete before we move on. | 73 | // line looks complete before we move on. |
| 74 | draw(prompt, &buffer, buffer.len(), &mut rows)?; | 74 | draw(prompt, &buffer, buffer.len(), &mut state)?; |
| 75 | drop(guard); | 75 | drop(guard); |
| 76 | let line: String = buffer.iter().collect(); | 76 | let line: String = buffer.iter().collect(); |
| 77 | self.remember(&line); | 77 | self.remember(&line); |
| @@ -89,7 +89,7 @@ impl Editor { | |||
| 89 | return Ok(Some(String::new())); | 89 | return Ok(Some(String::new())); |
| 90 | } | 90 | } |
| 91 | } | 91 | } |
| 92 | draw(prompt, &buffer, cursor, &mut rows)?; | 92 | draw(prompt, &buffer, cursor, &mut state)?; |
| 93 | } | 93 | } |
| 94 | _ => {} | 94 | _ => {} |
| 95 | } | 95 | } |
| @@ -303,15 +303,40 @@ fn cells(ch: char, col: usize) -> usize { | |||
| 303 | unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) | 303 | unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) |
| 304 | } | 304 | } |
| 305 | 305 | ||
| 306 | fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { | 306 | /// Where the caret physically is, relative to the first row of the input block. |
| 307 | let width = terminal_width(); | 307 | /// The previous draw can leave the caret on any row, so a redraw must move up by |
| 308 | /// that row, not by the number of rows the block occupies. | ||
| 309 | #[derive(Default, Clone, Copy)] | ||
| 310 | struct DrawState { | ||
| 311 | rows: usize, | ||
| 312 | cursor_row: usize, | ||
| 313 | } | ||
| 314 | |||
| 315 | fn draw( | ||
| 316 | prompt: &str, | ||
| 317 | buffer: &[char], | ||
| 318 | cursor: usize, | ||
| 319 | state: &mut DrawState, | ||
| 320 | ) -> io::Result<()> { | ||
| 308 | let mut out = io::stdout(); | 321 | let mut out = io::stdout(); |
| 322 | draw_to(&mut out, prompt, buffer, cursor, state)?; | ||
| 323 | out.flush() | ||
| 324 | } | ||
| 325 | |||
| 326 | fn draw_to<W: Write>( | ||
| 327 | out: &mut W, | ||
| 328 | prompt: &str, | ||
| 329 | buffer: &[char], | ||
| 330 | cursor: usize, | ||
| 331 | state: &mut DrawState, | ||
| 332 | ) -> io::Result<()> { | ||
| 333 | let width = terminal_width(); | ||
| 309 | 334 | ||
| 310 | let (end_row, _) = layout(prompt, buffer, buffer.len(), width); | 335 | let (end_row, _) = layout(prompt, buffer, buffer.len(), width); |
| 311 | let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); | 336 | let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); |
| 312 | 337 | ||
| 313 | if *rows > 1 { | 338 | if state.cursor_row > 0 { |
| 314 | execute!(out, MoveUp(clamp(*rows - 1)))?; | 339 | execute!(out, MoveUp(clamp(state.cursor_row)))?; |
| 315 | } | 340 | } |
| 316 | execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; | 341 | execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; |
| 317 | 342 | ||
| @@ -330,9 +355,9 @@ fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::R | |||
| 330 | execute!(out, MoveUp(clamp(end_row - cursor_row)))?; | 355 | execute!(out, MoveUp(clamp(end_row - cursor_row)))?; |
| 331 | } | 356 | } |
| 332 | execute!(out, MoveToColumn(clamp(cursor_col)))?; | 357 | execute!(out, MoveToColumn(clamp(cursor_col)))?; |
| 333 | out.flush()?; | ||
| 334 | 358 | ||
| 335 | *rows = end_row + 1; | 359 | state.rows = end_row + 1; |
| 360 | state.cursor_row = cursor_row; | ||
| 336 | Ok(()) | 361 | Ok(()) |
| 337 | } | 362 | } |
| 338 | 363 | ||
| @@ -433,6 +458,48 @@ mod tests { | |||
| 433 | assert!(terminal_width() >= 1); | 458 | assert!(terminal_width() >= 1); |
| 434 | } | 459 | } |
| 435 | 460 | ||
| 461 | fn drawn(prompt: &str, buffer: &str, cursor: usize, state: &mut DrawState) -> String { | ||
| 462 | let chars: Vec<char> = buffer.chars().collect(); | ||
| 463 | let mut out = Vec::new(); | ||
| 464 | draw_to(&mut out, prompt, &chars, cursor, state).unwrap(); | ||
| 465 | String::from_utf8(out).unwrap() | ||
| 466 | } | ||
| 467 | |||
| 468 | #[test] | ||
| 469 | fn redraw_moves_up_by_the_caret_row_not_the_block_height() { | ||
| 470 | let mut state = DrawState::default(); | ||
| 471 | |||
| 472 | // First draw with the caret at the end of a two-line buffer. | ||
| 473 | let first = drawn("> ", "one\ntwo", 7, &mut state); | ||
| 474 | assert_eq!(state.cursor_row, 1, "caret ends on the second row"); | ||
| 475 | assert!(!first.contains("\u{1b}[1A"), "first draw must not move up"); | ||
| 476 | |||
| 477 | // Move the caret to the first row. The previous caret was on row 1, so | ||
| 478 | // the redraw may move up exactly one row — not `rows - 1` from row 0, | ||
| 479 | // which would land on the line above the prompt and erase it. | ||
| 480 | let second = drawn("> ", "one\ntwo", 0, &mut state); | ||
| 481 | assert!( | ||
| 482 | second.starts_with("\u{1b}[1A"), | ||
| 483 | "expected one MoveUp, got {:?}", | ||
| 484 | second | ||
| 485 | ); | ||
| 486 | assert_eq!(state.cursor_row, 0); | ||
| 487 | |||
| 488 | // Drawing again with the caret already on row 0 must not move up | ||
| 489 | // *before* clearing; the only MoveUp is the one after writing, which | ||
| 490 | // puts the caret back on the first row. | ||
| 491 | let third = drawn("> ", "one\ntwo", 0, &mut state); | ||
| 492 | assert!( | ||
| 493 | !third.starts_with("\u{1b}[1A"), | ||
| 494 | "must not move up before clearing: {third:?}" | ||
| 495 | ); | ||
| 496 | assert_eq!( | ||
| 497 | third.matches("\u{1b}[1A").count(), | ||
| 498 | 1, | ||
| 499 | "exactly one repositioning move after writing: {third:?}" | ||
| 500 | ); | ||
| 501 | } | ||
| 502 | |||
| 436 | #[test] | 503 | #[test] |
| 437 | fn layout_counts_wide_and_combining_characters() { | 504 | fn layout_counts_wide_and_combining_characters() { |
| 438 | let wide: Vec<char> = "日本語".chars().collect(); | 505 | let wide: Vec<char> = "日本語".chars().collect(); |
