par-term 0.30.10

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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Tab constructors: `Tab::new`, `Tab::new_from_profile`, and `Tab::new_internal`.
//!
//! Split from `tab/mod.rs` to keep that file under 500 lines. The `Tab` struct
//! definition and `Drop` impl remain in `mod.rs`; all constructor logic lives here.

use super::{Tab, TabInitParams};
use crate::config::Config;
use crate::pane::PaneManager;
use crate::prettifier::gutter::GutterManager;
use crate::profile::Profile;
use crate::session_logger::{SessionLogger, create_shared_logger};
use crate::tab::activity_state::TabActivityMonitor;
use crate::tab::initial_text::build_initial_text_payload;
use crate::tab::profile_state::TabProfileState;
use crate::tab::scripting_state::TabScriptingState;
use crate::tab::setup::{
    apply_login_shell_flag, build_shell_env, create_base_terminal, get_shell_command,
};
use crate::tab::tmux_state::TabTmuxState;
use crate::terminal::TerminalManager;
use par_term_config::TabId;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use tokio::runtime::Runtime;
use tokio::sync::RwLock;

impl Tab {
    /// Shared constructor body called by both `Tab::new()` and `Tab::new_from_profile()`.
    ///
    /// Both public constructors:
    /// 1. Create and configure a `TerminalManager` (divergent: shell command, env, login_shell)
    /// 2. Call this method to handle the identical steps:
    ///    - Coprocess auto-start loop
    ///    - Session logging setup
    ///    - `Arc<RwLock<>>` wrapping
    ///    - Initial text scheduling (only when `params.runtime` is `Some`)
    ///    - `Tab` struct construction with all shared default fields
    ///
    /// # Arguments
    /// * `params` — Constructor-specific values (title, working_directory, etc.)
    /// * `terminal` — Fully configured `TerminalManager` with PTY already spawned
    /// * `config` — Global config (used for coprocesses, session logging, prettifier)
    /// * `session_title` — Human-readable title written to the session log file header
    pub(super) fn new_internal(
        params: TabInitParams,
        terminal: TerminalManager,
        config: &Config,
        session_title: String,
    ) -> anyhow::Result<Self> {
        let cols = params.cols;

        // Sync triggers from config into the core TriggerRegistry
        let trigger_security = terminal.sync_triggers(&config.triggers);

        // Auto-start configured coprocesses via the PtySession's built-in manager
        let mut coprocess_ids = Vec::with_capacity(config.coprocesses.len());
        for coproc_config in &config.coprocesses {
            if coproc_config.auto_start {
                let core_config = par_term_emu_core_rust::coprocess::CoprocessConfig {
                    command: coproc_config.command.clone(),
                    args: coproc_config.args.clone(),
                    cwd: None,
                    env: crate::terminal::coprocess_env(),
                    copy_terminal_output: coproc_config.copy_terminal_output,
                    restart_policy: coproc_config.restart_policy.to_core(),
                    restart_delay_ms: coproc_config.restart_delay_ms,
                };
                match terminal.start_coprocess(core_config) {
                    Ok(id) => {
                        log::info!(
                            "Auto-started coprocess '{}' (id={})",
                            coproc_config.name,
                            id
                        );
                        coprocess_ids.push(Some(id));
                    }
                    Err(e) => {
                        log::warn!(
                            "Failed to auto-start coprocess '{}': {}",
                            coproc_config.name,
                            e
                        );
                        coprocess_ids.push(None);
                    }
                }
            } else {
                coprocess_ids.push(None);
            }
        }

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

        // Set up session logging if enabled
        if config.auto_log_sessions {
            let logs_dir = config.logs_dir();

            // SEC-010: Ensure the logs directory exists with owner-only permissions
            // (0o700) so session logs are not world-listable.
            if let Err(e) = std::fs::create_dir_all(&logs_dir) {
                log::warn!("Failed to create logs directory {:?}: {}", logs_dir, e);
            } else {
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    let _ =
                        std::fs::set_permissions(&logs_dir, std::fs::Permissions::from_mode(0o700));
                }
            }

            let title_with_ts = Some(format!(
                "{} - {}",
                session_title,
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S")
            ));

            match SessionLogger::new(
                config.session_log_format,
                &logs_dir,
                (config.cols, config.rows),
                title_with_ts,
            ) {
                Ok(mut logger) => {
                    logger.set_redact_passwords(config.session_log_redact_passwords);
                    if let Err(e) = logger.start() {
                        log::warn!("Failed to start session logging: {}", e);
                    } else {
                        log::info!("Session logging started: {:?}", logger.output_path());

                        // Set up output callback to record PTY output
                        let logger_clone = Arc::clone(&session_logger);
                        terminal.set_output_callback(move |data: &[u8]| {
                            if let Some(ref mut logger) = *logger_clone.lock() {
                                logger.record_output(data);
                            }
                        });

                        *session_logger.lock() = Some(logger);
                    }
                }
                Err(e) => {
                    log::warn!("Failed to create session logger: {}", e);
                }
            }
        }

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

        // Send initial text after optional delay (only when a runtime is provided)
        if let Some(runtime) = params.runtime
            && let Some(payload) =
                build_initial_text_payload(&config.initial_text, config.initial_text_send_newline)
        {
            let delay_ms = config.initial_text_delay_ms;
            let terminal_clone = Arc::clone(&terminal);
            runtime.spawn(async move {
                if delay_ms > 0 {
                    tokio::time::sleep(tokio::time::Duration::from_millis(delay_ms)).await;
                }

                let term = terminal_clone.write().await;
                if let Err(err) = term.write(&payload) {
                    log::warn!("Failed to send initial text: {}", err);
                }
            });
        }

        // Always create a PaneManager with one primary pane that wraps `tab.terminal`
        // (R-32). This eliminates the fallback scroll_state/mouse/bell/cache fields
        // that were used when pane_manager was None (single-pane mode).
        let is_active = Arc::new(AtomicBool::new(false));
        let pane_manager = PaneManager::new_with_existing_terminal(
            Arc::clone(&terminal),
            params.working_directory.clone(),
            Arc::clone(&is_active),
        );

        Ok(Self {
            id: params.id,
            terminal,
            pane_manager: Some(pane_manager),
            title: params.title,
            refresh_task: None,
            working_directory: params.working_directory,
            custom_color: None,
            has_default_title: params.has_default_title,
            user_named: params.user_named,
            activity: TabActivityMonitor::default(),
            session_logger,
            tmux: TabTmuxState::default(),
            detected_hostname: None,
            detected_cwd: None,
            custom_icon: None,
            profile: TabProfileState::default(),
            scripting: TabScriptingState {
                coprocess_ids,
                trigger_prompt_before_run: trigger_security,
                ..TabScriptingState::default()
            },
            prettifier: crate::prettifier::config_bridge::create_pipeline_from_config(
                config, cols, None,
            ),
            gutter_manager: GutterManager::new(),
            was_alt_screen: false,
            is_active,
            shutdown_fast: false,
            is_hidden: false,
        })
    }

    /// Create a new tab with a terminal session
    ///
    /// # Arguments
    /// * `id` - Unique tab identifier
    /// * `tab_number` - Display number for the tab (1-indexed)
    /// * `config` - Terminal configuration
    /// * `runtime` - Tokio runtime for async operations
    /// * `working_directory` - Optional working directory to start in
    /// * `grid_size` - Optional (cols, rows) override. When provided, uses these
    ///   dimensions instead of config.cols/rows. This ensures the shell starts
    ///   with the correct dimensions when the renderer has already calculated
    ///   the grid size accounting for tab bar height.
    pub fn new(
        id: TabId,
        tab_number: usize,
        config: &Config,
        runtime: Arc<Runtime>,
        working_directory: Option<String>,
        grid_size: Option<(usize, usize)>,
    ) -> anyhow::Result<Self> {
        // Create and configure terminal
        let (mut terminal, cols, _) = create_base_terminal(config, grid_size)?;

        // Determine working directory:
        // 1. If explicitly provided (e.g., from tab_inherit_cwd), use that
        // 2. Otherwise, use the configured startup directory based on mode
        let effective_startup_dir = config.get_effective_startup_directory();
        let work_dir = working_directory
            .as_deref()
            .or(effective_startup_dir.as_deref());

        // Get shell command and apply login shell flag
        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(),
        )?;

        // Generate initial title based on current tab count, not unique ID
        let title = format!("Tab {}", tab_number);

        Self::new_internal(
            TabInitParams {
                id,
                title,
                has_default_title: true,
                user_named: false,
                working_directory: working_directory.or_else(|| config.working_directory.clone()),
                cols,
                runtime: Some(runtime),
            },
            terminal,
            config,
            format!("Tab {}", tab_number),
        )
    }

    /// Create a new tab from a profile configuration
    ///
    /// The profile can override:
    /// - Working directory
    /// - Command and arguments (instead of default shell)
    /// - Tab name
    ///
    /// If a profile specifies a command, it always runs from the profile's working
    /// directory (or config default if unset).
    ///
    /// # Arguments
    /// * `id` - Unique tab identifier
    /// * `config` - Terminal configuration
    /// * `_runtime` - Tokio runtime (unused: profile tabs don't send initial text)
    /// * `profile` - Profile configuration to use
    /// * `grid_size` - Optional (cols, rows) override for initial terminal size
    ///
    /// # Unique logic vs `Tab::new()`
    /// This constructor's divergent logic (not shared with `Tab::new()` via `new_internal`):
    /// - SSH command detection (`profile.ssh_command_args()`)
    /// - Per-profile `login_shell` override (takes precedence over `config.login_shell`)
    /// - Per-profile `SHELL` env-var injection when `profile.shell` is set
    /// - Title derived from `profile.tab_name` → `profile.name` (not "Tab N")
    /// - Profile tabs do NOT send `config.initial_text` on startup
    pub fn new_from_profile(
        id: TabId,
        config: &Config,
        _runtime: Arc<Runtime>,
        profile: &Profile,
        grid_size: Option<(usize, usize)>,
    ) -> anyhow::Result<Self> {
        // Create and configure terminal
        let (mut terminal, cols, _) = create_base_terminal(config, grid_size)?;

        // Determine working directory: profile overrides config startup directory
        let effective_startup_dir = config.get_effective_startup_directory();
        let work_dir = profile
            .working_directory
            .as_deref()
            .or(effective_startup_dir.as_deref());

        // Determine command and args with priority:
        // 0. profile.ssh_host → build ssh command with user/port/identity args
        // 1. profile.command → use as-is (non-shell commands like tmux, ssh)
        // 2. profile.shell → use as shell, apply login_shell logic
        // 3. neither → fall back to global config shell / $SHELL
        let is_ssh_profile = profile.ssh_host.is_some();
        let (shell_cmd, mut shell_args) = if let Some(ssh_args) = profile.ssh_command_args() {
            ("ssh".to_string(), Some(ssh_args))
        } else if let Some(ref cmd) = profile.command {
            (cmd.clone(), profile.command_args.clone())
        } else if let Some(ref shell) = profile.shell {
            (shell.clone(), None)
        } else {
            get_shell_command(config)
        };

        // Apply login shell flag when using a shell (not a custom command or SSH profile).
        // Per-profile login_shell overrides global config.login_shell.
        if profile.command.is_none() && !is_ssh_profile {
            let use_login_shell = profile.login_shell.unwrap_or(config.login_shell);
            if use_login_shell {
                let args = shell_args.get_or_insert_with(Vec::new);
                #[cfg(not(target_os = "windows"))]
                if !args.iter().any(|a| a == "-l" || a == "--login") {
                    args.insert(0, "-l".to_string());
                }
            }
        }

        let shell_args_deref = shell_args.as_deref();
        let mut shell_env = build_shell_env(config.shell_env.as_ref());

        // When a profile specifies a shell, set the SHELL env var so child
        // processes (and $SHELL) reflect the selected shell, not the login shell.
        if profile.command.is_none()
            && let Some(ref shell_path) = profile.shell
            && let Some(ref mut env) = shell_env
        {
            env.insert("SHELL".to_string(), shell_path.clone());
        }

        terminal.spawn_custom_shell_with_dir(
            &shell_cmd,
            shell_args_deref,
            work_dir,
            shell_env.as_ref(),
        )?;

        // Generate title: use profile tab_name or profile name
        let title = profile
            .tab_name
            .clone()
            .unwrap_or_else(|| profile.name.clone());

        let working_directory = profile
            .working_directory
            .clone()
            .or_else(|| config.working_directory.clone());

        // Session log title uses profile name (Tab::new uses "Tab N")
        let session_title = profile.name.clone();

        Self::new_internal(
            TabInitParams {
                id,
                title,
                has_default_title: false, // Profile-created tabs have explicit names
                user_named: profile.tab_name.is_some(),
                working_directory,
                cols,
                runtime: None, // Profile tabs don't send initial_text
            },
            terminal,
            config,
            session_title,
        )
    }
}

/// Minimal stub for use in unit tests (no PTY, no runtime).
#[cfg(test)]
impl Tab {
    pub(crate) fn new_stub(id: TabId, tab_number: usize) -> Self {
        use crate::session_logger::create_shared_logger;

        // Create a dummy TerminalManager without spawning a shell
        let terminal =
            TerminalManager::new_with_scrollback(80, 24, 100).expect("stub terminal creation");
        let terminal = Arc::new(RwLock::new(terminal));
        let is_active = Arc::new(AtomicBool::new(false));
        let pane_manager = PaneManager::new_with_existing_terminal(
            Arc::clone(&terminal),
            None,
            Arc::clone(&is_active),
        );
        Self {
            id,
            terminal,
            pane_manager: Some(pane_manager),
            title: format!("Tab {}", tab_number),
            refresh_task: None,
            working_directory: None,
            custom_color: None,
            has_default_title: true,
            user_named: false,
            activity: TabActivityMonitor::default(),
            session_logger: create_shared_logger(),
            tmux: TabTmuxState::default(),
            detected_hostname: None,
            detected_cwd: None,
            custom_icon: None,
            profile: TabProfileState::default(),
            scripting: TabScriptingState::default(),
            prettifier: None,
            gutter_manager: GutterManager::new(),
            was_alt_screen: false,
            is_active,
            shutdown_fast: false,
            is_hidden: false,
        }
    }
}