fresh-editor 0.1.90

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! Terminal integration for the Editor
//!
//! This module provides methods for the Editor to interact with the terminal system:
//! - Opening new terminal sessions
//! - Closing terminals
//! - Rendering terminal content
//! - Handling terminal input
//!
//! # Role in Incremental Streaming Architecture
//!
//! This module handles mode switching between terminal and scrollback modes.
//! See `crate::services::terminal` for the full architecture diagram.
//!
//! ## Mode Switching Methods
//!
//! - [`Editor::sync_terminal_to_buffer`]: Terminal → Scrollback mode
//!   - Appends visible screen (~50 lines) to backing file
//!   - Loads backing file as read-only buffer
//!   - Performance: O(screen_size) ≈ 5ms
//!
//! - [`Editor::enter_terminal_mode`]: Scrollback → Terminal mode
//!   - Truncates backing file to remove visible screen tail
//!   - Resumes live terminal rendering
//!   - Performance: O(1) ≈ 1ms

use super::{BufferId, BufferMetadata, Editor};
use crate::services::terminal::TerminalId;
use crate::state::EditorState;
use rust_i18n::t;

impl Editor {
    /// Open a new terminal in the current split
    pub fn open_terminal(&mut self) {
        // Get the current split dimensions for the terminal size
        let (cols, rows) = self.get_terminal_dimensions();

        // Set up async bridge for terminal manager if not already done
        if let Some(ref bridge) = self.async_bridge {
            self.terminal_manager.set_async_bridge(bridge.clone());
        }

        // Prepare persistent storage paths under the user's data directory
        let terminal_root = self.dir_context.terminal_dir_for(&self.working_dir);
        let _ = self.filesystem.create_dir_all(&terminal_root);
        // Precompute paths using the next terminal ID so we capture from the first byte
        let predicted_terminal_id = self.terminal_manager.next_terminal_id();
        let log_path =
            terminal_root.join(format!("fresh-terminal-{}.log", predicted_terminal_id.0));
        let backing_path =
            terminal_root.join(format!("fresh-terminal-{}.txt", predicted_terminal_id.0));
        // Stash backing path now so buffer creation can reuse it
        self.terminal_backing_files
            .insert(predicted_terminal_id, backing_path);

        // Spawn terminal with incremental scrollback streaming
        let backing_path_for_spawn = self
            .terminal_backing_files
            .get(&predicted_terminal_id)
            .cloned();
        match self.terminal_manager.spawn(
            cols,
            rows,
            Some(self.working_dir.clone()),
            Some(log_path.clone()),
            backing_path_for_spawn,
        ) {
            Ok(terminal_id) => {
                // Track log file path (use actual ID in case it differs)
                let actual_log_path = log_path.clone();
                self.terminal_log_files
                    .insert(terminal_id, actual_log_path.clone());
                // If predicted differs, move backing path entry
                if terminal_id != predicted_terminal_id {
                    self.terminal_backing_files.remove(&predicted_terminal_id);
                    let backing_path =
                        terminal_root.join(format!("fresh-terminal-{}.txt", terminal_id.0));
                    self.terminal_backing_files
                        .insert(terminal_id, backing_path);
                }

                // Create a buffer for this terminal
                let buffer_id = self.create_terminal_buffer_attached(
                    terminal_id,
                    self.split_manager.active_split(),
                );

                // Switch to the terminal buffer
                self.set_active_buffer(buffer_id);

                // Enable terminal mode
                self.terminal_mode = true;
                self.key_context = crate::input::keybindings::KeyContext::Terminal;

                // Resize terminal to match actual split content area
                self.resize_visible_terminals();

                // Get the terminal escape keybinding dynamically
                let exit_key = self
                    .keybindings
                    .find_keybinding_for_action(
                        "terminal_escape",
                        crate::input::keybindings::KeyContext::Terminal,
                    )
                    .unwrap_or_else(|| "Ctrl+Space".to_string());
                self.set_status_message(
                    t!("terminal.opened", id = terminal_id.0, exit_key = exit_key).to_string(),
                );
                tracing::info!(
                    "Opened terminal {:?} with buffer {:?}",
                    terminal_id,
                    buffer_id
                );
            }
            Err(e) => {
                self.set_status_message(
                    t!("terminal.failed_to_open", error = e.to_string()).to_string(),
                );
                tracing::error!("Failed to open terminal: {}", e);
            }
        }
    }

    /// Create a buffer for a terminal session
    fn create_terminal_buffer_attached(
        &mut self,
        terminal_id: TerminalId,
        split_id: crate::model::event::SplitId,
    ) -> BufferId {
        let buffer_id = BufferId(self.next_buffer_id);
        self.next_buffer_id += 1;

        // Get config values
        let large_file_threshold = self.config.editor.large_file_threshold_bytes as usize;

        // Rendered backing file for scrollback view (reuse if already recorded)
        let backing_file = self
            .terminal_backing_files
            .get(&terminal_id)
            .cloned()
            .unwrap_or_else(|| {
                let root = self.dir_context.terminal_dir_for(&self.working_dir);
                let _ = self.filesystem.create_dir_all(&root);
                root.join(format!("fresh-terminal-{}.txt", terminal_id.0))
            });

        // Ensure the file exists - but DON'T truncate if it already has content
        // The PTY read loop may have already started writing scrollback
        if !self.filesystem.exists(&backing_file) {
            if let Err(e) = self.filesystem.write_file(&backing_file, &[]) {
                tracing::warn!("Failed to create terminal backing file: {}", e);
            }
        }

        // Store the backing file path
        self.terminal_backing_files
            .insert(terminal_id, backing_file.clone());

        // Create editor state with the backing file
        let mut state = EditorState::new(
            self.terminal_width,
            self.terminal_height,
            large_file_threshold,
            std::sync::Arc::clone(&self.filesystem),
        );
        state.buffer.set_file_path(backing_file.clone());
        // Terminal buffers should never show line numbers
        state.margins.set_line_numbers(false);
        self.buffers.insert(buffer_id, state);

        // Use virtual metadata so the tab shows "*Terminal N*" and LSP stays off.
        // The backing file is still tracked separately for syncing scrollback.
        let metadata = BufferMetadata::virtual_buffer(
            format!("*Terminal {}*", terminal_id.0),
            "terminal".into(),
            false,
        );
        self.buffer_metadata.insert(buffer_id, metadata);

        // Map buffer to terminal
        self.terminal_buffers.insert(buffer_id, terminal_id);

        // Initialize event log for undo/redo
        self.event_logs
            .insert(buffer_id, crate::model::event::EventLog::new());

        // Set up split view state
        if let Some(view_state) = self.split_view_states.get_mut(&split_id) {
            view_state.open_buffers.push(buffer_id);
            // Terminal buffers should not wrap lines so escape sequences stay intact
            view_state.viewport.line_wrap_enabled = false;
        }

        buffer_id
    }

    /// Create a terminal buffer without attaching it to any split (used during session restore).
    pub(crate) fn create_terminal_buffer_detached(&mut self, terminal_id: TerminalId) -> BufferId {
        let buffer_id = BufferId(self.next_buffer_id);
        self.next_buffer_id += 1;

        // Get config values
        let large_file_threshold = self.config.editor.large_file_threshold_bytes as usize;

        let backing_file = self
            .terminal_backing_files
            .get(&terminal_id)
            .cloned()
            .unwrap_or_else(|| {
                let root = self.dir_context.terminal_dir_for(&self.working_dir);
                let _ = self.filesystem.create_dir_all(&root);
                root.join(format!("fresh-terminal-{}.txt", terminal_id.0))
            });

        // Create the file only if it doesn't exist (preserve existing scrollback for restore)
        if !self.filesystem.exists(&backing_file) {
            if let Err(e) = self.filesystem.write_file(&backing_file, &[]) {
                tracing::warn!("Failed to create terminal backing file: {}", e);
            }
        }

        // Create editor state with the backing file
        let mut state = EditorState::new(
            self.terminal_width,
            self.terminal_height,
            large_file_threshold,
            std::sync::Arc::clone(&self.filesystem),
        );
        state.buffer.set_file_path(backing_file.clone());
        state.margins.set_line_numbers(false);
        self.buffers.insert(buffer_id, state);

        let metadata = BufferMetadata::virtual_buffer(
            format!("*Terminal {}*", terminal_id.0),
            "terminal".into(),
            false,
        );
        self.buffer_metadata.insert(buffer_id, metadata);
        self.terminal_buffers.insert(buffer_id, terminal_id);
        self.event_logs
            .insert(buffer_id, crate::model::event::EventLog::new());

        buffer_id
    }

    /// Close the current terminal (if viewing a terminal buffer)
    pub fn close_terminal(&mut self) {
        let buffer_id = self.active_buffer();

        if let Some(&terminal_id) = self.terminal_buffers.get(&buffer_id) {
            // Close the terminal
            self.terminal_manager.close(terminal_id);
            self.terminal_buffers.remove(&buffer_id);

            // Clean up backing/rendering file
            let backing_file = self.terminal_backing_files.remove(&terminal_id);
            if let Some(ref path) = backing_file {
                let _ = self.filesystem.remove_file(path);
            }
            // Clean up raw log file
            if let Some(log_file) = self.terminal_log_files.remove(&terminal_id) {
                if backing_file.as_ref() != Some(&log_file) {
                    let _ = self.filesystem.remove_file(&log_file);
                }
            }

            // Exit terminal mode
            self.terminal_mode = false;
            self.key_context = crate::input::keybindings::KeyContext::Normal;

            // Close the buffer
            let _ = self.close_buffer(buffer_id);

            self.set_status_message(t!("terminal.closed", id = terminal_id.0).to_string());
        } else {
            self.set_status_message(t!("status.not_viewing_terminal").to_string());
        }
    }

    /// Check if a buffer is a terminal buffer
    pub fn is_terminal_buffer(&self, buffer_id: BufferId) -> bool {
        self.terminal_buffers.contains_key(&buffer_id)
    }

    /// Get the terminal ID for a buffer (if it's a terminal buffer)
    pub fn get_terminal_id(&self, buffer_id: BufferId) -> Option<TerminalId> {
        self.terminal_buffers.get(&buffer_id).copied()
    }

    /// Get the terminal state for the active buffer (if it's a terminal buffer)
    pub fn get_active_terminal_state(
        &self,
    ) -> Option<std::sync::MutexGuard<'_, crate::services::terminal::TerminalState>> {
        let terminal_id = self.terminal_buffers.get(&self.active_buffer())?;
        let handle = self.terminal_manager.get(*terminal_id)?;
        handle.state.lock().ok()
    }

    /// Send input to the active terminal
    pub fn send_terminal_input(&mut self, data: &[u8]) {
        if let Some(&terminal_id) = self.terminal_buffers.get(&self.active_buffer()) {
            if let Some(handle) = self.terminal_manager.get(terminal_id) {
                handle.write(data);
            }
        }
    }

    /// Send a key event to the active terminal
    pub fn send_terminal_key(
        &mut self,
        code: crossterm::event::KeyCode,
        modifiers: crossterm::event::KeyModifiers,
    ) {
        if let Some(bytes) = crate::services::terminal::pty::key_to_pty_bytes(code, modifiers) {
            self.send_terminal_input(&bytes);
        }
    }

    /// Get terminal dimensions based on split size
    fn get_terminal_dimensions(&self) -> (u16, u16) {
        // Use the visible area of the current split
        // Subtract 1 for status bar, tab bar, etc.
        let cols = self.terminal_width.saturating_sub(2).max(40);
        let rows = self.terminal_height.saturating_sub(4).max(10);
        (cols, rows)
    }

    /// Resize terminal to match split dimensions
    pub fn resize_terminal(&mut self, buffer_id: BufferId, cols: u16, rows: u16) {
        if let Some(&terminal_id) = self.terminal_buffers.get(&buffer_id) {
            if let Some(handle) = self.terminal_manager.get_mut(terminal_id) {
                handle.resize(cols, rows);
            }
        }
    }

    /// Resize all visible terminal PTYs to match their current split dimensions.
    /// Call this after operations that change split layout (maximize, resize, etc.)
    pub fn resize_visible_terminals(&mut self) {
        // Get the content area excluding file explorer
        let file_explorer_width = if self.file_explorer_visible {
            (self.terminal_width as f32 * self.file_explorer_width_percent) as u16
        } else {
            0
        };
        let editor_width = self.terminal_width.saturating_sub(file_explorer_width);
        let editor_area = ratatui::layout::Rect::new(
            file_explorer_width,
            1, // menu bar
            editor_width,
            self.terminal_height.saturating_sub(2), // menu bar + status bar
        );

        // Get visible buffers with their areas
        let visible_buffers = self.split_manager.get_visible_buffers(editor_area);

        // Resize each terminal buffer to match its split content area
        for (_split_id, buffer_id, split_area) in visible_buffers {
            if self.terminal_buffers.contains_key(&buffer_id) {
                // Calculate content dimensions (accounting for tab bar and borders)
                // Tab bar takes 1 row, and we leave 1 for scrollbar width on right
                let content_height = split_area.height.saturating_sub(2);
                let content_width = split_area.width.saturating_sub(2);

                if content_width > 0 && content_height > 0 {
                    self.resize_terminal(buffer_id, content_width, content_height);
                }
            }
        }
    }

    /// Handle terminal input when in terminal mode
    pub fn handle_terminal_key(
        &mut self,
        code: crossterm::event::KeyCode,
        modifiers: crossterm::event::KeyModifiers,
    ) -> bool {
        // Check for escape sequences to exit terminal mode
        // Ctrl+Space, Ctrl+], or Ctrl+` to exit (Ctrl+\ sends SIGQUIT on Unix)
        if modifiers.contains(crossterm::event::KeyModifiers::CONTROL) {
            match code {
                crossterm::event::KeyCode::Char(' ')
                | crossterm::event::KeyCode::Char(']')
                | crossterm::event::KeyCode::Char('`') => {
                    // Exit terminal mode and sync buffer
                    self.terminal_mode = false;
                    self.key_context = crate::input::keybindings::KeyContext::Normal;
                    self.sync_terminal_to_buffer(self.active_buffer());
                    self.set_status_message(
                        "Terminal mode disabled - read only (Ctrl+Space to resume)".to_string(),
                    );
                    return true;
                }
                _ => {}
            }
        }

        // Send the key to the terminal
        self.send_terminal_key(code, modifiers);
        true
    }

    /// Sync terminal content to the text buffer for read-only viewing/selection
    ///
    /// This uses the incremental streaming architecture:
    /// 1. Scrollback has already been streamed to the backing file during PTY reads
    /// 2. We just append the visible screen (~50 lines) to the backing file
    /// 3. Reload the buffer from the backing file (lazy load for large files)
    ///
    /// Performance: O(screen_size) instead of O(total_history)
    pub fn sync_terminal_to_buffer(&mut self, buffer_id: BufferId) {
        if let Some(&terminal_id) = self.terminal_buffers.get(&buffer_id) {
            // Get the backing file path
            let backing_file = match self.terminal_backing_files.get(&terminal_id) {
                Some(path) => path.clone(),
                None => return,
            };

            // Append visible screen to backing file
            // The scrollback has already been incrementally streamed by the PTY read loop
            if let Some(handle) = self.terminal_manager.get(terminal_id) {
                if let Ok(mut state) = handle.state.lock() {
                    // Record the current file size as the history end point
                    // (before appending visible screen) so we can truncate back to it
                    if let Ok(metadata) = self.filesystem.metadata(&backing_file) {
                        state.set_backing_file_history_end(metadata.size);
                    }

                    // Open backing file in append mode to add visible screen
                    if let Ok(mut file) = self.filesystem.open_file_for_append(&backing_file) {
                        use std::io::BufWriter;
                        let mut writer = BufWriter::new(&mut *file);
                        if let Err(e) = state.append_visible_screen(&mut writer) {
                            tracing::error!(
                                "Failed to append visible screen to backing file: {}",
                                e
                            );
                        }
                    }
                }
            }

            // Reload buffer from the backing file (reusing existing file loading)
            let large_file_threshold = self.config.editor.large_file_threshold_bytes as usize;
            if let Ok(new_state) = EditorState::from_file_with_languages(
                &backing_file,
                self.terminal_width,
                self.terminal_height,
                large_file_threshold,
                &self.grammar_registry,
                &self.config.languages,
                std::sync::Arc::clone(&self.filesystem),
            ) {
                // Replace buffer state
                if let Some(state) = self.buffers.get_mut(&buffer_id) {
                    *state = new_state;
                    // Move cursor to end of buffer
                    state.primary_cursor_mut().position = state.buffer.total_bytes();
                    // Terminal buffers should never be considered "modified"
                    state.buffer.set_modified(false);
                }
            }

            // Mark buffer as editing-disabled while in non-terminal mode
            if let Some(state) = self.buffers.get_mut(&buffer_id) {
                state.editing_disabled = true;
                state.margins.set_line_numbers(false);
            }

            // In read-only view, keep line wrapping disabled for terminal buffers
            // Also scroll viewport to show the end of the buffer where the cursor is
            if let Some(view_state) = self
                .split_view_states
                .get_mut(&self.split_manager.active_split())
            {
                view_state.viewport.line_wrap_enabled = false;

                // Clear skip_ensure_visible flag so the viewport scrolls to cursor
                // This fixes the bug where re-entering scrollback mode would jump to the
                // previous scroll position because the flag was still set from scrolling
                view_state.viewport.clear_skip_ensure_visible();

                // Scroll viewport to make cursor visible at the end of buffer
                if let Some(state) = self.buffers.get_mut(&buffer_id) {
                    let cursor = *state.cursors.primary();
                    view_state
                        .viewport
                        .ensure_visible(&mut state.buffer, &cursor);
                }
            }
        }
    }

    /// Re-enter terminal mode from read-only buffer view
    ///
    /// This truncates the backing file to remove the visible screen tail
    /// that was appended when we exited terminal mode, leaving only the
    /// incrementally-streamed scrollback history.
    pub fn enter_terminal_mode(&mut self) {
        if self.is_terminal_buffer(self.active_buffer()) {
            self.terminal_mode = true;
            self.key_context = crate::input::keybindings::KeyContext::Terminal;

            // Re-enable editing when in terminal mode (input goes to PTY)
            if let Some(state) = self.buffers.get_mut(&self.active_buffer()) {
                state.editing_disabled = false;
                state.margins.set_line_numbers(false);
            }
            if let Some(view_state) = self
                .split_view_states
                .get_mut(&self.split_manager.active_split())
            {
                view_state.viewport.line_wrap_enabled = false;
            }

            // Truncate backing file to remove visible screen tail and scroll to bottom
            if let Some(&terminal_id) = self.terminal_buffers.get(&self.active_buffer()) {
                // Truncate backing file to remove visible screen that was appended
                if let Some(backing_path) = self.terminal_backing_files.get(&terminal_id) {
                    if let Some(handle) = self.terminal_manager.get(terminal_id) {
                        if let Ok(state) = handle.state.lock() {
                            let truncate_pos = state.backing_file_history_end();
                            // Always truncate to remove appended visible screen
                            // (even if truncate_pos is 0, meaning no scrollback yet)
                            if let Err(e) =
                                self.filesystem.set_file_length(backing_path, truncate_pos)
                            {
                                tracing::warn!("Failed to truncate terminal backing file: {}", e);
                            }
                        }
                    }
                }

                // Scroll terminal to bottom when re-entering
                if let Some(handle) = self.terminal_manager.get(terminal_id) {
                    if let Ok(mut state) = handle.state.lock() {
                        state.scroll_to_bottom();
                    }
                }
            }

            // Ensure terminal PTY is sized correctly for current split dimensions
            self.resize_visible_terminals();

            self.set_status_message(t!("status.terminal_mode_enabled").to_string());
        }
    }

    /// Get terminal content for rendering
    pub fn get_terminal_content(
        &self,
        buffer_id: BufferId,
    ) -> Option<Vec<Vec<crate::services::terminal::TerminalCell>>> {
        let terminal_id = self.terminal_buffers.get(&buffer_id)?;
        let handle = self.terminal_manager.get(*terminal_id)?;
        let state = handle.state.lock().ok()?;

        let (_, rows) = state.size();
        let mut content = Vec::with_capacity(rows as usize);

        for row in 0..rows {
            content.push(state.get_line(row));
        }

        Some(content)
    }
}

impl Editor {
    /// Check if terminal mode is active (for testing)
    pub fn is_terminal_mode(&self) -> bool {
        self.terminal_mode
    }

    /// Check if a buffer is in terminal_mode_resume set (for testing/debugging)
    pub fn is_in_terminal_mode_resume(&self, buffer_id: BufferId) -> bool {
        self.terminal_mode_resume.contains(&buffer_id)
    }

    /// Check if keyboard capture is enabled in terminal mode (for testing)
    pub fn is_keyboard_capture(&self) -> bool {
        self.keyboard_capture
    }

    /// Set terminal jump_to_end_on_output config option (for testing)
    pub fn set_terminal_jump_to_end_on_output(&mut self, value: bool) {
        self.config.terminal.jump_to_end_on_output = value;
    }

    /// Get read-only access to the terminal manager (for testing)
    pub fn terminal_manager(&self) -> &crate::services::terminal::TerminalManager {
        &self.terminal_manager
    }

    /// Get read-only access to terminal backing files map (for testing)
    pub fn terminal_backing_files(
        &self,
    ) -> &std::collections::HashMap<crate::services::terminal::TerminalId, std::path::PathBuf> {
        &self.terminal_backing_files
    }

    /// Get the currently active buffer ID
    pub fn active_buffer_id(&self) -> BufferId {
        self.active_buffer()
    }

    /// Get buffer content as a string (for testing)
    pub fn get_buffer_content(&self, buffer_id: BufferId) -> Option<String> {
        self.buffers
            .get(&buffer_id)
            .and_then(|state| state.buffer.to_string())
    }

    /// Get cursor position for a buffer (for testing)
    pub fn get_cursor_position(&self, buffer_id: BufferId) -> Option<usize> {
        self.buffers
            .get(&buffer_id)
            .map(|state| state.primary_cursor().position)
    }

    /// Render terminal content for all terminal buffers in split areas
    ///
    /// Renders all visible terminal buffers from their live terminal state.
    /// This ensures terminals continue updating even when not focused, as long
    /// as they remain visible in a split.
    pub fn render_terminal_splits(
        &self,
        frame: &mut ratatui::Frame,
        split_areas: &[(
            crate::model::event::SplitId,
            BufferId,
            ratatui::layout::Rect,
            ratatui::layout::Rect,
            usize,
            usize,
        )],
    ) {
        for (_split_id, buffer_id, content_rect, _scrollbar_rect, _thumb_start, _thumb_end) in
            split_areas
        {
            // Only render terminal buffers - skip regular file buffers
            if let Some(&terminal_id) = self.terminal_buffers.get(buffer_id) {
                // Only render from live terminal state if in terminal mode OR if not the active buffer
                // (when it's the active buffer but not in terminal mode, we're in read-only scrollback mode
                // and should show the synced buffer content instead)
                let is_active = *buffer_id == self.active_buffer();
                if is_active && !self.terminal_mode {
                    // Active buffer in read-only mode - let normal buffer rendering handle it
                    continue;
                }
                // Get terminal content and cursor info
                if let Some(handle) = self.terminal_manager.get(terminal_id) {
                    if let Ok(state) = handle.state.lock() {
                        let cursor_pos = state.cursor_position();
                        // Only show cursor for the active terminal in terminal mode
                        let cursor_visible =
                            state.cursor_visible() && is_active && self.terminal_mode;
                        let (_, rows) = state.size();

                        // Collect content
                        let mut content = Vec::with_capacity(rows as usize);
                        for row in 0..rows {
                            content.push(state.get_line(row));
                        }

                        // Clear the content area first
                        frame.render_widget(ratatui::widgets::Clear, *content_rect);

                        // Render terminal content with theme colors
                        render::render_terminal_content(
                            &content,
                            cursor_pos,
                            cursor_visible,
                            *content_rect,
                            frame.buffer_mut(),
                            self.theme.terminal_fg,
                            self.theme.terminal_bg,
                        );
                    }
                }
            }
        }
    }
}

/// Terminal rendering utilities
pub mod render {
    use crate::services::terminal::TerminalCell;
    use ratatui::buffer::Buffer;
    use ratatui::layout::Rect;
    use ratatui::style::{Color, Modifier, Style};

    /// Render terminal content to a ratatui buffer
    pub fn render_terminal_content(
        content: &[Vec<TerminalCell>],
        cursor_pos: (u16, u16),
        cursor_visible: bool,
        area: Rect,
        buf: &mut Buffer,
        default_fg: Color,
        default_bg: Color,
    ) {
        for (row_idx, row) in content.iter().enumerate() {
            if row_idx as u16 >= area.height {
                break;
            }

            let y = area.y + row_idx as u16;

            for (col_idx, cell) in row.iter().enumerate() {
                if col_idx as u16 >= area.width {
                    break;
                }

                let x = area.x + col_idx as u16;

                // Build style from cell attributes, using theme defaults
                let mut style = Style::default().fg(default_fg).bg(default_bg);

                // Override with cell-specific colors if present
                if let Some((r, g, b)) = cell.fg {
                    style = style.fg(Color::Rgb(r, g, b));
                }

                if let Some((r, g, b)) = cell.bg {
                    style = style.bg(Color::Rgb(r, g, b));
                }

                // Apply modifiers
                if cell.bold {
                    style = style.add_modifier(Modifier::BOLD);
                }
                if cell.italic {
                    style = style.add_modifier(Modifier::ITALIC);
                }
                if cell.underline {
                    style = style.add_modifier(Modifier::UNDERLINED);
                }
                if cell.inverse {
                    style = style.add_modifier(Modifier::REVERSED);
                }

                // Check if this is the cursor position
                if cursor_visible
                    && row_idx as u16 == cursor_pos.1
                    && col_idx as u16 == cursor_pos.0
                {
                    style = style.add_modifier(Modifier::REVERSED);
                }

                buf.set_string(x, y, cell.c.to_string(), style);
            }
        }
    }
}