opendev-tui 0.1.4

Ratatui-based terminal UI for OpenDev
Documentation
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Event dispatching: routes AppEvents to state mutations.

use crate::event::AppEvent;
use crate::widgets::{TodoDisplayItem, TodoDisplayStatus};

use super::{App, DisplayRole};

/// Map TodoManager items to TUI display items.
pub(super) fn map_todo_items(mgr: &opendev_runtime::TodoManager) -> Vec<TodoDisplayItem> {
    mgr.all()
        .iter()
        .map(|item| TodoDisplayItem {
            id: item.id,
            title: item.title.clone(),
            status: match item.status {
                opendev_runtime::TodoStatus::Pending => TodoDisplayStatus::Pending,
                opendev_runtime::TodoStatus::InProgress => TodoDisplayStatus::InProgress,
                opendev_runtime::TodoStatus::Completed => TodoDisplayStatus::Completed,
            },
            active_form: if item.active_form.is_empty() {
                None
            } else {
                Some(item.active_form.clone())
            },
        })
        .collect()
}

impl App {
    /// Synchronize TUI todo display items from the shared TodoManager.
    pub(super) fn sync_todo_display(&mut self) {
        if let Some(ref mgr) = self.state.todo_manager
            && let Ok(mgr) = mgr.lock()
        {
            self.state.todo_items = map_todo_items(&mgr);
        }
    }
    /// Finalize the most recent unfinalized thinking block by freezing its duration.
    /// Called when a non-reasoning event arrives (AgentChunk, ToolStarted, etc.).
    pub(super) fn finalize_active_thinking(&mut self) {
        if let Some(msg) = self
            .state
            .messages
            .iter_mut()
            .rev()
            .find(|m| m.role == DisplayRole::Reasoning && m.thinking_duration_secs.is_none())
        {
            if let Some(started) = msg.thinking_started_at {
                msg.thinking_duration_secs = Some(started.elapsed().as_secs());
            } else {
                // History replay or missing start time
                msg.thinking_duration_secs = Some(0);
            }
            self.state.message_generation += 1;
        }
    }

    /// Drain the next pending item from the unified queue.
    /// User messages are sent one at a time. Consecutive background results are batched.
    pub(super) fn drain_next_pending(&mut self) {
        if self.state.pending_queue.is_empty() {
            return;
        }
        match self.state.pending_queue.front() {
            Some(super::PendingItem::UserMessage(_)) => {
                if let Some(super::PendingItem::UserMessage(msg)) =
                    self.state.pending_queue.pop_front()
                {
                    // Display the user message NOW (deferred from queue time)
                    self.message_controller
                        .handle_user_submit(&mut self.state, &msg);
                    self.state.message_generation += 1;
                    self.state.agent_active = true;
                    let _ = self.event_tx.send(AppEvent::UserSubmit(msg));
                    self.state.dirty = true;
                }
            }
            Some(super::PendingItem::BackgroundResult { .. }) => {
                // Process one background result at a time so the foreground agent
                // reasons about each task independently (no cross-group pollution).
                if let Some(super::PendingItem::BackgroundResult {
                    task_id,
                    query,
                    result,
                    tool_call_count,
                    ..
                }) = self.state.pending_queue.pop_front()
                {
                    let msg = format!(
                        "[Background task [{task_id}] completed ({tool_call_count} tools)]\n\
                         Task: {query}\n\n\
                         {result}"
                    );
                    // Display as a user-like message so the agent's streaming
                    // response creates a NEW assistant message instead of
                    // appending to the previous nudge message.
                    self.message_controller
                        .handle_user_submit(&mut self.state, &msg);
                    self.state.agent_active = true;
                    self.state.message_generation += 1;
                    let _ = self.event_tx.send(AppEvent::UserSubmit(msg));
                    self.state.dirty = true;
                }
            }
            None => {}
        }
    }

    /// Dispatch an event to the appropriate handler.
    pub(super) fn handle_event(&mut self, event: AppEvent) {
        // Detect tab-switch return via timing gap: if >1s elapsed since last
        // user-interactive event, the user likely switched away and came back.
        // Force a full repaint to fix any screen corruption from the terminal
        // emulator (works even when FocusGained doesn't fire).
        let is_user_event = matches!(
            event,
            AppEvent::Key(_)
                | AppEvent::ScrollUp
                | AppEvent::ScrollDown
                | AppEvent::MouseDown { .. }
                | AppEvent::MouseDrag { .. }
                | AppEvent::MouseUp { .. }
        );
        if is_user_event {
            if let Some(last) = self.state.last_event_time
                && last.elapsed() > std::time::Duration::from_secs(1)
            {
                self.state.force_clear = true;
            }
            self.state.last_event_time = Some(std::time::Instant::now());
        }

        match event {
            AppEvent::Key(key) => {
                // Any keyboard input clears the selection
                if self.state.selection.range.is_some() {
                    self.state.selection.clear();
                }
                self.handle_key(key);
                self.state.dirty = true;
            }
            AppEvent::Resize(_, _) => {
                // Clear selection on resize (geometry changed)
                self.state.selection.clear();
                self.state.dirty = true;
            }
            AppEvent::FocusGained => {
                // Force full redraw when terminal regains focus to fix screen corruption.
                // Setting force_clear triggers terminal.clear() which resets ratatui's
                // internal diff buffer, ensuring every cell is repainted.
                self.state.force_clear = true;
                self.state.dirty = true;
            }
            AppEvent::ScrollUp => {
                let amount = self.accelerated_scroll(true);
                self.state.scroll_offset = self.state.scroll_offset.saturating_add(amount);
                self.state.user_scrolled = true;
                self.state.dirty = true;
            }
            AppEvent::ScrollDown => {
                if self.state.scroll_offset > 0 {
                    let amount = self.accelerated_scroll(false);
                    self.state.scroll_offset = self.state.scroll_offset.saturating_sub(amount);
                } else {
                    self.state.user_scrolled = false;
                }
                self.state.dirty = true;
            }
            AppEvent::MouseDown { col, row } => {
                self.handle_mouse_down(col, row);
                self.state.dirty = true;
            }
            AppEvent::MouseDrag { col, row } => {
                self.handle_mouse_drag(col, row);
                self.state.dirty = true;
            }
            AppEvent::MouseUp { col, row } => {
                self.handle_mouse_up(col, row);
                self.state.dirty = true;
            }
            AppEvent::Tick => {
                self.handle_tick();
                // Tick is dirty only when there are animations running
                if self.state.agent_active
                    || !self.state.active_tools.is_empty()
                    || !self.state.active_subagents.is_empty()
                    || self.state.task_progress.is_some()
                    || !self.state.welcome_panel.fade_complete
                    || self.state.task_watcher_open
                    || self.state.background_task_count > 0
                    || self.state.last_task_completion.is_some()
                    || self.state.backgrounded_task_info.is_some()
                    || !self.state.toasts.is_empty()
                    || self.state.leader_pending
                    || self.state.selection.active
                {
                    self.state.dirty = true;
                }
            }

            // Budget events
            AppEvent::BudgetExhausted {
                cost_usd,
                budget_usd,
            } => self.handle_budget_exhausted(cost_usd, budget_usd),

            // File change summary events
            AppEvent::FileChangeSummary {
                files,
                additions,
                deletions,
            } => self.handle_file_change_summary(files, additions, deletions),

            // Context usage events
            AppEvent::ContextUsage(pct) => self.handle_context_usage(pct),

            // Agent events
            AppEvent::AgentStarted => self.handle_agent_started(),
            AppEvent::AgentChunk(text) => self.handle_agent_chunk(text),
            AppEvent::AgentMessage(msg) => self.handle_agent_message(msg),
            AppEvent::AgentFinished => self.handle_agent_finished(),
            AppEvent::AgentError(err) => self.handle_agent_error(err),

            // Reasoning events
            AppEvent::ReasoningBlockStart => self.handle_reasoning_block_start(),
            AppEvent::ReasoningContent(content) => self.handle_reasoning_content(content),

            // Tool events
            AppEvent::ToolStarted {
                tool_id,
                tool_name,
                args,
            } => self.handle_tool_started(tool_id, tool_name, args),
            AppEvent::ToolOutput { tool_id, output } => self.handle_tool_output(tool_id, output),
            AppEvent::ToolResult {
                tool_id,
                tool_name,
                output,
                success,
                args: result_args,
            } => self.handle_tool_result(tool_id, tool_name, output, success, result_args),
            AppEvent::ToolFinished { tool_id, success } => {
                self.handle_tool_finished(tool_id, success)
            }
            AppEvent::ToolApprovalRequired {
                tool_id: _,
                tool_name: _,
                description,
            } => self.handle_tool_approval_required(description),
            AppEvent::ToolApprovalRequested {
                command,
                working_dir,
                response_tx,
            } => self.handle_tool_approval_requested(command, working_dir, response_tx),
            AppEvent::AskUserRequested {
                question,
                options,
                default,
                response_tx,
            } => self.handle_ask_user_requested(question, options, default, response_tx),

            // Subagent events
            AppEvent::SubagentStarted {
                subagent_id,
                subagent_name,
                task,
                cancel_token,
            } => self.handle_subagent_started(subagent_id, subagent_name, task, cancel_token),
            AppEvent::SubagentToolCall {
                subagent_id,
                tool_name,
                tool_id,
                args,
                ..
            } => self.handle_subagent_tool_call(subagent_id, tool_name, tool_id, args),
            AppEvent::SubagentToolComplete {
                subagent_id,
                tool_name,
                tool_id,
                success,
                ..
            } => self.handle_subagent_tool_complete(subagent_id, tool_name, tool_id, success),
            AppEvent::SubagentFinished {
                subagent_id,
                success,
                result_summary,
                tool_call_count,
                shallow_warning,
                ..
            } => self.handle_subagent_finished(
                subagent_id,
                success,
                result_summary,
                tool_call_count,
                shallow_warning,
            ),
            AppEvent::SubagentTokenUpdate {
                subagent_id,
                input_tokens,
                output_tokens,
                ..
            } => self.handle_subagent_token_update(subagent_id, input_tokens, output_tokens),

            // Task progress events
            AppEvent::TaskProgressStarted { description } => {
                self.handle_task_progress_started(description)
            }
            AppEvent::TaskProgressFinished => self.handle_task_progress_finished(),

            // Plan approval events
            AppEvent::PlanApprovalRequested {
                plan_content,
                response_tx,
            } => self.handle_plan_approval_requested(plan_content, response_tx),

            AppEvent::UserSubmit(ref msg) => self.handle_user_submit(msg),
            AppEvent::Interrupt => self.handle_interrupt(),
            AppEvent::SetInterruptToken(token) => self.handle_set_interrupt_token(token),
            AppEvent::AgentInterrupted => self.handle_agent_interrupted(),
            AppEvent::ModeChanged(mode) => self.handle_mode_changed(mode),
            AppEvent::KillTask(id) => self.handle_kill_task(id),
            AppEvent::CompactionStarted => self.handle_compaction_started(),
            AppEvent::CompactionFinished { success, message } => {
                self.handle_compaction_finished(success, message)
            }

            // Background agent events
            AppEvent::AgentBackgrounded {
                task_id,
                query_summary: _,
            } => self.handle_agent_backgrounded(task_id),
            AppEvent::BackgroundNudge { content } => self.handle_background_nudge(content),
            AppEvent::BackgroundAgentCompleted {
                task_id,
                success,
                result_summary,
                full_result,
                cost_usd,
                tool_call_count,
            } => self.handle_background_agent_completed(
                task_id,
                success,
                result_summary,
                full_result,
                cost_usd,
                tool_call_count,
            ),
            AppEvent::BackgroundAgentProgress {
                task_id,
                tool_name,
                tool_count,
            } => self.handle_background_agent_progress(task_id, tool_name, tool_count),
            AppEvent::BackgroundAgentActivity { task_id, line } => {
                self.handle_background_agent_activity(task_id, line)
            }
            AppEvent::BackgroundAgentKilled { task_id } => {
                self.handle_background_agent_killed(task_id)
            }
            AppEvent::SetBackgroundAgentToken {
                task_id,
                query,
                session_id,
                interrupt_token,
            } => {
                self.handle_set_background_agent_token(task_id, query, session_id, interrupt_token)
            }

            // Undo/Redo/Share events
            AppEvent::SnapshotTaken { hash } => self.handle_snapshot_taken(hash),
            AppEvent::UndoResult { success, message } => self.handle_undo_result(success, message),
            AppEvent::RedoResult { success, message } => self.handle_redo_result(success, message),
            AppEvent::ShareResult { path } => self.handle_share_result(path),
            AppEvent::FileChanged { paths } => self.handle_file_changed(paths),
            AppEvent::SessionTitleUpdated(title) => self.handle_session_title_updated(title),

            AppEvent::Quit => {
                self.state.running = false;
                self.state.dirty = true;
            }

            // Passthrough for unhandled events
            _ => {}
        }
    }

    /// Handle mouse button press — start selection if in conversation area.
    fn handle_mouse_down(&mut self, col: u16, row: u16) {
        // Don't start selection during modal overlays
        if self.approval_controller.active()
            || self.ask_user_controller.active()
            || self.plan_approval_controller.active()
            || self
                .model_picker_controller
                .as_ref()
                .is_some_and(|p| p.active())
            || self.state.task_watcher_open
            || self.state.debug_panel_open
        {
            return;
        }

        if self.state.selection.is_in_conversation_area(col, row) {
            self.state.selection.start(col, row);
        } else {
            self.state.selection.clear();
        }
    }

    /// Handle mouse drag — extend selection, set auto-scroll direction.
    fn handle_mouse_drag(&mut self, col: u16, row: u16) {
        if !self.state.selection.active {
            return;
        }
        self.state.selection.extend(col, row);
    }

    /// Handle mouse button release — finalize selection and copy to clipboard.
    fn handle_mouse_up(&mut self, col: u16, row: u16) {
        if !self.state.selection.active {
            return;
        }

        // Update cursor position one last time
        self.state.selection.extend(col, row);

        if self.state.selection.finalize() {
            // Extract and copy selected text
            if let Some(text) = self.extract_selected_text() {
                self.copy_to_clipboard(&text);
            }
        }
    }

    /// Extract plain text from the selected range of cached lines.
    fn extract_selected_text(&self) -> Option<String> {
        let range = self.state.selection.range?;
        let (start, end) = range.ordered();
        let lines = &self.state.cached_lines;

        if lines.is_empty() || start.line_index >= lines.len() {
            return None;
        }

        let end_line = end.line_index.min(lines.len().saturating_sub(1));
        let mut result = String::new();

        for (i, line) in lines[start.line_index..=end_line].iter().enumerate() {
            let line_idx = start.line_index + i;
            if i > 0 {
                result.push('\n');
            }

            // Collect the full text of this line from spans
            let full_text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();

            let col_start = if line_idx == start.line_index {
                start.char_offset
            } else {
                0
            };
            let col_end = if line_idx == end.line_index {
                end.char_offset
            } else {
                full_text.len()
            };

            // Clamp to actual line length (char boundary safe)
            let clamped_start = col_start.min(full_text.len());
            let clamped_end = col_end.min(full_text.len());
            if clamped_start < clamped_end {
                // Find char boundaries
                let byte_start = full_text
                    .char_indices()
                    .nth(clamped_start)
                    .map(|(i, _)| i)
                    .unwrap_or(full_text.len());
                let byte_end = full_text
                    .char_indices()
                    .nth(clamped_end)
                    .map(|(i, _)| i)
                    .unwrap_or(full_text.len());
                result.push_str(&full_text[byte_start..byte_end]);
            }
        }

        if result.is_empty() {
            None
        } else {
            Some(result)
        }
    }

    /// Copy text to the system clipboard.
    fn copy_to_clipboard(&mut self, text: &str) {
        match arboard::Clipboard::new() {
            Ok(mut clipboard) => {
                if let Err(e) = clipboard.set_text(text) {
                    tracing::warn!("Failed to copy to clipboard: {e}");
                }
            }
            Err(e) => {
                tracing::warn!("Failed to access clipboard: {e}");
            }
        }
    }
}