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 InlineCommand, InlineEvent, InlineHeaderContext, InlineMessageKind, InlineTextStyle,
22 InlineTheme,
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 trust;
78
79use self::file_palette::FilePalette;
80use self::history_picker::HistoryPickerState;
81use self::input_manager::InputManager;
82use self::message::{MessageLabels, MessageLine};
83use self::modal::{ModalState, WizardModalState};
84
85use self::config::AppearanceConfig;
86pub(crate) use self::input::status_requires_shimmer;
87use self::mouse_selection::MouseSelectionState;
88use self::queue::QueueOverlay;
89use self::scroll::ScrollManager;
90use self::slash_palette::SlashPalette;
91use self::spinner::{ShimmerState, ThinkingSpinner};
92use self::styling::SessionStyles;
93use self::transcript::TranscriptReflowCache;
94#[cfg(test)]
95use super::types::InlineHeaderHighlight;
96use crate::ui::tui::log::{LogEntry, highlight_log_entry};
98
99const USER_PREFIX: &str = "";
100const PLACEHOLDER_COLOR: RgbColor =
101 RgbColor(ui::PLACEHOLDER_R, ui::PLACEHOLDER_G, ui::PLACEHOLDER_B);
102const MAX_LOG_LINES: usize = 256;
103const MAX_LOG_DRAIN_PER_TICK: usize = 256;
104
105#[derive(Clone, Debug)]
106struct CollapsedPaste {
107 line_index: usize,
108 full_text: String,
109}
110
111pub struct Session {
112 pub(crate) input_manager: InputManager,
115 pub(crate) scroll_manager: ScrollManager,
117 user_scrolled: bool,
118
119 pub(crate) lines: Vec<MessageLine>,
121 collapsed_pastes: Vec<CollapsedPaste>,
122 pub(crate) theme: InlineTheme,
123 pub(crate) styles: SessionStyles,
124 pub(crate) appearance: AppearanceConfig,
125 pub(crate) header_context: InlineHeaderContext,
126 pub(crate) header_rows: u16,
127 pub(crate) labels: MessageLabels,
128
129 prompt_prefix: String,
131 prompt_style: InlineTextStyle,
132 placeholder: Option<String>,
133 placeholder_style: Option<InlineTextStyle>,
134 pub(crate) input_status_left: Option<String>,
135 pub(crate) input_status_right: Option<String>,
136 input_compact_mode: bool,
137
138 slash_palette: SlashPalette,
140 #[allow(dead_code)]
141 navigation_state: ListState,
142 input_enabled: bool,
143 cursor_visible: bool,
144 pub(crate) needs_redraw: bool,
145 pub(crate) needs_full_clear: bool,
146 pub(crate) transcript_content_changed: bool,
148 should_exit: bool,
149 scroll_cursor_steady_until: Option<Instant>,
150 last_shimmer_active: bool,
151 pub(crate) view_rows: u16,
152 pub(crate) input_height: u16,
153 pub(crate) transcript_rows: u16,
154 pub(crate) transcript_width: u16,
155 pub(crate) transcript_view_top: usize,
156 transcript_area: Option<Rect>,
157 input_area: Option<Rect>,
158
159 log_receiver: Option<UnboundedReceiver<LogEntry>>,
161 log_lines: VecDeque<Arc<Text<'static>>>,
162 log_cached_text: Option<Arc<Text<'static>>>,
163 log_evicted: bool,
164 pub(crate) show_logs: bool,
165
166 transcript_cache: Option<TranscriptReflowCache>,
168 pub(crate) visible_lines_cache: Option<(usize, u16, Arc<Vec<Line<'static>>>)>,
171 pub(crate) queued_inputs: Vec<String>,
172 queue_overlay_cache: Option<QueueOverlay>,
173 queue_overlay_version: u64,
174 pub(crate) modal: Option<ModalState>,
175 wizard_modal: Option<WizardModalState>,
176 line_revision_counter: u64,
177 first_dirty_line: Option<usize>,
179 in_tool_code_fence: bool,
180
181 pub(crate) file_palette: Option<FilePalette>,
183 pub(crate) file_palette_active: bool,
184 pub(crate) inline_lists_visible: bool,
185
186 pub(crate) thinking_spinner: ThinkingSpinner,
188 pub(crate) shimmer_state: ShimmerState,
189
190 pub(crate) reverse_search_state: reverse_search::ReverseSearchState,
192
193 pub(crate) history_picker_state: HistoryPickerState,
195
196 pub(crate) active_pty_sessions: Option<Arc<std::sync::atomic::AtomicUsize>>,
198
199 #[allow(dead_code)]
201 pub(crate) clipboard: String,
202
203 pub(crate) mouse_selection: MouseSelectionState,
205
206 pub(crate) diff_preview: Option<crate::ui::tui::types::DiffPreviewState>,
208
209 pub(crate) skip_confirmations: bool,
210
211 pub(crate) header_lines_cache: Option<Vec<Line<'static>>>,
213 pub(crate) header_height_cache: hashbrown::HashMap<u16, u16>,
214 pub(crate) queued_inputs_preview_cache: Option<Vec<String>>,
215
216 pub(crate) app_name: String,
219 pub(crate) workspace_root: Option<std::path::PathBuf>,
221 last_terminal_title: Option<String>,
223
224 pub(crate) is_streaming_final_answer: bool,
229}