| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
mod cell; |
| 10 |
mod edit; |
| 11 |
mod history; |
| 12 |
mod mouse; |
| 13 |
pub mod oracle; |
| 14 |
mod perform; |
| 15 |
mod ring; |
| 16 |
mod selection; |
| 17 |
mod sgr; |
| 18 |
mod text; |
| 19 |
|
| 20 |
#[cfg(test)] |
| 21 |
mod testutil; |
| 22 |
|
| 23 |
pub use cell::{Attrs, Cell, Color}; |
| 24 |
pub use mouse::{MouseAction, MouseButton, MouseEncoding, MouseMods, MouseReport, MouseTracking}; |
| 25 |
pub use selection::{Point, Selection, SelectionMode, SelectionSpan}; |
| 26 |
|
| 27 |
use cell::{MAX_MARKS, MarkTable, char_cols, encode_attrs, encode_color}; |
| 28 |
use std::collections::VecDeque; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 33 |
pub enum CursorShape { |
| 34 |
Block, |
| 35 |
Underline, |
| 36 |
Bar, |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[derive(Debug, Clone, Default)] |
| 42 |
pub struct Damage { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
pub scroll: i16, |
| 49 |
|
| 50 |
|
| 51 |
pub dirty_rows: Vec<u16>, |
| 52 |
|
| 53 |
|
| 54 |
pub screen_swapped: bool, |
| 55 |
|
| 56 |
pub resized: bool, |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pub view_moved: bool, |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#[derive(Clone, Debug, PartialEq, Eq)] |
| 77 |
pub struct Identity { |
| 78 |
|
| 79 |
pub name: String, |
| 80 |
pub version: String, |
| 81 |
|
| 82 |
|
| 83 |
pub cell_px: (u16, u16), |
| 84 |
|
| 85 |
pub fg: [u8; 3], |
| 86 |
pub bg: [u8; 3], |
| 87 |
} |
| 88 |
|
| 89 |
impl Default for Identity { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fn default() -> Self { |
| 98 |
Self { |
| 99 |
name: "shop".into(), |
| 100 |
version: "0".into(), |
| 101 |
cell_px: (0, 0), |
| 102 |
fg: [0xe6, 0xde, 0xd3], |
| 103 |
bg: [0x25, 0x23, 0x1f], |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
#[derive(Copy, Clone, Debug, Default)] |
| 110 |
pub struct Cursor { |
| 111 |
pub row: u16, |
| 112 |
pub col: u16, |
| 113 |
pub visible: bool, |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
pub wrap_next: bool, |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#[derive(Clone, Debug)] |
| 125 |
struct HistoryRow { |
| 126 |
cells: Vec<Cell>, |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
wrapped: bool, |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
pub const DEFAULT_HISTORY_LIMIT: usize = 10_000; |
| 141 |
|
| 142 |
pub struct Grid { |
| 143 |
cols: u16, |
| 144 |
rows: u16, |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
main: Vec<Cell>, |
| 151 |
alt: Vec<Cell>, |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
main_wrapped: Vec<bool>, |
| 161 |
alt_wrapped: Vec<bool>, |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
history: VecDeque<HistoryRow>, |
| 173 |
|
| 174 |
history_limit: usize, |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
marks: MarkTable, |
| 182 |
|
| 183 |
|
| 184 |
view_offset: u16, |
| 185 |
|
| 186 |
|
| 187 |
view_dirty: bool, |
| 188 |
on_alt: bool, |
| 189 |
main_origin: u16, |
| 190 |
alt_origin: u16, |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
region_origin: u16, |
| 195 |
cursor: Cursor, |
| 196 |
cursor_shape: CursorShape, |
| 197 |
|
| 198 |
|
| 199 |
swap_saved: [Cursor; 2], |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
dec_saved: [Cursor; 2], |
| 209 |
scroll_top: u16, |
| 210 |
scroll_bottom: u16, |
| 211 |
pending_fg: Color, |
| 212 |
pending_bg: Color, |
| 213 |
pending_attrs: Attrs, |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
pending_fg_word: u32, |
| 219 |
pending_bg_word: u32, |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
cur_row_start: u32, |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
sync_update: bool, |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
bracketed_paste: bool, |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
cursor_keys_application: bool, |
| 237 |
keypad_application: bool, |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
alternate_scroll: bool, |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
mouse_tracking: MouseTracking, |
| 249 |
mouse_encoding: MouseEncoding, |
| 250 |
pending_title: Option<String>, |
| 251 |
identity: Identity, |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
pending_replies: Vec<u8>, |
| 260 |
|
| 261 |
row_dirty: Vec<bool>, |
| 262 |
pending_scroll: i16, |
| 263 |
pending_screen_swap: bool, |
| 264 |
pending_resize: bool, |
| 265 |
} |
| 266 |
|
| 267 |
const CUR_ROW_INVALID: u32 = u32::MAX; |
| 268 |
|
| 269 |
impl Grid { |
| 270 |
pub fn new(cols: u16, rows: u16) -> Self { |
| 271 |
let cols = cols.max(1); |
| 272 |
let rows = rows.max(1); |
| 273 |
let cell_count = cols as usize * rows as usize; |
| 274 |
Self { |
| 275 |
cols, |
| 276 |
rows, |
| 277 |
main: vec![Cell::default(); cell_count], |
| 278 |
alt: vec![Cell::default(); cell_count], |
| 279 |
main_wrapped: vec![false; rows as usize], |
| 280 |
alt_wrapped: vec![false; rows as usize], |
| 281 |
history: VecDeque::new(), |
| 282 |
history_limit: DEFAULT_HISTORY_LIMIT, |
| 283 |
marks: MarkTable::default(), |
| 284 |
view_offset: 0, |
| 285 |
view_dirty: false, |
| 286 |
on_alt: false, |
| 287 |
main_origin: 0, |
| 288 |
alt_origin: 0, |
| 289 |
region_origin: 0, |
| 290 |
cursor: Cursor { |
| 291 |
visible: true, |
| 292 |
..Cursor::default() |
| 293 |
}, |
| 294 |
cursor_shape: CursorShape::Block, |
| 295 |
swap_saved: [Cursor::default(); 2], |
| 296 |
dec_saved: [Cursor::default(); 2], |
| 297 |
scroll_top: 0, |
| 298 |
scroll_bottom: rows - 1, |
| 299 |
pending_fg: Color::Default, |
| 300 |
pending_bg: Color::Default, |
| 301 |
pending_attrs: Attrs::default(), |
| 302 |
pending_fg_word: 0, |
| 303 |
pending_bg_word: 0, |
| 304 |
cur_row_start: CUR_ROW_INVALID, |
| 305 |
sync_update: false, |
| 306 |
bracketed_paste: false, |
| 307 |
cursor_keys_application: false, |
| 308 |
keypad_application: false, |
| 309 |
alternate_scroll: true, |
| 310 |
mouse_tracking: MouseTracking::Off, |
| 311 |
mouse_encoding: MouseEncoding::X10, |
| 312 |
pending_title: None, |
| 313 |
identity: Identity::default(), |
| 314 |
pending_replies: Vec::new(), |
| 315 |
|
| 316 |
|
| 317 |
row_dirty: vec![true; rows as usize], |
| 318 |
pending_scroll: 0, |
| 319 |
pending_screen_swap: false, |
| 320 |
pending_resize: true, |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
pub fn take_damage(&mut self) -> Damage { |
| 328 |
let scroll = std::mem::take(&mut self.pending_scroll); |
| 329 |
let screen_swapped = std::mem::take(&mut self.pending_screen_swap); |
| 330 |
let resized = std::mem::take(&mut self.pending_resize); |
| 331 |
let view_dirty = std::mem::take(&mut self.view_dirty); |
| 332 |
let mut dirty_rows: Vec<u16> = Vec::new(); |
| 333 |
for (i, d) in self.row_dirty.iter_mut().enumerate() { |
| 334 |
if *d { |
| 335 |
dirty_rows.push(i as u16); |
| 336 |
*d = false; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
let stale = view_dirty |
| 346 |
|| (self.view_offset > 0 && (scroll != 0 || !dirty_rows.is_empty() || screen_swapped)); |
| 347 |
if stale && !resized { |
| 348 |
return Damage { |
| 349 |
scroll: 0, |
| 350 |
dirty_rows: (0..self.rows).collect(), |
| 351 |
screen_swapped, |
| 352 |
resized, |
| 353 |
view_moved: true, |
| 354 |
}; |
| 355 |
} |
| 356 |
Damage { |
| 357 |
scroll, |
| 358 |
dirty_rows, |
| 359 |
screen_swapped, |
| 360 |
resized, |
| 361 |
view_moved: false, |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
fn mark_row_dirty(&mut self, row: u16) { |
| 366 |
if let Some(slot) = self.row_dirty.get_mut(row as usize) { |
| 367 |
*slot = true; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
fn mark_all_rows_dirty(&mut self) { |
| 372 |
for d in &mut self.row_dirty { |
| 373 |
*d = true; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
pub fn invalidate_render(&mut self) { |
| 391 |
self.mark_all_rows_dirty(); |
| 392 |
self.view_dirty = true; |
| 393 |
} |
| 394 |
|
| 395 |
pub fn cursor_shape(&self) -> CursorShape { |
| 396 |
self.cursor_shape |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
pub fn sync_update(&self) -> bool { |
| 403 |
self.sync_update |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
pub fn bracketed_paste(&self) -> bool { |
| 413 |
self.bracketed_paste |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
pub fn cursor_keys_application(&self) -> bool { |
| 421 |
self.cursor_keys_application |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
pub fn keypad_application(&self) -> bool { |
| 426 |
self.keypad_application |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
pub fn alternate_scroll(&self) -> bool { |
| 433 |
self.alternate_scroll |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
pub fn on_alt(&self) -> bool { |
| 441 |
self.on_alt |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
pub fn take_pending_title(&mut self) -> Option<String> { |
| 448 |
self.pending_title.take() |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
pub fn take_pending_replies(&mut self) -> Vec<u8> { |
| 456 |
std::mem::take(&mut self.pending_replies) |
| 457 |
} |
| 458 |
|
| 459 |
fn reply(&mut self, bytes: &[u8]) { |
| 460 |
self.pending_replies.extend_from_slice(bytes); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
pub fn set_identity(&mut self, identity: Identity) { |
| 467 |
self.identity = identity; |
| 468 |
} |
| 469 |
|
| 470 |
pub fn cols(&self) -> u16 { |
| 471 |
self.cols |
| 472 |
} |
| 473 |
|
| 474 |
pub fn rows(&self) -> u16 { |
| 475 |
self.rows |
| 476 |
} |
| 477 |
|
| 478 |
pub fn cursor(&self) -> Cursor { |
| 479 |
self.cursor |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
pub fn cursor_view_row(&self) -> Option<u16> { |
| 488 |
let r = self.cursor.row.checked_add(self.view_offset)?; |
| 489 |
(r < self.rows).then_some(r) |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
pub fn row(&self, r: u16) -> &[Cell] { |
| 499 |
if let Some(h) = self.history_row(r) { |
| 500 |
return &h.cells; |
| 501 |
} |
| 502 |
let start = self.row_start(self.live_row(r)); |
| 503 |
let end = start + self.cols as usize; |
| 504 |
&self.active_cells()[start..end] |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
pub(crate) fn push_cell_text(&self, cell: &Cell, out: &mut String) { |
| 516 |
out.push(match cell.c() { |
| 517 |
'\0' => ' ', |
| 518 |
c => c, |
| 519 |
}); |
| 520 |
out.extend(self.marks.get(cell.marks_id())); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
pub fn marks(&self, cell: &Cell) -> &[char] { |
| 530 |
self.marks.get(cell.marks_id()) |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
pub fn snap_col(&self, row: u16, col: u16) -> u16 { |
| 538 |
let cells = self.row(row); |
| 539 |
match cells.get(col as usize) { |
| 540 |
Some(c) if c.is_spacer() && col > 0 => col - 1, |
| 541 |
_ => col, |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
pub fn cursor_cols(&self) -> u16 { |
| 548 |
self.cursor_view_row() |
| 549 |
.and_then(|r| self.row(r).get(self.cursor.col as usize).map(Cell::cols)) |
| 550 |
.unwrap_or(1) |
| 551 |
} |
| 552 |
|
| 553 |
#[inline] |
| 554 |
fn invalidate_cur_row(&mut self) { |
| 555 |
self.cur_row_start = CUR_ROW_INVALID; |
| 556 |
} |
| 557 |
|
| 558 |
#[inline] |
| 559 |
fn recompute_style_words(&mut self) { |
| 560 |
self.pending_fg_word = encode_color(self.pending_fg) | encode_attrs(self.pending_attrs); |
| 561 |
self.pending_bg_word = encode_color(self.pending_bg); |
| 562 |
} |
| 563 |
|
| 564 |
fn place_char(&mut self, c: char) { |
| 565 |
|
| 566 |
|
| 567 |
let width = if self.cols < 2 { 1 } else { char_cols(c) }; |
| 568 |
if width == 0 { |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
self.amend_with_mark(c); |
| 573 |
return; |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
if self.cursor.wrap_next { |
| 578 |
|
| 579 |
|
| 580 |
self.set_row_wrapped(self.cursor.row, true); |
| 581 |
self.newline(); |
| 582 |
self.cursor.col = 0; |
| 583 |
self.cursor.wrap_next = false; |
| 584 |
} else if width == 2 && self.cursor.col + 1 >= self.cols { |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
let pad = self.cursor.col; |
| 591 |
self.set_row_wrapped(self.cursor.row, true); |
| 592 |
self.write_pad(pad); |
| 593 |
self.newline(); |
| 594 |
self.cursor.col = 0; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
let start = if self.cur_row_start == CUR_ROW_INVALID { |
| 599 |
let s = self.row_start(self.cursor.row) as u32; |
| 600 |
self.cur_row_start = s; |
| 601 |
s as usize |
| 602 |
} else { |
| 603 |
self.cur_row_start as usize |
| 604 |
}; |
| 605 |
let col = self.cursor.col as usize; |
| 606 |
let row_idx = self.cursor.row as usize; |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
self.heal_pair(start, col, width as usize); |
| 612 |
let cells: &mut [Cell] = if self.on_alt { |
| 613 |
&mut self.alt |
| 614 |
} else { |
| 615 |
&mut self.main |
| 616 |
}; |
| 617 |
if width == 2 { |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
let (lead, spacer) = Cell::wide_pair(c, self.pending_fg_word, self.pending_bg_word); |
| 622 |
cells[start + col] = lead; |
| 623 |
cells[start + col + 1] = spacer; |
| 624 |
self.row_dirty[row_idx] = true; |
| 625 |
} else { |
| 626 |
|
| 627 |
|
| 628 |
debug_assert!(start + col < cells.len()); |
| 629 |
debug_assert!(row_idx < self.row_dirty.len()); |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
#[allow(unsafe_code)] |
| 637 |
unsafe { |
| 638 |
let cell = cells.get_unchecked_mut(start + col); |
| 639 |
cell.c_raw = c as u32; |
| 640 |
cell.fg_word = self.pending_fg_word; |
| 641 |
|
| 642 |
|
| 643 |
cell.bg_word = self.pending_bg_word; |
| 644 |
*self.row_dirty.get_unchecked_mut(row_idx) = true; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
let last = self.cursor.col + width - 1; |
| 650 |
if last + 1 >= self.cols { |
| 651 |
self.cursor.col = last; |
| 652 |
self.cursor.wrap_next = true; |
| 653 |
} else { |
| 654 |
self.cursor.col = last + 1; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
fn heal_pair(&mut self, start: usize, col: usize, width: usize) { |
| 661 |
let cols = self.cols as usize; |
| 662 |
let cells = self.active_cells_mut(); |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
if cells[start + col].is_spacer() && col > 0 && cells[start + col - 1].is_wide() { |
| 675 |
cells[start + col - 1] = Cell::default(); |
| 676 |
} else if width == 1 && cells[start + col].is_wide() && col + 1 < cols { |
| 677 |
cells[start + col + 1] = Cell::default(); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
if width == 2 && col + 1 < cols && cells[start + col + 1].is_wide() && col + 2 < cols { |
| 682 |
cells[start + col + 2] = Cell::default(); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
fn amend_with_mark(&mut self, mark: char) { |
| 693 |
|
| 694 |
|
| 695 |
let Some(col) = (if self.cursor.wrap_next { |
| 696 |
Some(self.cursor.col) |
| 697 |
} else { |
| 698 |
self.cursor.col.checked_sub(1) |
| 699 |
}) else { |
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
return; |
| 704 |
}; |
| 705 |
let start = self.row_start(self.cursor.row); |
| 706 |
let cells = self.active_cells(); |
| 707 |
let col = if cells[start + col as usize].is_spacer() && col > 0 { |
| 708 |
col - 1 |
| 709 |
} else { |
| 710 |
col |
| 711 |
}; |
| 712 |
let base = cells[start + col as usize]; |
| 713 |
let mut seq: Vec<char> = self.marks.get(base.marks_id()).to_vec(); |
| 714 |
if seq.len() >= MAX_MARKS { |
| 715 |
return; |
| 716 |
} |
| 717 |
seq.push(mark); |
| 718 |
let Some(id) = self.marks.intern(&seq) else { |
| 719 |
|
| 720 |
|
| 721 |
return; |
| 722 |
}; |
| 723 |
let cells = self.active_cells_mut(); |
| 724 |
cells[start + col as usize] = base.with_marks(id); |
| 725 |
self.mark_row_dirty(self.cursor.row); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
fn write_pad(&mut self, col: u16) { |
| 731 |
let start = self.row_start(self.cursor.row); |
| 732 |
let cells = self.active_cells_mut(); |
| 733 |
cells[start + col as usize] = Cell::pad(); |
| 734 |
self.mark_row_dirty(self.cursor.row); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
#[cfg(test)] |
| 739 |
mod tests { |
| 740 |
use crate::testutil::{assert_cursor, feed, history_text, row_str}; |
| 741 |
use crate::*; |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
#[test] |
| 752 |
fn a_pad_at_the_right_edge_is_not_half_a_pair() { |
| 753 |
let mut g = Grid::new(10, 4); |
| 754 |
|
| 755 |
|
| 756 |
feed(&mut g, "\x1b[1;8H日日".as_bytes()); |
| 757 |
assert!(g.row(0)[7].is_wide()); |
| 758 |
assert!(g.row(0)[8].is_spacer()); |
| 759 |
assert!( |
| 760 |
g.row(0)[9].is_spacer(), |
| 761 |
"the column the wide character could not use" |
| 762 |
); |
| 763 |
assert!(!g.row(0)[9].is_wide()); |
| 764 |
|
| 765 |
feed(&mut g, b"\x1b[1;10Hx"); |
| 766 |
assert_eq!(g.row(0)[9].c(), 'x'); |
| 767 |
assert_eq!( |
| 768 |
g.row(0)[7].c(), |
| 769 |
'日', |
| 770 |
"the pair two columns back was blanked" |
| 771 |
); |
| 772 |
assert!(g.row(0)[7].is_wide()); |
| 773 |
assert!(g.row(0)[8].is_spacer(), "its spacer went with it"); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
#[test] |
| 779 |
fn new_grid_is_all_spaces() { |
| 780 |
let g = Grid::new(10, 3); |
| 781 |
for r in 0..3 { |
| 782 |
assert_eq!(row_str(&g, r), ""); |
| 783 |
} |
| 784 |
assert_cursor(&g, 0, 0); |
| 785 |
} |
| 786 |
|
| 787 |
#[test] |
| 788 |
fn print_advances_cursor() { |
| 789 |
let mut g = Grid::new(20, 3); |
| 790 |
feed(&mut g, b"hello"); |
| 791 |
assert_eq!(row_str(&g, 0), "hello"); |
| 792 |
assert_cursor(&g, 0, 5); |
| 793 |
} |
| 794 |
|
| 795 |
#[test] |
| 796 |
fn cr_lf_move_to_next_row_col_zero() { |
| 797 |
let mut g = Grid::new(20, 3); |
| 798 |
feed(&mut g, b"one\r\ntwo"); |
| 799 |
assert_eq!(row_str(&g, 0), "one"); |
| 800 |
assert_eq!(row_str(&g, 1), "two"); |
| 801 |
assert_cursor(&g, 1, 3); |
| 802 |
} |
| 803 |
|
| 804 |
#[test] |
| 805 |
fn backspace_moves_cursor_back() { |
| 806 |
let mut g = Grid::new(20, 3); |
| 807 |
feed(&mut g, b"abc\x08"); |
| 808 |
assert_cursor(&g, 0, 2); |
| 809 |
|
| 810 |
assert_eq!(row_str(&g, 0), "abc"); |
| 811 |
} |
| 812 |
|
| 813 |
#[test] |
| 814 |
fn tab_advances_to_next_multiple_of_eight() { |
| 815 |
let mut g = Grid::new(40, 3); |
| 816 |
feed(&mut g, b"ab\t"); |
| 817 |
assert_cursor(&g, 0, 8); |
| 818 |
feed(&mut g, b"c"); |
| 819 |
assert_cursor(&g, 0, 9); |
| 820 |
} |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
#[test] |
| 825 |
fn deferred_wrap_on_rightmost_cell() { |
| 826 |
let mut g = Grid::new(4, 3); |
| 827 |
feed(&mut g, b"ABCD"); |
| 828 |
|
| 829 |
assert_eq!(row_str(&g, 0), "ABCD"); |
| 830 |
let c = g.cursor(); |
| 831 |
assert!(c.wrap_next, "should have wrap_next set"); |
| 832 |
feed(&mut g, b"E"); |
| 833 |
|
| 834 |
assert_eq!(row_str(&g, 1), "E"); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
#[test] |
| 840 |
fn cup_moves_cursor_one_indexed() { |
| 841 |
let mut g = Grid::new(20, 5); |
| 842 |
feed(&mut g, b"\x1b[3;5H"); |
| 843 |
assert_cursor(&g, 2, 4); |
| 844 |
} |
| 845 |
|
| 846 |
#[test] |
| 847 |
fn cup_defaults_to_top_left() { |
| 848 |
let mut g = Grid::new(20, 5); |
| 849 |
feed(&mut g, b"aaa\r\nbbb\r\nccc\x1b[H"); |
| 850 |
assert_cursor(&g, 0, 0); |
| 851 |
} |
| 852 |
|
| 853 |
#[test] |
| 854 |
fn cuu_cud_cuf_cub_clamp_at_edges() { |
| 855 |
let mut g = Grid::new(20, 5); |
| 856 |
feed(&mut g, b"\x1b[100A"); |
| 857 |
assert_cursor(&g, 0, 0); |
| 858 |
feed(&mut g, b"\x1b[100B"); |
| 859 |
assert_cursor(&g, 4, 0); |
| 860 |
feed(&mut g, b"\x1b[100C"); |
| 861 |
assert_cursor(&g, 4, 19); |
| 862 |
feed(&mut g, b"\x1b[100D"); |
| 863 |
assert_cursor(&g, 4, 0); |
| 864 |
} |
| 865 |
|
| 866 |
#[test] |
| 867 |
fn cha_and_vpa_position_absolutely() { |
| 868 |
let mut g = Grid::new(20, 5); |
| 869 |
feed(&mut g, b"\x1b[10G\x1b[3d"); |
| 870 |
assert_cursor(&g, 2, 9); |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
#[test] |
| 883 |
fn a_wide_character_takes_two_columns() { |
| 884 |
let mut g = Grid::new(8, 2); |
| 885 |
feed(&mut g, "日x".as_bytes()); |
| 886 |
let cells = g.row(0); |
| 887 |
assert!( |
| 888 |
cells[0].is_wide(), |
| 889 |
"the lead does not claim its second column" |
| 890 |
); |
| 891 |
assert!(cells[1].is_spacer(), "the second column is not held"); |
| 892 |
assert_eq!(cells[2].c(), 'x', "the next character overlapped the pair"); |
| 893 |
assert_eq!( |
| 894 |
g.cursor().col, |
| 895 |
3, |
| 896 |
"the cursor is not where the program thinks" |
| 897 |
); |
| 898 |
} |
| 899 |
|
| 900 |
#[test] |
| 901 |
fn a_wide_character_reads_back_as_one_character() { |
| 902 |
|
| 903 |
|
| 904 |
let mut g = Grid::new(8, 2); |
| 905 |
feed(&mut g, "日本語".as_bytes()); |
| 906 |
assert_eq!(g.text_range(0, 1), "日本語\n"); |
| 907 |
} |
| 908 |
|
| 909 |
#[test] |
| 910 |
fn a_wide_character_that_does_not_fit_moves_to_the_next_row_whole() { |
| 911 |
|
| 912 |
|
| 913 |
let mut g = Grid::new(5, 3); |
| 914 |
feed(&mut g, "abcd日".as_bytes()); |
| 915 |
assert_eq!(row_str(&g, 0), "abcd", "the odd column was written into"); |
| 916 |
assert!( |
| 917 |
g.row(1)[0].is_wide(), |
| 918 |
"the character did not move down whole" |
| 919 |
); |
| 920 |
assert!(g.row_wrapped(0), "the line stopped continuing"); |
| 921 |
assert_eq!( |
| 922 |
g.text_range(0, 2), |
| 923 |
"abcd日\n", |
| 924 |
"the column it could not use came out as a space" |
| 925 |
); |
| 926 |
} |
| 927 |
|
| 928 |
#[test] |
| 929 |
fn overwriting_half_a_wide_character_takes_the_other_half_with_it() { |
| 930 |
|
| 931 |
|
| 932 |
let mut g = Grid::new(6, 2); |
| 933 |
feed(&mut g, "日本".as_bytes()); |
| 934 |
feed(&mut g, b"\x1b[1;1Hx"); |
| 935 |
assert_eq!(row_str(&g, 0), "x 本", "the orphaned spacer survived"); |
| 936 |
assert!(!g.row(0)[1].is_spacer()); |
| 937 |
|
| 938 |
let mut g = Grid::new(6, 2); |
| 939 |
feed(&mut g, "日本".as_bytes()); |
| 940 |
feed(&mut g, b"\x1b[1;2Hx"); |
| 941 |
assert_eq!(row_str(&g, 0), " x本", "the orphaned lead survived"); |
| 942 |
assert!(!g.row(0)[0].is_wide()); |
| 943 |
} |
| 944 |
|
| 945 |
#[test] |
| 946 |
fn a_wide_character_written_over_a_pair_clears_the_pair_it_overlaps() { |
| 947 |
let mut g = Grid::new(6, 2); |
| 948 |
feed(&mut g, "日本".as_bytes()); |
| 949 |
|
| 950 |
feed(&mut g, "\x1b[1;2H語".as_bytes()); |
| 951 |
assert_eq!(row_str(&g, 0), " 語", "a half of the old pair survived"); |
| 952 |
assert!(!g.row(0)[0].is_wide()); |
| 953 |
assert!(!g.row(0)[3].is_spacer()); |
| 954 |
} |
| 955 |
|
| 956 |
#[test] |
| 957 |
fn erasing_through_half_a_wide_character_takes_the_other_half() { |
| 958 |
let mut g = Grid::new(6, 2); |
| 959 |
feed(&mut g, "ab日x".as_bytes()); |
| 960 |
|
| 961 |
feed(&mut g, b"\x1b[1;4H\x1b[K"); |
| 962 |
assert_eq!( |
| 963 |
row_str(&g, 0), |
| 964 |
"ab", |
| 965 |
"the lead was left claiming two columns" |
| 966 |
); |
| 967 |
assert!(!g.row(0)[2].is_wide()); |
| 968 |
} |
| 969 |
|
| 970 |
#[test] |
| 971 |
fn a_one_column_grid_holds_a_wide_character_as_one_column() { |
| 972 |
|
| 973 |
|
| 974 |
let mut g = Grid::new(1, 2); |
| 975 |
feed(&mut g, "日".as_bytes()); |
| 976 |
assert_eq!(g.row(0)[0].c(), '日'); |
| 977 |
assert!(!g.row(0)[0].is_wide()); |
| 978 |
} |
| 979 |
|
| 980 |
#[test] |
| 981 |
fn narrowing_then_widening_gives_wide_characters_back() { |
| 982 |
|
| 983 |
|
| 984 |
let mut g = Grid::new(10, 2); |
| 985 |
feed(&mut g, "日本語です\r\nabc\r\nx".as_bytes()); |
| 986 |
let before = history_text(&g); |
| 987 |
g.resize(4, 2); |
| 988 |
g.resize(10, 2); |
| 989 |
assert_eq!(history_text(&g), before); |
| 990 |
} |
| 991 |
|
| 992 |
#[test] |
| 993 |
fn a_rewrap_does_not_split_a_wide_character() { |
| 994 |
|
| 995 |
|
| 996 |
let mut g = Grid::new(10, 2); |
| 997 |
feed(&mut g, "日本語です\r\nx\r\ny".as_bytes()); |
| 998 |
g.resize(5, 2); |
| 999 |
for r in 0..g.history_len() as u16 { |
| 1000 |
let cells = g.row(r); |
| 1001 |
assert!( |
| 1002 |
!cells[cells.len() - 1].is_wide(), |
| 1003 |
"row {r} ends on half a character" |
| 1004 |
); |
| 1005 |
assert!(!cells[0].is_spacer(), "row {r} starts on half a character"); |
| 1006 |
} |
| 1007 |
assert!( |
| 1008 |
history_text(&g).starts_with("日本語です"), |
| 1009 |
"the line did not survive the rewrap: {:?}", |
| 1010 |
history_text(&g) |
| 1011 |
); |
| 1012 |
} |
| 1013 |
|
| 1014 |
#[test] |
| 1015 |
fn a_rewrap_does_not_carry_the_old_widths_pad_columns() { |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
let mut g = Grid::new(5, 2); |
| 1020 |
feed(&mut g, "abcd日本\r\nx\r\ny".as_bytes()); |
| 1021 |
g.resize(6, 2); |
| 1022 |
assert!( |
| 1023 |
history_text(&g).starts_with("abcd日本"), |
| 1024 |
"a stale pad column survived: {:?}", |
| 1025 |
history_text(&g) |
| 1026 |
); |
| 1027 |
} |
| 1028 |
|
| 1029 |
#[test] |
| 1030 |
fn narrowing_the_live_screen_does_not_leave_half_a_character() { |
| 1031 |
|
| 1032 |
|
| 1033 |
let mut g = Grid::new(6, 2); |
| 1034 |
feed(&mut g, "ab日".as_bytes()); |
| 1035 |
g.resize(3, 2); |
| 1036 |
assert!(!g.row(0)[2].is_wide(), "a lead survived without its spacer"); |
| 1037 |
} |
| 1038 |
|
| 1039 |
#[test] |
| 1040 |
fn a_wide_character_is_one_word_for_a_double_click() { |
| 1041 |
|
| 1042 |
|
| 1043 |
let g = { |
| 1044 |
let mut g = Grid::new(10, 2); |
| 1045 |
feed(&mut g, "日本語 x".as_bytes()); |
| 1046 |
g |
| 1047 |
}; |
| 1048 |
let sel = Selection::new(SelectionMode::Word, Point::new(0, 0)); |
| 1049 |
assert_eq!(g.selection_text(&sel), "日本語"); |
| 1050 |
} |
| 1051 |
|
| 1052 |
#[test] |
| 1053 |
fn a_selection_stopping_on_half_a_character_still_copies_the_character() { |
| 1054 |
let mut g = Grid::new(10, 2); |
| 1055 |
feed(&mut g, "日本".as_bytes()); |
| 1056 |
|
| 1057 |
let mut sel = Selection::new(SelectionMode::Char, Point::new(0, 0)); |
| 1058 |
sel.drag_to(Point::new(0, 2)); |
| 1059 |
assert_eq!(g.selection_text(&sel), "日本"); |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
#[test] |
| 1070 |
fn a_combining_mark_takes_no_column_of_its_own() { |
| 1071 |
let mut g = Grid::new(8, 2); |
| 1072 |
feed(&mut g, "e\u{301}x".as_bytes()); |
| 1073 |
assert_eq!(g.cursor().col, 2, "the mark consumed a column"); |
| 1074 |
assert_eq!(g.row(0)[0].c(), 'e', "the base is not inline any more"); |
| 1075 |
assert_eq!( |
| 1076 |
g.row(0)[1].c(), |
| 1077 |
'x', |
| 1078 |
"the mark displaced the next character" |
| 1079 |
); |
| 1080 |
} |
| 1081 |
|
| 1082 |
#[test] |
| 1083 |
fn a_combining_mark_copies_back_with_its_base() { |
| 1084 |
|
| 1085 |
|
| 1086 |
let mut g = Grid::new(20, 2); |
| 1087 |
feed(&mut g, "Jose\u{301}/".as_bytes()); |
| 1088 |
assert_eq!(g.text_range(0, 1), "Jose\u{301}/\n"); |
| 1089 |
} |
| 1090 |
|
| 1091 |
#[test] |
| 1092 |
fn several_marks_stack_on_one_base() { |
| 1093 |
let mut g = Grid::new(8, 2); |
| 1094 |
feed(&mut g, "o\u{323}\u{302}".as_bytes()); |
| 1095 |
assert_eq!(g.cursor().col, 1); |
| 1096 |
assert_eq!(g.text_range(0, 1), "o\u{323}\u{302}\n"); |
| 1097 |
} |
| 1098 |
|
| 1099 |
#[test] |
| 1100 |
fn a_mark_after_a_wide_character_lands_on_the_character_not_its_spacer() { |
| 1101 |
let mut g = Grid::new(8, 2); |
| 1102 |
feed(&mut g, "日\u{301}".as_bytes()); |
| 1103 |
assert_eq!(g.row(0)[0].marks_id(), 1, "the mark missed the lead"); |
| 1104 |
assert_eq!(g.row(0)[1].marks_id(), 0, "the spacer took the mark"); |
| 1105 |
assert_eq!(g.text_range(0, 1), "日\u{301}\n"); |
| 1106 |
} |
| 1107 |
|
| 1108 |
#[test] |
| 1109 |
fn a_mark_at_the_right_edge_amends_the_character_still_under_the_cursor() { |
| 1110 |
|
| 1111 |
|
| 1112 |
let mut g = Grid::new(4, 2); |
| 1113 |
feed(&mut g, "abcd\u{301}".as_bytes()); |
| 1114 |
assert_eq!(g.text_range(0, 1), "abcd\u{301}\n"); |
| 1115 |
assert_eq!(g.cursor().col, 3, "the mark moved the cursor off the edge"); |
| 1116 |
} |
| 1117 |
|
| 1118 |
#[test] |
| 1119 |
fn a_mark_with_nothing_before_it_is_dropped() { |
| 1120 |
|
| 1121 |
|
| 1122 |
let mut g = Grid::new(8, 2); |
| 1123 |
feed(&mut g, "\u{301}x".as_bytes()); |
| 1124 |
assert_eq!(g.cursor().col, 1); |
| 1125 |
assert_eq!(g.text_range(0, 1), "x\n"); |
| 1126 |
} |
| 1127 |
|
| 1128 |
#[test] |
| 1129 |
fn marks_survive_a_rewrap() { |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
let mut g = Grid::new(10, 2); |
| 1134 |
feed(&mut g, "abcde\u{301}fghij\r\nx\r\ny".as_bytes()); |
| 1135 |
let before = history_text(&g); |
| 1136 |
assert!(before.contains('\u{301}')); |
| 1137 |
g.resize(4, 2); |
| 1138 |
g.resize(10, 2); |
| 1139 |
assert_eq!(history_text(&g), before); |
| 1140 |
} |
| 1141 |
|
| 1142 |
#[test] |
| 1143 |
fn overwriting_a_base_drops_the_marks_that_were_on_it() { |
| 1144 |
|
| 1145 |
let mut g = Grid::new(8, 2); |
| 1146 |
feed(&mut g, "e\u{301}".as_bytes()); |
| 1147 |
feed(&mut g, b"\x1b[1;1Hx"); |
| 1148 |
assert_eq!(g.text_range(0, 1), "x\n"); |
| 1149 |
assert_eq!(g.row(0)[0].marks_id(), 0); |
| 1150 |
} |
| 1151 |
|
| 1152 |
#[test] |
| 1153 |
fn the_same_mark_sequence_is_interned_once() { |
| 1154 |
|
| 1155 |
|
| 1156 |
let mut g = Grid::new(20, 2); |
| 1157 |
feed( |
| 1158 |
&mut g, |
| 1159 |
"a\u{301}e\u{301}i\u{301}o\u{301}u\u{301}".as_bytes(), |
| 1160 |
); |
| 1161 |
let ids: Vec<u16> = (0..5).map(|c| g.row(0)[c].marks_id()).collect(); |
| 1162 |
assert_eq!(ids, vec![1; 5], "one sequence took five ids"); |
| 1163 |
} |
| 1164 |
|
| 1165 |
#[test] |
| 1166 |
fn a_cell_stops_taking_marks_at_the_cap() { |
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
let mut g = Grid::new(8, 2); |
| 1171 |
let mut bytes = String::from("e"); |
| 1172 |
for _ in 0..MAX_MARKS + 5 { |
| 1173 |
bytes.push('\u{301}'); |
| 1174 |
} |
| 1175 |
feed(&mut g, bytes.as_bytes()); |
| 1176 |
let text = g.text_range(0, 1); |
| 1177 |
assert_eq!( |
| 1178 |
text.chars().filter(|c| *c == '\u{301}').count(), |
| 1179 |
MAX_MARKS, |
| 1180 |
"the cap did not hold" |
| 1181 |
); |
| 1182 |
} |
| 1183 |
|
| 1184 |
#[test] |
| 1185 |
fn a_marked_character_is_one_word_for_a_double_click() { |
| 1186 |
|
| 1187 |
let mut g = Grid::new(20, 2); |
| 1188 |
feed(&mut g, "Jose\u{301} x".as_bytes()); |
| 1189 |
let sel = Selection::new(SelectionMode::Word, Point::new(0, 0)); |
| 1190 |
assert_eq!(g.selection_text(&sel), "Jose\u{301}"); |
| 1191 |
} |
| 1192 |
|
| 1193 |
#[test] |
| 1194 |
fn invalidate_render_reports_every_row_however_quiet_the_grid_is() { |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
let mut g = Grid::new(8, 4); |
| 1200 |
feed(&mut g, b"one\r\ntwo"); |
| 1201 |
let _ = g.take_damage(); |
| 1202 |
assert!( |
| 1203 |
g.take_damage().dirty_rows.is_empty(), |
| 1204 |
"a quiet grid still had damage to report" |
| 1205 |
); |
| 1206 |
g.invalidate_render(); |
| 1207 |
let d = g.take_damage(); |
| 1208 |
assert_eq!(d.dirty_rows.len(), 4, "not every row came back"); |
| 1209 |
assert!(d.view_moved, "the viewport was not invalidated with them"); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|