Files
codex/prs/bolinfest/study/PR-1732-study.md
2025-09-02 15:17:45 -07:00

65 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
**DOs**
- **Comment Rationale**: Add a brief comment when using lowlevel cursor ops that deliberately bypass `Terminal` state.
```rust
// Using MoveTo instead of terminal.set_cursor_position to avoid mutating
// last_known_cursor_pos, which the viewport logic relies on.
queue!(stdout, MoveTo(0, cursor_top))?;
```
- **Use `MoveTo` For Ephemeral Moves**: Temporarily reposition the cursor with `crossterm::queue!` so `last_known_cursor_pos` stays accurate.
```rust
use crossterm::{queue, cursor::MoveTo};
use std::io::{self, Write};
let mut stdout = io::stdout();
queue!(stdout, MoveTo(0, cursor_top))?;
```
- **Preserve CursorPosition Neutrality**: Capture and restore the cursor position around history insertion.
```rust
let saved = terminal.get_cursor_position().ok();
// ... emit lines above viewport ...
if let Some(pos) = saved {
queue!(stdout, MoveTo(pos.x, pos.y))?;
}
```
- **Bracket Scroll Region Changes**: Always pair `SetScrollRegion` with `ResetScrollRegion`.
```rust
use crossterm::terminal::{SetScrollRegion, ResetScrollRegion};
queue!(stdout, SetScrollRegion(1..area.top()))?;
// ... writes that rely on the scroll region ...
queue!(stdout, ResetScrollRegion)?;
```
- **Keep Comments Close To The Action**: Place the explanatory comment immediately above the `MoveTo`/restore call so intent is unmistakable.
```rust
// insert_history_lines should be cursor-position-neutral.
queue!(stdout, MoveTo(pos.x, pos.y))?;
```
**DONTs**
- **Dont Mutate Terminal State For Temporary Moves**: Avoid `terminal.set_cursor_position(...)` when you dont intend to change `last_known_cursor_pos`.
```rust
// ❌ Avoid: this updates last_known_cursor_pos and can desync viewport math.
terminal.set_cursor_position((0, cursor_top))?;
```
- **Dont Forget To Restore The Cursor**: Leaving the cursor at the injection point can confuse subsequent rendering.
```rust
// ❌ Missing restore leaves cursor in the wrong place
// Do: capture -> write -> restore (see DOs above).
```
- **Dont Leave The Scroll Region Dirty**: Failing to reset it affects all future draws.
```rust
// ❌ Missing ResetScrollRegion
queue!(stdout, SetScrollRegion(1..area.top()))?;
// Do: always follow with ResetScrollRegion.
```
- **Dont Skip Explaining NonObvious Choices**: If you bypass higherlevel APIs (e.g., `Terminal::set_cursor_position`), document why.
```rust
// ❌ No comment explaining why low-level cursor ops are used
// Do: add a one-liner clarifying the state-preservation intent.
```