1use std::{collections::VecDeque, sync::Arc, time::Instant};
2
3#[cfg(test)]
4use anstyle::Color as AnsiColorEnum;
5use anstyle::RgbColor;
6use ratatui::crossterm::event::{
7 Event as CrosstermEvent, KeyCode, KeyEvent, KeyEventKind, MouseEvent, MouseEventKind,
8};
9
10use ratatui::{
11 Frame,
12 layout::{Constraint, Layout, Rect},
13 text::{Line, Span, Text},
14 widgets::{Clear, ListState, Widget},
15};
16use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
17
18use super::{
19 style::{measure_text_width, ratatui_color_from_ansi, ratatui_style_from_inline},
20 types::{
21 DiffPreviewState, InlineCommand, InlineEvent, InlineHeaderContext, InlineMessageKind,
22 InlineTextStyle, InlineTheme, OverlayRequest,
23 },
24};
25use crate::config::constants::ui;
26use crate::ui::tui::widgets::SessionWidget;
27
28pub mod file_palette;
29mod header;
30mod impl_events;
31mod impl_init;
32mod impl_input;
33mod impl_layout;
34mod impl_logs;
35mod impl_render;
36mod impl_scroll;
37mod impl_style;
38mod inline_list;
39mod input;
40mod input_manager;
41mod list_panel;
42mod message;
43pub mod modal;
44pub mod mouse_selection;
45mod navigation;
46mod queue;
47pub mod render;
48mod scroll;
49pub mod slash;
50pub mod slash_palette;
51pub mod styling;
52mod text_utils;
53mod transcript;
54pub mod utils;
55pub mod wrapping;
56
57mod command;
59mod editing;
60
61pub mod config;
62mod diff_preview;
63mod events;
64pub mod history_picker;
65mod message_renderer;
66mod messages;
67mod palette;
68mod reflow;
69mod reverse_search;
70mod spinner;
71mod state;
72pub mod terminal_capabilities;
73mod terminal_title;
74#[cfg(test)]
75mod tests;
76mod tool_renderer;
77mod transcript_links;
78mod trust;
79
80use self::file_palette::FilePalette;
81use self::history_picker::HistoryPickerState;
82use self::input_manager::InputManager;
83use self::message::{MessageLabels, MessageLine};
84use self::modal::{ModalState, WizardModalState};
85
86use self::config::AppearanceConfig;
87pub(crate) use self::input::status_requires_shimmer;
88use self::mouse_selection::MouseSelectionState;
89use self::queue::QueueOverlay;
90use self::scroll::ScrollManager;
91use self::slash_palette::SlashPalette;
92use self::spinner::{ShimmerState, ThinkingSpinner};
93use self::styling::SessionStyles;
94use self::transcript::TranscriptReflowCache;
95use self::transcript_links::TranscriptFileLinkTarget;
96#[cfg(test)]
97use super::types::InlineHeaderHighlight;
98use crate::ui::tui::log::{LogEntry, highlight_log_entry};
100
101const USER_PREFIX: &str = "";
102const PLACEHOLDER_COLOR: RgbColor =
103 RgbColor(ui::PLACEHOLDER_R, ui::PLACEHOLDER_G, ui::PLACEHOLDER_B);
104const MAX_LOG_LINES: usize = 256;
105const MAX_LOG_DRAIN_PER_TICK: usize = 256;
106
107#[derive(Clone, Debug)]
108struct CollapsedPaste {
109 line_index: usize,
110 full_text: String,
111}
112
113pub(crate) enum ActiveOverlay {
114 Modal(Box<ModalState>),
115 Wizard(Box<WizardModalState>),
116 Diff(Box<DiffPreviewState>),
117}
118
119impl ActiveOverlay {
120 fn as_modal(&self) -> Option<&ModalState> {
121 match self {
122 Self::Modal(state) => Some(state),
123 Self::Wizard(_) | Self::Diff(_) => None,
124 }
125 }
126
127 fn as_modal_mut(&mut self) -> Option<&mut ModalState> {
128 match self {
129 Self::Modal(state) => Some(state),
130 Self::Wizard(_) | Self::Diff(_) => None,
131 }
132 }
133
134 fn as_wizard(&self) -> Option<&WizardModalState> {
135 match self {
136 Self::Wizard(state) => Some(state),
137 Self::Modal(_) | Self::Diff(_) => None,
138 }
139 }
140
141 fn as_wizard_mut(&mut self) -> Option<&mut WizardModalState> {
142 match self {
143 Self::Wizard(state) => Some(state),
144 Self::Modal(_) | Self::Diff(_) => None,
145 }
146 }
147
148 fn as_diff(&self) -> Option<&DiffPreviewState> {
149 match self {
150 Self::Diff(state) => Some(state),
151 Self::Modal(_) | Self::Wizard(_) => None,
152 }
153 }
154
155 fn as_diff_mut(&mut self) -> Option<&mut DiffPreviewState> {
156 match self {
157 Self::Diff(state) => Some(state),
158 Self::Modal(_) | Self::Wizard(_) => None,
159 }
160 }
161
162 fn is_modal_like(&self) -> bool {
163 matches!(self, Self::Modal(_) | Self::Wizard(_))
164 }
165
166 fn restore_input(&self) -> bool {
167 match self {
168 Self::Modal(state) => state.restore_input,
169 Self::Wizard(_) | Self::Diff(_) => true,
170 }
171 }
172
173 fn restore_cursor(&self) -> bool {
174 match self {
175 Self::Modal(state) => state.restore_cursor,
176 Self::Wizard(_) | Self::Diff(_) => true,
177 }
178 }
179}
180
181pub struct Session {
182 pub(crate) input_manager: InputManager,
185 pub(crate) scroll_manager: ScrollManager,
187 user_scrolled: bool,
188
189 pub(crate) lines: Vec<MessageLine>,
191 collapsed_pastes: Vec<CollapsedPaste>,
192 pub(crate) theme: InlineTheme,
193 pub(crate) styles: SessionStyles,
194 pub(crate) appearance: AppearanceConfig,
195 pub(crate) header_context: InlineHeaderContext,
196 pub(crate) header_rows: u16,
197 pub(crate) labels: MessageLabels,
198
199 prompt_prefix: String,
201 prompt_style: InlineTextStyle,
202 placeholder: Option<String>,
203 placeholder_style: Option<InlineTextStyle>,
204 pub(crate) input_status_left: Option<String>,
205 pub(crate) input_status_right: Option<String>,
206 input_compact_mode: bool,
207
208 slash_palette: SlashPalette,
210 #[allow(dead_code)]
211 navigation_state: ListState,
212 input_enabled: bool,
213 cursor_visible: bool,
214 pub(crate) needs_redraw: bool,
215 pub(crate) needs_full_clear: bool,
216 pub(crate) transcript_content_changed: bool,
218 should_exit: bool,
219 scroll_cursor_steady_until: Option<Instant>,
220 last_shimmer_active: bool,
221 pub(crate) view_rows: u16,
222 pub(crate) input_height: u16,
223 pub(crate) transcript_rows: u16,
224 pub(crate) transcript_width: u16,
225 pub(crate) transcript_view_top: usize,
226 transcript_area: Option<Rect>,
227 input_area: Option<Rect>,
228 transcript_file_link_targets: Vec<TranscriptFileLinkTarget>,
229 hovered_transcript_file_link: Option<usize>,
230 last_mouse_position: Option<(u16, u16)>,
231
232 log_receiver: Option<UnboundedReceiver<LogEntry>>,
234 log_lines: VecDeque<Arc<Text<'static>>>,
235 log_cached_text: Option<Arc<Text<'static>>>,
236 log_evicted: bool,
237 pub(crate) show_logs: bool,
238
239 transcript_cache: Option<TranscriptReflowCache>,
241 pub(crate) visible_lines_cache: Option<(usize, u16, Arc<Vec<Line<'static>>>)>,
244 pub(crate) queued_inputs: Vec<String>,
245 queue_overlay_cache: Option<QueueOverlay>,
246 queue_overlay_version: u64,
247 active_overlay: Option<ActiveOverlay>,
248 overlay_queue: VecDeque<OverlayRequest>,
249 line_revision_counter: u64,
250 first_dirty_line: Option<usize>,
252 in_tool_code_fence: bool,
253
254 pub(crate) file_palette: Option<FilePalette>,
256 pub(crate) file_palette_active: bool,
257 pub(crate) inline_lists_visible: bool,
258
259 pub(crate) thinking_spinner: ThinkingSpinner,
261 pub(crate) shimmer_state: ShimmerState,
262
263 pub(crate) reverse_search_state: reverse_search::ReverseSearchState,
265
266 pub(crate) history_picker_state: HistoryPickerState,
268
269 pub(crate) active_pty_sessions: Option<Arc<std::sync::atomic::AtomicUsize>>,
271
272 #[allow(dead_code)]
274 pub(crate) clipboard: String,
275
276 pub(crate) mouse_selection: MouseSelectionState,
278
279 pub(crate) skip_confirmations: bool,
280
281 pub(crate) header_lines_cache: Option<Vec<Line<'static>>>,
283 pub(crate) header_height_cache: hashbrown::HashMap<u16, u16>,
284 pub(crate) queued_inputs_preview_cache: Option<Vec<String>>,
285
286 pub(crate) app_name: String,
289 pub(crate) workspace_root: Option<std::path::PathBuf>,
291 last_terminal_title: Option<String>,
293
294 pub(crate) is_streaming_final_answer: bool,
299}