1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! Viewport helper — ports `@clack/prompts` `limitOptions()`.
//!
//! Slides a window of size `max` over a list so the active cursor stays
//! visible. Returns `(start, end)` half-open indices, plus flags telling
//! callers whether to render `↑ N more` / `↓ N more` indicators.
pub struct Viewport {
pub start: usize,
pub end: usize,
pub above: usize,
pub below: usize,
}
pub fn limit_options(total: usize, cursor: usize, max: usize) -> Viewport {
if total <= max {
return Viewport {
start: 0,
end: total,
above: 0,
below: 0,
};
}
let half = max / 2;
let start = if cursor < half {
0
} else if cursor + (max - half) > total {
total - max
} else {
cursor - half
};
let end = (start + max).min(total);
Viewport {
start,
end,
above: start,
below: total - end,
}
}