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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Scroll-offset clamping for the messages panel.
//!
//! The chat [`Paragraph`] is rendered without `Wrap`, so ratatui draws
//! exactly one terminal row per [`Line`]. Total height is therefore just
//! `lines.len()` — no per-line width arithmetic required.
//!
//! [`Paragraph`]: ratatui::widgets::Paragraph
use Rect;
use Line;
use crateApp;
/// Sentinel meaning "follow latest content" (auto-scroll).
///
/// ```rust
/// use codetether_agent::tui::ui::chat_view::scroll::SCROLL_BOTTOM;
/// assert!(SCROLL_BOTTOM > 999_999);
/// ```
pub const SCROLL_BOTTOM: usize = 1_000_000;
/// No-op retained for API compatibility with an earlier memo-based
/// revision of the scroll-height calculator.
///
/// The current implementation recomputes the total in O(1) from
/// `lines.len()`, so there is no memo to reset. Callers that still invoke
/// this function continue to compile and behave identically.
///
/// # Examples
///
/// ```rust
/// use codetether_agent::tui::ui::chat_view::scroll::reset_height_memo;
/// reset_height_memo(); // always safe, never fails
/// ```
/// Clamp scroll offset so the visible slice of `lines` stays within
/// `messages_rect` and honor the `SCROLL_BOTTOM` follow-tail sentinel.
///
/// The chat `Paragraph` is rendered without wrapping (see module docs),
/// so the total rendered height equals `lines.len()` and no per-line
/// width arithmetic is needed.
///
/// # Arguments
///
/// * `app` — mutable app state; `chat_scroll` is read and the computed
/// `max_scroll` is written via `AppState::set_chat_max_scroll`.
/// * `messages_rect` — the panel rect including borders; interior height
/// is `height - 2`.
/// * `lines` — the rendered chat lines for this frame.
///
/// # Returns
///
/// The clamped scroll offset suitable for `Paragraph::scroll((offset, 0))`.
/// Saturates at `u16::MAX` for safety on very tall buffers.
///
/// # Examples
///
/// ```rust,no_run
/// # use codetether_agent::tui::ui::chat_view::scroll::clamp_scroll;
/// # fn d(a:&mut codetether_agent::tui::app::state::App){
/// let r = clamp_scroll(a, ratatui::layout::Rect::new(0, 0, 80, 24), &vec![]);
/// assert_eq!(r, 0);
/// # }
/// ```