par-term 0.48.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
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
//! Automatic profile switching (UX.md PR6).
//!
//! Hostname rules (OSC 7, usually over SSH) and directory rules (OSC 7 cwd)
//! apply a matching profile to a tab. The rules:
//!
//! - **Every tab is evaluated**, not only the active one: a background tab
//!   that SSHes somewhere switches too.
//! - **A pinned tab never switches.** Pin profile on the tab context menu
//!   sets it, and an explicit Change Tab Profile pins the tab — explicit
//!   user selection wins (PROFILES.md).
//! - **Every switch is announced** with a toast carrying an Undo button.
//!   Undo reverts the tab and declines that profile until its rule stops
//!   matching, so the next frame does not re-apply it.
//! - **Hostname outranks directory.** A host match replaces a directory
//!   match; a directory match never replaces a host match.
//!
//! The per-tab badge (B60): the switched profile's `badge_text` becomes the
//! tab's `badge_override`, which the badge renderer reads for the active
//! tab; a revert restores what the tab had.
//!
//! Profile commands go through the confirmation queue, targeted at the
//! matched tab (`profile_command`).

use crate::tab::{AutoApply, AutoRule, TabId, TitleChange};

use super::super::window_state::WindowState;
use super::profile_command::ProfileCommand;

/// One tab's rule inputs for this frame.
#[derive(Debug, Clone, Default)]
pub(crate) struct TabRuleInputs {
    /// A hostname change this frame: `Some(Some(host))` remote,
    /// `Some(None)` back to local, `None` unchanged.
    pub(crate) hostname: Option<Option<String>>,
    /// A cwd change this frame.
    pub(crate) cwd: Option<String>,
}

/// What the evaluator decided for one tab.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum AutoDecision {
    Nothing,
    Apply {
        rule: AutoRule,
        profile_id: crate::profile::ProfileId,
        reason: String,
    },
    Revert,
}

/// Decide what one tab's rule inputs do, from the tab's state and the
/// matching profile for each input. Pure, so tests drive it without a PTY.
pub(crate) fn decide(
    state: &crate::tab::TabProfileState,
    inputs: &TabRuleInputs,
    host_match: Option<crate::profile::ProfileId>,
    dir_match: Option<crate::profile::ProfileId>,
    host_switch_enabled: bool,
) -> AutoDecision {
    if state.pinned {
        return AutoDecision::Nothing;
    }
    // Hostname rules first.
    match &inputs.hostname {
        Some(None) if state.auto_applied_profile_id.is_some() => return AutoDecision::Revert,
        Some(Some(host)) if host_switch_enabled => match host_match {
            Some(id) if Some(id) == state.declined_profile_id => {}
            Some(id) if Some(id) != state.auto_applied_profile_id => {
                return AutoDecision::Apply {
                    rule: AutoRule::Host,
                    profile_id: id,
                    reason: format!("host {host}"),
                };
            }
            Some(_) => {}
            None if state.auto_applied_profile_id.is_some() => return AutoDecision::Revert,
            None => {}
        },
        _ => {}
    }
    // A host-applied profile outranks every directory rule.
    if state.auto_applied_profile_id.is_some() {
        return AutoDecision::Nothing;
    }
    if let Some(cwd) = &inputs.cwd {
        return match dir_match {
            Some(id) if Some(id) == state.declined_profile_id => AutoDecision::Nothing,
            Some(id) if Some(id) != state.auto_applied_dir_profile_id => AutoDecision::Apply {
                rule: AutoRule::Directory,
                profile_id: id,
                reason: format!("directory {cwd}"),
            },
            Some(_) => AutoDecision::Nothing,
            None if state.auto_applied_dir_profile_id.is_some() => AutoDecision::Revert,
            None => AutoDecision::Nothing,
        };
    }
    AutoDecision::Nothing
}

impl WindowState {
    /// Evaluate automatic profile switching for every tab (UX.md PR6).
    /// Returns true when any tab changed, triggering a redraw.
    pub fn check_auto_profile_switch(&mut self) -> bool {
        if self.overlay_ui.profile_manager.is_empty() {
            return false;
        }
        let host_switch_enabled = self.config.load().ssh.ssh_auto_profile_switch;
        let active = self.tab_manager.active_tab_id();
        let tab_ids: Vec<TabId> = self.tab_manager.tabs().iter().map(|t| t.id).collect();
        let mut changed = false;
        for tab_id in tab_ids {
            let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
                continue;
            };
            // Tmux gateway tabs switch through the gateway's session rule.
            if tab.tmux.tmux_gateway_active {
                continue;
            }
            // A background tab is re-read only after new output: its OSC 7
            // cannot change otherwise, and the reads below lock its PTY
            // session on the event loop. try_read: contention skips a frame.
            if Some(tab_id) != active {
                let Some(generation) = tab.terminal.try_read().ok().map(|t| t.update_generation())
                else {
                    continue;
                };
                if generation == tab.profile.evaluated_generation {
                    continue;
                }
                tab.profile.evaluated_generation = generation;
            }
            let inputs = TabRuleInputs {
                hostname: tab.check_hostname_change().map(Some).or_else(|| {
                    // `check_hostname_change` reports only remote hosts; a
                    // return to localhost reads as no hostname with a
                    // host-applied profile still set.
                    (tab.detected_hostname.is_none()
                        && tab.profile.auto_applied_profile_id.is_some())
                    .then_some(None)
                }),
                cwd: tab.check_cwd_change(),
            };
            let manager = &self.overlay_ui.profile_manager;
            let host_match = match &inputs.hostname {
                Some(Some(host)) => manager.find_by_hostname(host).map(|p| p.id),
                _ => None,
            };
            let dir_match = inputs
                .cwd
                .as_deref()
                .and_then(|cwd| manager.find_by_directory(cwd))
                .map(|p| p.id);
            // A declined profile is honoured only while its rule matches.
            if tab.profile.declined_profile_id.is_some()
                && (inputs.hostname.is_some() || inputs.cwd.is_some())
                && tab.profile.declined_profile_id != host_match
                && tab.profile.declined_profile_id != dir_match
            {
                tab.profile.declined_profile_id = None;
            }
            match decide(
                &tab.profile,
                &inputs,
                host_match,
                dir_match,
                host_switch_enabled,
            ) {
                AutoDecision::Nothing => {}
                AutoDecision::Revert => {
                    self.revert_auto_profile(tab_id);
                    changed = true;
                }
                AutoDecision::Apply {
                    rule,
                    profile_id,
                    reason,
                } => {
                    self.apply_auto_profile(tab_id, rule, profile_id, reason);
                    changed = true;
                }
            }
        }
        changed
    }

    /// Apply `profile_id` to tab `tab_id` as an automatic switch: tab state,
    /// title, per-tab badge, the profile command (targeted, confirmed), and
    /// the toast with Undo.
    pub(crate) fn apply_auto_profile(
        &mut self,
        tab_id: TabId,
        rule: AutoRule,
        profile_id: crate::profile::ProfileId,
        reason: String,
    ) {
        let Some(profile) = self.overlay_ui.profile_manager.get(&profile_id).cloned() else {
            return;
        };
        let is_active = self.tab_manager.active_tab_id() == Some(tab_id);
        let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
            return;
        };
        if let Some(stale) = tab.profile.pending_command_id.take() {
            self.withdraw_profile_command(stale);
        }
        let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
            return;
        };
        let title = profile
            .tab_name
            .clone()
            .unwrap_or_else(|| profile.name.clone());
        let change = tab.profile.apply_auto(
            rule,
            AutoApply {
                profile_id,
                title,
                icon: profile.icon.clone(),
                badge_text: profile.badge_text.clone(),
                reason: reason.clone(),
            },
            &tab.title.clone(),
        );
        if let TitleChange::Set(title) = change {
            tab.set_title(&title);
        }
        if rule == AutoRule::Host {
            tab.profile.ssh_auto_switched = true;
        }
        crate::debug_info!(
            "PROFILE",
            "Auto-applied profile '{}' to tab {} ({})",
            profile.name,
            tab_id,
            reason
        );
        let command_id = self.queue_profile_command(
            ProfileCommand {
                profile_id,
                profile_name: &profile.name,
                command: profile.command.as_deref(),
                command_args: profile.command_args.as_deref(),
                remote_origin: profile.source.is_dynamic(),
                match_reason: &reason,
            },
            Some(tab_id),
        );
        if let Some(tab) = self.tab_manager.get_tab_mut(tab_id) {
            tab.profile.pending_command_id = command_id;
        }
        if is_active {
            self.apply_profile_badge(&profile);
        }
        self.post_toast(
            crate::app::overlay::toast::ToastKind::Info,
            format!("Profile {} applied ({reason})", profile.name),
            Some(crate::app::overlay::toast::ToastAction {
                label: "Undo".to_string(),
                action_id: format!("{UNDO_PROFILE_SWITCH}{tab_id}"),
            }),
        );
        log::info!(
            "Auto-applied profile '{}' to tab {tab_id} ({reason})",
            profile.name
        );
    }

    /// Revert tab `tab_id`'s automatic profile, restoring its title, icon,
    /// badge, and withdrawing a still-pending profile command.
    pub(crate) fn revert_auto_profile(&mut self, tab_id: TabId) {
        let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
            return;
        };
        let pending = tab.profile.pending_command_id.take();
        if let TitleChange::Set(title) = tab.profile.revert_auto() {
            tab.set_title(&title);
        }
        let source = tab.profile.source_profile_id;
        crate::debug_info!("PROFILE", "Reverted auto-applied profile on tab {tab_id}");
        if let Some(id) = pending {
            self.withdraw_profile_command(id);
        }
        if self.tab_manager.active_tab_id() == Some(tab_id) {
            self.sync_active_tab_profile(source);
        }
    }

    /// The toast's Undo (`undo_profile_switch:<tab id>`): revert the tab and
    /// decline the profile until its rule stops matching.
    pub(crate) fn undo_profile_switch(&mut self, tab_id: TabId) -> bool {
        let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
            return false;
        };
        let undone = tab
            .profile
            .auto_applied_profile_id
            .or(tab.profile.auto_applied_dir_profile_id);
        if undone.is_none() {
            return false;
        }
        tab.profile.declined_profile_id = undone;
        self.revert_auto_profile(tab_id);
        self.request_redraw();
        true
    }

    /// The par-mux analogue of the tmux gateway's session rule (UX.md PR5):
    /// the window attached to a par-mux session, so a profile whose
    /// `mux_session_patterns` match the session name applies to the attached
    /// tab — the active tab at attach time. A pinned active tab keeps its
    /// profile: an explicit pin outranks the rule, which also keeps an
    /// explicit profile open (it pins the tab) from being overridden.
    pub(crate) fn apply_mux_session_profile(&mut self, session_name: &str) {
        if self
            .tab_manager
            .active_tab()
            .is_some_and(|tab| tab.profile.pinned)
        {
            return;
        }
        let Some(tab_id) = self.tab_manager.active_tab_id() else {
            return;
        };
        let Some(matched) = self
            .overlay_ui
            .profile_manager
            .find_by_mux_session(session_name)
            .map(|p| (p.id, p.name.clone()))
        else {
            crate::debug_info!(
                "PROFILE",
                "No profile matches par-mux session '{session_name}' - consider adding mux_session_patterns to a profile"
            );
            return;
        };
        crate::debug_info!(
            "PROFILE",
            "Auto-switching to profile '{}' for par-mux session '{session_name}'",
            matched.1
        );
        self.apply_auto_profile(
            tab_id,
            AutoRule::Host,
            matched.0,
            format!("par-mux session {session_name}"),
        );
    }

    /// Re-point the window-wide profile surfaces (badge settings,
    /// `session.profile_name`) at the active tab's profile after it changed
    /// or reverted. `None` restores the configured badge.
    pub(crate) fn sync_active_tab_profile(&mut self, profile: Option<crate::profile::ProfileId>) {
        let effective = self
            .tab_manager
            .active_tab()
            .and_then(|t| t.profile.effective_profile_id())
            .or(profile);
        match effective.and_then(|id| self.overlay_ui.profile_manager.get(&id).cloned()) {
            Some(profile) => self.apply_profile_badge(&profile),
            None => {
                let config = self.config.load_full();
                self.badge_state.update_config(&config);
                self.badge_state
                    .set_format(config.badge.badge_format.clone());
                self.badge_state.variables_mut().profile_name = "Default".to_string();
                self.badge_state.mark_dirty();
            }
        }
    }
}

/// The toast Undo action prefix; the rest is the tab id.
pub(crate) const UNDO_PROFILE_SWITCH: &str = "undo_profile_switch:";

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tab::TabProfileState;

    fn host(h: &str) -> TabRuleInputs {
        TabRuleInputs {
            hostname: Some(Some(h.to_string())),
            cwd: None,
        }
    }

    fn dir(d: &str) -> TabRuleInputs {
        TabRuleInputs {
            hostname: None,
            cwd: Some(d.to_string()),
        }
    }

    const P1: uuid::Uuid = uuid::Uuid::from_u128(1);
    const P2: uuid::Uuid = uuid::Uuid::from_u128(2);

    #[test]
    fn a_host_match_applies_with_its_reason() {
        let d = decide(
            &TabProfileState::default(),
            &host("prod"),
            Some(P1),
            None,
            true,
        );
        assert_eq!(
            d,
            AutoDecision::Apply {
                rule: AutoRule::Host,
                profile_id: P1,
                reason: "host prod".to_string()
            }
        );
    }

    #[test]
    fn a_pinned_tab_does_not_auto_switch() {
        // UX.md MP3 acceptance: a pinned tab does not auto-switch.
        let pinned = TabProfileState {
            pinned: true,
            ..Default::default()
        };
        assert_eq!(
            decide(&pinned, &host("prod"), Some(P1), None, true),
            AutoDecision::Nothing
        );
        assert_eq!(
            decide(&pinned, &dir("/work"), None, Some(P1), true),
            AutoDecision::Nothing
        );
    }

    #[test]
    fn host_switching_off_leaves_hosts_alone_but_not_directories() {
        let s = TabProfileState::default();
        assert_eq!(
            decide(&s, &host("prod"), Some(P1), None, false),
            AutoDecision::Nothing
        );
        assert!(matches!(
            decide(&s, &dir("/w"), None, Some(P2), false),
            AutoDecision::Apply { .. }
        ));
    }

    #[test]
    fn a_host_profile_outranks_directory_rules() {
        let s = TabProfileState {
            auto_applied_profile_id: Some(P1),
            ..Default::default()
        };
        assert_eq!(
            decide(&s, &dir("/w"), None, Some(P2), true),
            AutoDecision::Nothing
        );
    }

    #[test]
    fn returning_to_localhost_or_leaving_a_matching_directory_reverts() {
        let host_applied = TabProfileState {
            auto_applied_profile_id: Some(P1),
            ..Default::default()
        };
        let back_home = TabRuleInputs {
            hostname: Some(None),
            cwd: None,
        };
        assert_eq!(
            decide(&host_applied, &back_home, None, None, true),
            AutoDecision::Revert
        );
        let dir_applied = TabProfileState {
            auto_applied_dir_profile_id: Some(P2),
            ..Default::default()
        };
        assert_eq!(
            decide(&dir_applied, &dir("/elsewhere"), None, None, true),
            AutoDecision::Revert
        );
    }

    #[test]
    fn an_undone_profile_is_not_reapplied_while_its_rule_matches() {
        let declined = TabProfileState {
            declined_profile_id: Some(P1),
            ..Default::default()
        };
        assert_eq!(
            decide(&declined, &host("prod"), Some(P1), None, true),
            AutoDecision::Nothing
        );
        // A different profile still applies.
        assert!(matches!(
            decide(&declined, &host("other"), Some(P2), None, true),
            AutoDecision::Apply { profile_id: P2, .. }
        ));
    }

    #[test]
    fn the_same_profile_is_not_reapplied() {
        let s = TabProfileState {
            auto_applied_profile_id: Some(P1),
            ..Default::default()
        };
        assert_eq!(
            decide(&s, &host("prod"), Some(P1), None, true),
            AutoDecision::Nothing
        );
    }
}