Files
hx/crates/hx-tui/src/scroll.rs
T
johannesandClaude Sonnet 5.5 a94632ea3d chore: delete code comments, add ratchet lint and ADRs
Classify every comment as tautology, stale, excuse or decision record.
Tautology/stale/excuse comments are deleted (excuses filed as issues
#196-#236), decisions moved to docs/adr/0001-0034.

- scripts/lint-comments.sh + .comment-baseline: per-file comment count
  may only go down; wired into tests/all.sh
- .claude/skills/delete-comment: repeatable audit workflow
- cargo fmt applied to touched crates

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 13:37:32 +01:00

90 lines
2.3 KiB
Rust

#[derive(Default, Debug, Clone, PartialEq)]
pub struct Scroll {
top: Option<u16>,
total: u16,
height: u16,
}
impl Scroll {
fn max(&self) -> u16 {
self.total.saturating_sub(self.height)
}
fn cur(&self) -> u16 {
self.top.unwrap_or(self.max()).min(self.max())
}
pub fn resolve(&mut self, total: u16, height: u16) -> u16 {
self.total = total;
self.height = height;
if self.top.is_some_and(|t| t >= self.max()) {
self.top = None;
}
self.cur()
}
pub fn up(&mut self, n: u16) {
self.top = Some(self.cur().saturating_sub(n));
}
pub fn down(&mut self, n: u16) {
self.top = Some(self.cur().saturating_add(n));
if self.top.is_some_and(|t| t >= self.max()) {
self.top = None;
}
}
pub fn top(&self) -> u16 {
self.cur()
}
pub fn half_page(&self) -> u16 {
(self.height / 2).max(1)
}
pub fn page(&self) -> u16 {
self.height.saturating_sub(1).max(1)
}
pub fn start(&mut self) {
self.top = Some(0);
}
pub fn end(&mut self) {
self.top = None;
}
pub fn behind(&self) -> u16 {
self.max() - self.cur()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn follows_bottom_until_scrolled_and_pins_while_content_grows() {
let mut s = Scroll::default();
assert_eq!(s.resolve(100, 20), 80);
s.up(10);
assert_eq!((s.resolve(100, 20), s.behind()), (70, 10));
assert_eq!((s.resolve(130, 20), s.behind()), (70, 40));
s.down(1000);
assert_eq!((s.resolve(130, 20), s.behind()), (110, 0));
assert_eq!(s.resolve(140, 20), 120);
}
#[test]
fn paging_clamping_start_end() {
let mut s = Scroll::default();
s.resolve(50, 10);
assert_eq!((s.half_page(), s.page()), (5, 9));
s.up(s.page());
assert_eq!(s.resolve(50, 10), 31);
s.start();
assert_eq!(s.resolve(50, 10), 0);
s.up(5);
assert_eq!(s.resolve(50, 10), 0, "no underflow");
s.end();
assert_eq!(s.resolve(50, 10), 40);
let mut t = Scroll::default();
t.up(5);
assert_eq!((t.resolve(3, 10), t.behind()), (0, 0));
t.down(5);
assert_eq!(t.resolve(3, 10), 0);
}
}