Skip to main content

justerm_core/
damage.rs

1//! Damage tracking — what changed since the last reset, as line + column spans.
2//! See ADR-0003 for the model (incremental bounds, ack-gated reset).
3
4/// The damaged column span of a single line.
5#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6pub struct LineDamage {
7    pub line: usize,
8    pub left: usize,
9    pub right: usize,
10}
11
12/// A first-class scroll: rows `[top..=bottom]` shifted by `count` lines
13/// (positive = up, negative = down). The renderer moves the rows instead of
14/// redrawing them. Recorded by the engine — which executes the scroll — rather
15/// than diff-detected (ADR-0003).
16///
17/// **A reported `count` never exceeds `bottom - top + 1`** — see
18/// [`crate::Engine::scroll_delta`], which caps it. `count` is `isize` here and
19/// `i16` on the wire, so an uncapped accumulation overflowed the field and
20/// reversed the shift's direction (#661); the bound is also the point past which
21/// the value stops meaning anything, since every source row is then outside the
22/// region. The cap is applied when the op is *read*, not while it accumulates, so
23/// a region that scrolls far and returns still reports its true small net.
24#[derive(Clone, Copy, PartialEq, Eq, Debug)]
25pub struct ScrollOp {
26    pub top: usize,
27    pub bottom: usize,
28    pub count: isize,
29}
30
31/// What changed since the last `reset_damage()`.
32#[derive(Clone, PartialEq, Eq, Debug)]
33pub enum TermDamage {
34    /// The whole screen must be redrawn (flood / resize / alt-screen clear).
35    Full,
36    /// Only these lines changed, each carrying its damaged column span.
37    Partial(Vec<LineDamage>),
38}
39
40/// Per-line damage bounds. "Undamaged" is encoded as `left > right`, so an
41/// untouched line never reports as damaged and the first `expand` sets a real
42/// span. (Mirrors Alacritty's `LineDamageBounds`.)
43#[derive(Clone, Copy)]
44pub(crate) struct LineBounds {
45    left: usize,
46    right: usize,
47    cols: usize,
48}
49
50impl LineBounds {
51    pub(crate) fn undamaged(cols: usize) -> Self {
52        LineBounds {
53            left: cols,
54            right: 0,
55            cols,
56        }
57    }
58
59    /// A line damaged across its full width (a newly exposed scroll line).
60    pub(crate) fn fully_damaged(cols: usize) -> Self {
61        LineBounds {
62            left: 0,
63            right: cols.saturating_sub(1),
64            cols,
65        }
66    }
67
68    /// Widen the span to include columns `[left, right]`.
69    pub(crate) fn expand(&mut self, left: usize, right: usize) {
70        self.left = self.left.min(left);
71        self.right = self.right.max(right);
72    }
73
74    pub(crate) fn is_damaged(&self) -> bool {
75        self.left <= self.right
76    }
77
78    pub(crate) fn reset(&mut self) {
79        self.left = self.cols;
80        self.right = 0;
81    }
82
83    pub(crate) fn span(&self) -> (usize, usize) {
84        (self.left, self.right)
85    }
86}