par-term 0.29.0

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
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
//! `Pane` — a single terminal pane with its own PTY session and display state.

use std::sync::Arc;
use std::sync::atomic::AtomicBool;

use tokio::runtime::Runtime;
use tokio::sync::RwLock;
use tokio::task::JoinHandle;

use crate::config::Config;
use crate::pane::bell::BellState;
use crate::pane::mouse::MouseState;
use crate::pane::render_cache::RenderCache;
use crate::scroll_state::ScrollState;
use crate::session_logger::{SharedSessionLogger, create_shared_logger};
use crate::tab::{
    apply_login_shell_flag, build_shell_env, configure_terminal_from_config, get_shell_command,
};
use crate::terminal::TerminalManager;
use crate::ui_constants::VISUAL_BELL_FLASH_DURATION_MS;

use super::bounds::PaneBounds;
use super::common::{PaneBackground, PaneId, RestartState};

/// A single terminal pane with its own state
///
/// # RwLock Strategy
///
/// `terminal` uses `tokio::sync::RwLock` for the same reason as `Tab::terminal`:
/// `TerminalManager` is shared between the async PTY reader task and the sync winit
/// event loop.
///
/// Access rules:
/// - **From async tasks**: `terminal.read().await` or `terminal.write().await`
/// - **From the sync event loop**: `terminal.try_read()` or `terminal.try_write()` for polling;
///   `terminal.blocking_write()` for infrequent user-initiated operations only.
pub struct Pane {
    /// Unique identifier for this pane
    pub id: PaneId,
    /// The terminal session for this pane.
    ///
    /// Uses `tokio::sync::RwLock`. From sync contexts use `.try_read()` or `.try_write()` for
    /// non-blocking access or `.blocking_write()` for user-initiated operations.
    pub terminal: Arc<RwLock<TerminalManager>>,
    /// Scroll state for this pane
    pub scroll_state: ScrollState,
    /// Mouse state for this pane
    pub mouse: MouseState,
    /// Bell state for this pane
    pub bell: BellState,
    /// Render cache for this pane
    pub cache: RenderCache,
    /// Async task for refresh polling
    pub refresh_task: Option<JoinHandle<()>>,
    /// Working directory when pane was created
    pub working_directory: Option<String>,
    /// Last time terminal output (activity) was detected
    pub last_activity_time: std::time::Instant,
    /// Last terminal update generation seen
    pub last_seen_generation: u64,
    /// Last activity time for anti-idle keep-alive
    pub anti_idle_last_activity: std::time::Instant,
    /// Last terminal generation recorded for anti-idle tracking
    pub anti_idle_last_generation: u64,
    /// Whether silence notification has been sent for current idle period
    pub silence_notified: bool,
    /// Whether exit notification has been sent for this pane
    pub exit_notified: bool,
    /// Session logger for automatic session recording
    pub session_logger: SharedSessionLogger,
    /// Current bounds of this pane (updated on layout calculation)
    pub bounds: PaneBounds,
    /// Per-pane background settings (overrides global config if image_path is set)
    pub background: PaneBackground,
    /// Last-known title from OSC sequences or CWD fallback (empty if never set)
    pub title: String,
    /// True when pane still has its default/fallback title
    pub has_default_title: bool,
    /// State for shell restart behavior (None = shell running or closed normally)
    pub restart_state: Option<RestartState>,
    /// Whether the parent tab is active (shared with tab for refresh throttling)
    pub is_active: Arc<AtomicBool>,
    /// When true, Drop impl skips cleanup (terminal Arcs are dropped on background threads)
    pub(crate) shutdown_fast: bool,
}

impl Pane {
    /// Create a new pane with a terminal session
    pub fn new(
        id: PaneId,
        config: &Config,
        _runtime: Arc<Runtime>,
        working_directory: Option<String>,
    ) -> anyhow::Result<Self> {
        // Create terminal with scrollback from config
        let mut terminal = TerminalManager::new_with_scrollback(
            config.cols,
            config.rows,
            config.scrollback.scrollback_lines,
        )?;

        // Apply common terminal configuration (theme, clipboard limits, cursor style, unicode)
        configure_terminal_from_config(&mut terminal, config);

        // Determine working directory
        let work_dir = working_directory
            .as_deref()
            .or(config.working_directory.as_deref());

        // Get shell command and apply login shell flag
        #[allow(unused_mut)] // mut is needed on Unix for login shell modification
        let (shell_cmd, mut shell_args) = get_shell_command(config);
        apply_login_shell_flag(&mut shell_args, config);

        let shell_args_deref = shell_args.as_deref();
        let shell_env = build_shell_env(config.shell_env.as_ref());
        terminal.spawn_custom_shell_with_dir(
            &shell_cmd,
            shell_args_deref,
            work_dir,
            shell_env.as_ref(),
        )?;

        // Create shared session logger
        let session_logger = create_shared_logger();

        let terminal = Arc::new(RwLock::new(terminal));

        Ok(Self {
            id,
            terminal,
            scroll_state: ScrollState::new(),
            mouse: MouseState::new(),
            bell: BellState::new(),
            cache: RenderCache::new(),
            refresh_task: None,
            working_directory: working_directory.or_else(|| config.working_directory.clone()),
            last_activity_time: std::time::Instant::now(),
            last_seen_generation: 0,
            anti_idle_last_activity: std::time::Instant::now(),
            anti_idle_last_generation: 0,
            silence_notified: false,
            exit_notified: false,
            session_logger,
            bounds: PaneBounds::default(),
            title: String::new(),
            has_default_title: true,
            background: PaneBackground::new(),
            restart_state: None,
            is_active: Arc::new(AtomicBool::new(false)),
            shutdown_fast: false,
        })
    }

    /// Create a pane that launches `command args` instead of the configured login shell.
    ///
    /// Identical to `Pane::new()` except the PTY is started with the given command.
    /// All other fields (scroll state, cache, refresh task, etc.) are the same as `new()`.
    pub fn new_with_command(
        id: PaneId,
        config: &Config,
        _runtime: Arc<Runtime>,
        working_directory: Option<String>,
        command: String,
        args: Vec<String>,
    ) -> anyhow::Result<Self> {
        // Create terminal with scrollback from config
        let mut terminal = TerminalManager::new_with_scrollback(
            config.cols,
            config.rows,
            config.scrollback.scrollback_lines,
        )?;

        // Apply common terminal configuration (theme, clipboard limits, cursor style, unicode)
        configure_terminal_from_config(&mut terminal, config);

        // Determine working directory
        let work_dir = working_directory
            .as_deref()
            .or(config.working_directory.as_deref());

        // Spawn the caller-supplied command instead of the login shell
        let shell_env = build_shell_env(config.shell_env.as_ref());
        terminal.spawn_custom_shell_with_dir(
            &command,
            Some(args.as_slice()),
            work_dir,
            shell_env.as_ref(),
        )?;

        // Create shared session logger
        let session_logger = create_shared_logger();

        let terminal = Arc::new(RwLock::new(terminal));

        Ok(Self {
            id,
            terminal,
            scroll_state: ScrollState::new(),
            mouse: MouseState::new(),
            bell: BellState::new(),
            cache: RenderCache::new(),
            refresh_task: None,
            working_directory: working_directory.or_else(|| config.working_directory.clone()),
            last_activity_time: std::time::Instant::now(),
            last_seen_generation: 0,
            anti_idle_last_activity: std::time::Instant::now(),
            anti_idle_last_generation: 0,
            silence_notified: false,
            exit_notified: false,
            session_logger,
            bounds: PaneBounds::default(),
            title: String::new(),
            has_default_title: true,
            background: PaneBackground::new(),
            restart_state: None,
            is_active: Arc::new(AtomicBool::new(false)),
            shutdown_fast: false,
        })
    }

    /// Create a primary pane that wraps an already-running terminal session.
    ///
    /// Unlike [`Pane::new`], this constructor does **not** spawn a new shell.
    /// It shares the caller's `Arc<RwLock<TerminalManager>>` directly, so the
    /// pane's terminal is the same session that `Tab::terminal` points to.
    ///
    /// Used by `Tab::new_internal` to always initialise a `PaneManager` with a
    /// single primary pane at tab creation, removing the need for tab-level
    /// `scroll_state`, `mouse`, `bell`, and `cache` fallback fields (R-32).
    ///
    /// # Arguments
    /// * `id` — Pane identifier (typically `1`)
    /// * `terminal` — Shared `Arc` cloned from the owning `Tab::terminal`
    /// * `working_directory` — Optional CWD exposed via [`Pane::get_cwd`]
    /// * `is_active` — Shared atomic flag cloned from the owning `Tab::is_active`
    pub fn new_wrapping_terminal(
        id: PaneId,
        terminal: Arc<RwLock<TerminalManager>>,
        working_directory: Option<String>,
        is_active: Arc<AtomicBool>,
    ) -> Self {
        let session_logger = create_shared_logger();

        Self {
            id,
            terminal,
            scroll_state: ScrollState::new(),
            mouse: MouseState::new(),
            bell: BellState::new(),
            cache: RenderCache::new(),
            refresh_task: None,
            working_directory,
            last_activity_time: std::time::Instant::now(),
            last_seen_generation: 0,
            anti_idle_last_activity: std::time::Instant::now(),
            anti_idle_last_generation: 0,
            silence_notified: false,
            exit_notified: false,
            session_logger,
            bounds: PaneBounds::default(),
            title: String::new(),
            has_default_title: true,
            background: PaneBackground::new(),
            restart_state: None,
            is_active,
            shutdown_fast: false,
        }
    }

    /// Create a new pane for tmux integration (no shell spawned)
    ///
    /// This creates a terminal that receives output from tmux control mode
    /// rather than a local PTY.
    pub fn new_for_tmux(
        id: PaneId,
        config: &Config,
        _runtime: Arc<Runtime>,
    ) -> anyhow::Result<Self> {
        // Create terminal with scrollback from config but don't spawn a shell
        let mut terminal = TerminalManager::new_with_scrollback(
            config.cols,
            config.rows,
            config.scrollback.scrollback_lines,
        )?;

        // Apply common terminal configuration (theme, clipboard limits, cursor style, unicode)
        configure_terminal_from_config(&mut terminal, config);

        // Don't spawn any shell - tmux provides the output
        // Create shared session logger
        let session_logger = create_shared_logger();

        let terminal = Arc::new(RwLock::new(terminal));

        Ok(Self {
            id,
            terminal,
            scroll_state: ScrollState::new(),
            mouse: MouseState::new(),
            bell: BellState::new(),
            cache: RenderCache::new(),
            refresh_task: None,
            working_directory: None,
            last_activity_time: std::time::Instant::now(),
            last_seen_generation: 0,
            anti_idle_last_activity: std::time::Instant::now(),
            anti_idle_last_generation: 0,
            silence_notified: false,
            exit_notified: false,
            session_logger,
            bounds: PaneBounds::default(),
            title: String::new(),
            has_default_title: true,
            background: PaneBackground::new(),
            restart_state: None,
            is_active: Arc::new(AtomicBool::new(false)),
            shutdown_fast: false,
        })
    }

    /// Check if the visual bell is currently active
    pub fn is_bell_active(&self) -> bool {
        if let Some(flash_start) = self.bell.visual_flash {
            flash_start.elapsed().as_millis() < VISUAL_BELL_FLASH_DURATION_MS
        } else {
            false
        }
    }

    /// Check if the terminal in this pane is still running
    pub fn is_running(&self) -> bool {
        if let Ok(term) = self.terminal.try_write() {
            let running = term.is_running();
            if !running {
                crate::debug_info!(
                    "PANE_EXIT",
                    "Pane {} terminal detected as NOT running (shell exited)",
                    self.id
                );
            }
            running
        } else {
            true // Assume running if locked
        }
    }

    /// Get the current working directory of this pane's shell
    pub fn get_cwd(&self) -> Option<String> {
        if let Ok(term) = self.terminal.try_write() {
            term.shell_integration_cwd()
        } else {
            self.working_directory.clone()
        }
    }

    /// Set per-pane background settings (overrides global config)
    pub fn set_background(&mut self, background: PaneBackground) {
        self.background = background;
    }

    /// Get per-pane background settings
    pub fn background(&self) -> &PaneBackground {
        &self.background
    }

    /// Set a per-pane background image (overrides global config)
    pub fn set_background_image(&mut self, path: Option<String>) {
        self.background.image_path = path;
    }

    /// Get the per-pane background image path (if set)
    pub fn get_background_image(&self) -> Option<&str> {
        self.background.image_path.as_deref()
    }
}

impl Drop for Pane {
    fn drop(&mut self) {
        if self.shutdown_fast {
            log::info!(
                "Fast-dropping pane {} (cleanup handled externally)",
                self.id
            );
            return;
        }

        log::info!("Dropping pane {}", self.id);

        // Stop session logging first
        if let Some(ref mut logger) = *self.session_logger.lock() {
            match logger.stop() {
                Ok(path) => {
                    log::info!("Session log saved to: {:?}", path);
                }
                Err(e) => {
                    log::warn!("Failed to stop session logging: {}", e);
                }
            }
        }

        self.stop_refresh_task();

        // Give the task time to abort
        std::thread::sleep(std::time::Duration::from_millis(50));

        // Kill the terminal
        if let Ok(mut term) = self.terminal.try_write()
            && term.is_running()
        {
            log::info!("Killing terminal for pane {}", self.id);
            let _ = term.kill();
        }
    }
}