kraal 0.1.1

A terminal workspace manager for SSH fleets and coding agents
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//! A pane: a process on a PTY with its terminal emulator, or a rich preview block.

use std::path::PathBuf;
use std::time::Instant;

use alacritty_terminal::event::{Event as TermEvent, EventListener, WindowSize};
use alacritty_terminal::grid::Scroll;
use alacritty_terminal::term::TermMode;
use anyhow::Result;
use crossbeam_channel::Sender;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;

use crate::agent::AgentTracker;
use crate::input::PaneInputModes;
use crate::layout::PaneId;
use crate::preview::Preview;
use crate::server::Event;

mod commands;
mod guard;
mod osc;
mod pty;
mod screen;

pub use commands::{Finished, short_duration};
use osc::sanitize_title;
use osc::{Osc, OscScanner};
pub use osc::{osc_notify, osc52};
use pty::Size;
pub use pty::{ProcessHold, TermPane};
#[cfg(test)]
pub use screen::render_term;
pub use screen::set_outer_colors;
use screen::{SearchState, default_color};

/// Every mode a program may have left set, back to how a fresh terminal starts, without RIS
/// (which would clear the scrollback a restarted pane keeps): the alternate screen, the kitty
/// keyboard stacks of both screens and the alternate screen's saved cursor, the scrolling region
/// (cursor kept), origin, cursor-key and keypad modes, wrapping, insert and newline modes, mouse
/// and focus reporting, bracketed paste and the cursor. Tab stops, character sets and colours
/// follow in [`reset_sequence`].
const RESET_MODES: &[u8] = b"\x1b[?1049l\x1b[?1049h\x1b[<4096u\x1b[=0;1u\x1b(B\x1b)B\x1b*B\x1b+B\x0f\x1b[0m\x1b7\
\x1b[?1049l\x1b[<4096u\x1b[=0;1u\
\x1b[?6l\x1b7\x1b[r\x1b8\x1b[?1l\x1b>\x1b[?7h\x1b[4l\x1b[20l\
\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1005l\x1b[?1006l\x1b[?1004l\x1b[?2004l\x1b[?1007h\
\x1b[?25h\x1b[0 q";

/// What a restarted pane's emulator gets: [`RESET_MODES`], then tab stops every 8 columns of
/// `cols` (the cursor kept), the four character sets back to ASCII with G0 in use, and the
/// colours. The character sets come after the cursor is restored, and the clean state is saved
/// last: a saved cursor carries character sets, and the old program's must not come back.
fn reset_sequence(cols: u16) -> Vec<u8> {
    let mut out = RESET_MODES.to_vec();
    out.extend_from_slice(b"\x1b7\x1b[3g");
    for col in (9..=cols).step_by(8) {
        out.extend_from_slice(format!("\x1b[{col}G\x1bH").as_bytes());
    }
    out.extend_from_slice(b"\x1b8\x1b(B\x1b)B\x1b*B\x1b+B\x0f\x1b[0m\r\n\x1b7");
    out
}

/// What to run in a pane; kept so the pane can be restarted and persisted.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct PaneSpec {
    /// Empty = the user's shell.
    pub command: Vec<String>,
    pub cwd: Option<PathBuf>,
    pub close_on_exit: bool,
    pub preview: Option<PathBuf>,
    /// Typed into the shell once it starts (used to resume agents after a restore).
    pub startup_input: Option<String>,
    /// Inventory name of the server this pane connects to, when kraal started the connection.
    pub server: Option<String>,
    /// The connection itself (user, port, jump host, key), when kraal started it: the entry
    /// named `server` may reach that machine another way.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub connection: Option<crate::servers::Server>,
    /// Close the pane a few seconds after its command succeeds (transfers).
    pub close_on_success: bool,
    /// With `close_on_exit`: stay open when the command fails, to show why (tools).
    pub keep_on_failure: bool,
    /// A two-panel file manager on these locations (`/path` or `server:path`).
    pub files: Option<[String; 2]>,
    /// Extra environment for the process, and variables to remove.
    pub env: Vec<(String, String)>,
    pub env_remove: Vec<String>,
    /// `agent:account` the pane's agent runs on, when kraal launched it.
    pub account: Option<String>,
    /// Conversation id the agent was started with, for resuming it.
    pub session: Option<String>,
    /// The git worktree kraal created for this pane's agent.
    pub worktree: Option<crate::git::Worktree>,
    /// An agent waiting for its account (the rules depend on what git says about its
    /// directory): what to start once that is known, kept across a restart.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pending_agent: Option<PendingAgent>,
    /// Environment holding secrets: given to the process only, never saved with the session.
    #[serde(skip)]
    pub secret_env: SecretEnv,
}

/// An agent of a workspace definition, before its account is chosen.
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct PendingAgent {
    pub agent: String,
    pub args: Vec<String>,
    /// The workspace whose rules apply.
    pub workspace: Option<String>,
    /// Once chosen, wait for Enter (`start_suspended`) rather than start.
    pub suspended: bool,
    pub close_on_exit: bool,
}

/// Variables whose values must not show: debug output lists their names alone.
#[derive(Clone, Default)]
pub struct SecretEnv(pub Vec<(String, String)>);

impl std::fmt::Debug for SecretEnv {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_list().entries(self.0.iter().map(|(k, _)| k)).finish()
    }
}

/// Things the server must act on after feeding output to a pane.
#[derive(Debug, Clone)]
pub enum PaneSignal {
    Notification {
        title: String,
        body: String,
    },
    Clipboard(String),
    Bell,
    TitleChanged,
    /// A command the shell marked (OSC 133) ended.
    CommandDone(Finished),
}

pub enum Content {
    Term(Box<TermPane>),
    Preview(Box<Preview>),
    Files(Box<crate::files::FilesPane>),
}

pub struct Pane {
    pub id: PaneId,
    pub name: Option<String>,
    /// Title set by the program (OSC 0/2).
    pub title: String,
    pub spec: PaneSpec,
    pub content: Content,
    pub cols: u16,
    pub rows: u16,
    pub agent: AgentTracker,
    /// Set when the process exited; the pane stays open for command panes.
    pub exit_code: Option<Option<i32>>,
    pub last_output: Instant,
    pub bell: bool,
    /// Current directory reported by the shell (OSC 7), and the machine it is on.
    pub reported_cwd: Option<PathBuf>,
    pub reported_host: String,
    /// The server this pane is connected to: from its command, or an `ssh` seen in the foreground.
    pub ssh: Option<crate::servers::Server>,
    /// What the commands run here last worked on (Kubernetes, AWS, Terraform), as seen from
    /// their command lines and environments.
    pub seen: crate::context::Seen,
    /// When to close the pane automatically.
    pub close_at: Option<Instant>,
    /// Replace the spec and restart once the current process exits (account switch).
    pub restart_with: Option<PaneSpec>,
    /// The command has not been started yet (restored session, `start_suspended`, or it failed
    /// to start): Enter starts it.
    pub suspended: bool,
    search: Option<SearchState>,
}

/// Environment given to every pane process.
pub struct PaneEnv {
    pub socket: PathBuf,
    pub session: String,
    pub shell: String,
    pub scrollback: usize,
    /// Images: whether they can be shown, the outer terminal's cell size, the session quota.
    pub graphics: std::sync::Arc<crate::graphics::Shared>,
}

impl Pane {
    pub fn spawn(
        id: PaneId,
        name: Option<String>,
        spec: PaneSpec,
        cols: u16,
        rows: u16,
        env: &PaneEnv,
        tx: &Sender<Event>,
    ) -> Result<Pane> {
        let cols = cols.max(2);
        let rows = rows.max(1);
        let content = if let Some(path) = &spec.preview {
            Content::Preview(Box::new(Preview::open(path)))
        } else {
            Content::Term(Box::new(TermPane::spawn(id, &spec, cols, rows, env, tx)?))
        };
        Ok(Pane {
            id,
            name,
            title: String::new(),
            spec,
            content,
            cols,
            rows,
            agent: AgentTracker::default(),
            exit_code: None,
            last_output: Instant::now(),
            bell: false,
            reported_cwd: None,
            reported_host: String::new(),
            seen: Default::default(),
            ssh: None,
            close_at: None,
            restart_with: None,
            suspended: false,
            search: None,
        })
    }

    /// A file manager pane.
    pub fn files(id: PaneId, name: Option<String>, spec: PaneSpec, files: crate::files::FilesPane) -> Pane {
        Pane {
            id,
            name,
            title: String::new(),
            spec,
            content: Content::Files(Box::new(files)),
            cols: 80,
            rows: 24,
            agent: AgentTracker::default(),
            exit_code: None,
            last_output: Instant::now(),
            bell: false,
            reported_cwd: None,
            reported_host: String::new(),
            seen: Default::default(),
            ssh: None,
            close_at: None,
            restart_with: None,
            suspended: false,
            search: None,
        }
    }

    /// Restart the command of an exited pane, keeping its scrollback.
    pub fn respawn(&mut self, env: &PaneEnv, tx: &Sender<Event>) -> Result<()> {
        let Content::Term(old) = &mut self.content else { return Ok(()) };
        let mut fresh = TermPane::spawn(self.id, &self.spec, self.cols, self.rows, env, tx)?;
        // Keep history: move the old emulator over and reset its modes.
        std::mem::swap(&mut fresh.term, &mut old.term);
        std::mem::swap(&mut fresh.events, &mut old.events);
        std::mem::swap(&mut fresh.commands, &mut old.commands);
        // The images its scrollback shows stay with it.
        std::mem::swap(&mut fresh.graphics, &mut old.graphics);
        let reset = reset_sequence(self.cols);
        fresh.marked(|t| t.parser.advance(&mut t.term, &reset));
        **old = fresh;
        // A new process: nothing of the old one's lifecycle carries over, but its conversation.
        self.exit_code = None;
        self.suspended = false;
        self.close_at = None;
        let session = self.agent.session_id.take();
        self.agent = AgentTracker { session_id: session, ..AgentTracker::default() };
        Ok(())
    }

    pub fn display_name(&self) -> String {
        if let Some(n) = &self.name {
            return n.clone();
        }
        match &self.content {
            Content::Preview(p) => p.title(),
            Content::Files(f) => f.title(),
            Content::Term(_) if !self.title.is_empty() => self.title.clone(),
            Content::Term(_) => self
                .spec
                .command
                .first()
                .map(|c| c.rsplit('/').next().unwrap_or(c).to_string())
                .unwrap_or_else(|| "shell".into()),
        }
    }

    pub fn is_preview(&self) -> bool {
        matches!(self.content, Content::Preview(_))
    }

    /// The preview this pane shows of `path`, if it is one.
    pub fn preview_of(&mut self, path: &std::path::Path) -> Option<&mut Preview> {
        match &mut self.content {
            Content::Preview(p) if p.path() == path => Some(p),
            _ => None,
        }
    }

    /// A process on a PTY (as opposed to a preview or a file manager).
    pub fn is_terminal(&self) -> bool {
        matches!(self.content, Content::Term(_))
    }

    pub fn term(&self) -> Option<&TermPane> {
        match &self.content {
            Content::Term(t) => Some(t),
            _ => None,
        }
    }

    pub fn term_mut(&mut self) -> Option<&mut TermPane> {
        match &mut self.content {
            Content::Term(t) => Some(t),
            _ => None,
        }
    }

    pub fn resize(&mut self, cols: u16, rows: u16) {
        let cols = cols.max(2);
        let rows = rows.max(1);
        if (cols, rows) == (self.cols, self.rows) {
            return;
        }
        self.cols = cols;
        self.rows = rows;
        if let Content::Term(t) = &mut self.content {
            let _ = t.master.resize(pty::pty_size(cols, rows, t.graphics.shared()));
            t.resize_marked(Size { cols, rows });
        }
    }

    /// Feed process output. Returns signals for the server.
    pub fn feed(&mut self, bytes: &[u8]) -> Vec<PaneSignal> {
        self.last_output = Instant::now();
        let Content::Term(t) = &mut self.content else { return Vec::new() };
        let mut signals = Vec::new();
        // The emulator gets the output up to each command mark, so that the mark lands where
        // the shell put it.
        let mut start = 0;
        for (end, osc) in t.osc.scan(bytes) {
            match osc {
                Osc::Notify { title, body } => signals.push(PaneSignal::Notification { title, body }),
                Osc::Cwd { host, path } => {
                    self.reported_cwd = Some(path);
                    self.reported_host = host;
                }
                Osc::Mark(mark) => {
                    t.advance(&bytes[start..end]);
                    start = end;
                    signals.extend(t.mark(mark).map(PaneSignal::CommandDone));
                }
            }
        }
        t.advance(&bytes[start..]);
        self.drain_events(&mut signals);
        signals
    }

    /// Tell the program the pane's size in pixels again: the outer terminal's cells changed.
    pub fn refresh_pixel_size(&mut self) {
        if let Content::Term(t) = &mut self.content {
            let _ = t.master.resize(pty::pty_size(self.cols, self.rows, t.graphics.shared()));
        }
    }

    /// Make the image placeholders `area` shows from this pane displayable, or blank when the
    /// client cannot show images.
    pub fn show_images(&self, area: Rect, buf: &mut Buffer, can_show: bool) {
        if let Content::Term(t) = &self.content {
            t.graphics.show_cells(area, buf, can_show);
        }
    }

    /// The next chunk of output read from the PTY, if any.
    pub fn take_output(&self) -> Option<Vec<u8>> {
        self.term()?.output.try_recv().ok()
    }

    pub fn has_output(&self) -> bool {
        self.term().is_some_and(|t| !t.output.is_empty())
    }

    /// Flush a synchronized update whose timeout expired: the signals of what it held (replies
    /// to queries are sent to the program right away), or None when nothing was due.
    pub fn poll_sync_timeout(&mut self) -> Option<Vec<PaneSignal>> {
        let Content::Term(t) = &mut self.content else { return None };
        match t.parser.sync_timeout().sync_timeout() {
            Some(deadline) if deadline <= Instant::now() => {
                t.marked(|t| t.parser.stop_sync(&mut t.term));
                let mut signals = Vec::new();
                self.drain_events(&mut signals);
                Some(signals)
            }
            _ => None,
        }
    }

    fn drain_events(&mut self, signals: &mut Vec<PaneSignal>) {
        let Content::Term(t) = &mut self.content else { return };
        let events: Vec<TermEvent> = t.events.borrow_mut().drain(..).collect();
        for ev in events {
            match ev {
                TermEvent::PtyWrite(s) => t.write(t.graphics.device_attributes(&s).into_bytes()),
                TermEvent::Title(s) => {
                    self.title = sanitize_title(&s);
                    signals.push(PaneSignal::TitleChanged);
                }
                TermEvent::ResetTitle => {
                    self.title.clear();
                    signals.push(PaneSignal::TitleChanged);
                }
                TermEvent::Bell => {
                    self.bell = true;
                    signals.push(PaneSignal::Bell);
                }
                TermEvent::ClipboardStore(_, text) => signals.push(PaneSignal::Clipboard(text)),
                TermEvent::ColorRequest(index, format) => {
                    let rgb = t.term.colors()[index].unwrap_or_else(|| default_color(index));
                    t.write(format(rgb).into_bytes());
                }
                TermEvent::TextAreaSizeRequest(format) => {
                    let (cell_width, cell_height) = t.graphics.shared().cell();
                    let size = WindowSize { num_lines: self.rows, num_cols: self.cols, cell_width, cell_height };
                    t.write(format(size).into_bytes());
                }
                _ => {}
            }
        }
    }

    pub fn write_input(&mut self, bytes: Vec<u8>) {
        if let Content::Term(t) = &mut self.content {
            t.term.scroll_display(Scroll::Bottom);
            t.write(bytes);
        }
    }

    pub fn input_modes(&self) -> PaneInputModes {
        let Some(t) = self.term() else { return PaneInputModes::default() };
        let m = *t.term.mode();
        PaneInputModes {
            app_cursor: m.contains(TermMode::APP_CURSOR),
            app_keypad: m.contains(TermMode::APP_KEYPAD),
            bracketed_paste: m.contains(TermMode::BRACKETED_PASTE),
            mouse_click: m.contains(TermMode::MOUSE_REPORT_CLICK),
            mouse_drag: m.contains(TermMode::MOUSE_DRAG),
            mouse_motion: m.contains(TermMode::MOUSE_MOTION),
            sgr_mouse: m.contains(TermMode::SGR_MOUSE),
            utf8_mouse: m.contains(TermMode::UTF8_MOUSE),
            focus_events: m.contains(TermMode::FOCUS_IN_OUT),
            alternate_scroll: m.contains(TermMode::ALTERNATE_SCROLL),
            alt_screen: m.contains(TermMode::ALT_SCREEN),
            kitty_flags: [
                (TermMode::DISAMBIGUATE_ESC_CODES, 1),
                (TermMode::REPORT_EVENT_TYPES, 2),
                (TermMode::REPORT_ALTERNATE_KEYS, 4),
                (TermMode::REPORT_ALL_KEYS_AS_ESC, 8),
                (TermMode::REPORT_ASSOCIATED_TEXT, 16),
            ]
            .iter()
            .filter(|(f, _)| m.contains(*f))
            .map(|(_, b)| b)
            .sum(),
        }
    }

    pub fn wants_mouse(&self) -> bool {
        self.term().is_some_and(|t| t.term.mode().intersects(TermMode::MOUSE_MODE))
    }

    /// Hang up, as closing a terminal does (SIGHUP to the program and its foreground job).
    pub fn kill(&mut self) {
        if let Content::Term(t) = &mut self.content {
            t.hang_up();
        }
    }

    /// Send `signal` to the pane's process group and its foreground job (what runs in it).
    pub fn signal(&self, signal: i32) {
        let Some(t) = self.term() else { return };
        t.process.signal_group(signal);
        #[cfg(unix)]
        if let Some(fg) = self.foreground_pid().filter(|fg| Some(*fg) != t.pid) {
            // SAFETY: plain syscall to the terminal's current foreground group.
            unsafe {
                libc::kill(-(fg as libc::pid_t), signal);
            }
        }
    }

    pub fn pid(&self) -> Option<u32> {
        self.term().and_then(|t| t.pid)
    }

    /// A hold on the pane's current process (see [`ProcessHold`]).
    pub fn process_hold(&self) -> Option<ProcessHold> {
        self.term().map(|t| ProcessHold::new(&t.process))
    }

    /// Command line of the foreground process group leader.
    pub fn foreground_argv(&self) -> Option<Vec<String>> {
        let t = self.term()?;
        let pgid = t.master.process_group_leader()?;
        crate::process::argv(pgid as u32)
    }

    /// Whether something other than the pane's shell is in the foreground.
    pub fn has_foreground_job(&self) -> bool {
        let Some(t) = self.term() else { return false };
        match (t.master.process_group_leader(), t.pid) {
            (Some(pg), Some(pid)) => pg as u32 != pid,
            _ => false,
        }
    }

    /// Whether a shell waits at its prompt here: nothing else in the foreground, and what the
    /// pane runs is a shell (not `vim` started as the pane's command, or by `exec`). When the
    /// system does not say what runs, only the foreground counts.
    pub fn shell_waiting(&self) -> bool {
        if self.has_foreground_job() {
            return false;
        }
        let program = self.pid().and_then(crate::process::argv).and_then(|a| a.into_iter().next());
        program.is_none_or(|p| is_shell(&p))
    }

    /// Best-known working directory on this machine: OSC 7, else the shell's.
    /// A directory reported by a shell on another machine (over ssh) does not count.
    pub fn current_cwd(&self) -> Option<PathBuf> {
        if let Some(p) = &self.reported_cwd
            && !self.reported_remotely()
        {
            return Some(p.clone());
        }
        crate::process::cwd(self.pid()?)
    }

    /// The directory a shell on another machine reported (a remote shell that sends OSC 7).
    pub fn remote_cwd(&self) -> Option<PathBuf> {
        if self.reported_remotely() { self.reported_cwd.clone() } else { None }
    }

    fn reported_remotely(&self) -> bool {
        let host = self.reported_host.as_str();
        if host.is_empty() || host == "localhost" {
            return false;
        }
        static LOCAL: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
        let local = LOCAL.get_or_init(crate::context::hostname);
        // Shells report either the short or the full name.
        local.as_deref().is_some_and(|l| {
            let short = |s: &str| s.split('.').next().unwrap_or(s).to_ascii_lowercase();
            short(l) != short(host)
        })
    }

    /// Process id of the foreground process group leader.
    pub fn foreground_pid(&self) -> Option<u32> {
        let t = self.term()?;
        t.master.process_group_leader().map(|p| p as u32)
    }
}

/// Open a URL with the desktop's default application.
pub fn open_url(url: &str) -> std::io::Result<()> {
    let opener = if cfg!(target_os = "macos") { "open" } else { "xdg-open" };
    std::process::Command::new(opener)
        .arg(url)
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .map(crate::exec::reap_later)
}

/// Whether `program` (an argv[0]: a path, or `-zsh` for a login shell) is a shell.
pub fn is_shell(program: &str) -> bool {
    let name = program.rsplit('/').next().unwrap_or(program).trim_start_matches('-');
    matches!(
        name,
        "sh" | "bash"
            | "zsh"
            | "fish"
            | "dash"
            | "ash"
            | "ksh"
            | "mksh"
            | "yash"
            | "tcsh"
            | "csh"
            | "nu"
            | "xonsh"
            | "elvish"
            | "pwsh"
    )
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::rc::Rc;

    use alacritty_terminal::term::{Config as TermConfig, Term};
    use alacritty_terminal::vte::ansi::{Processor, Rgb};
    use ratatui::buffer::Buffer;
    use ratatui::layout::Rect;
    use ratatui::style::{Color, Modifier};

    use super::pty::Listener;
    use super::screen::{parse_xcolor, render_term, url_in_text};
    use super::*;
    use super::{pty, reset_sequence};

    #[test]
    fn a_restarted_pane_starts_with_fresh_modes() {
        let size = pty::Size { cols: 20, rows: 6 };
        let config = TermConfig { kitty_keyboard: true, ..TermConfig::default() };
        let mut term = Term::new(config, &size, Listener::default());
        let mut parser: Processor = Processor::new();
        parser.advance(
            &mut term,
            b"\x1b[?1049h\x1b[>1u\x1b[?1049l\x1b[>3u\x1b[?1h\x1b=\x1b[?6h\x1b[2;4r\x1b[?1003h\x1b[?1004h\
              \x1b[?2004h\x1b[4h\x1b[?25l\x1b(0\x1b7\x1b[?1049h",
        );
        // Line drawing in G1, selected with SO, and a single tab stop at column 3; line drawing
        // in G0 too, kept in the alternate screen's saved cursor (the primary's is kept above).
        parser.advance(&mut term, b"\x1b)0\x0e\x1b[3g\x1b[3G\x1bH\r\x1b(0\x1b7");
        assert_ne!(*term.mode(), TermMode::default());
        parser.advance(&mut term, &reset_sequence(20));
        assert_eq!(*term.mode(), TermMode::default());
        // ASCII again, and tab stops every 8 columns.
        parser.advance(&mut term, b"\rq\tx");
        let line = term.grid().cursor.point.line;
        let row = &term.grid()[line];
        assert_eq!(row[alacritty_terminal::index::Column(0)].c, 'q');
        assert_eq!(row[alacritty_terminal::index::Column(8)].c, 'x');
        // A saved cursor brings back no character set of the old program, on either screen.
        let restored_q = |term: &mut Term<Listener>, parser: &mut Processor| {
            parser.advance(term, b"\x1b8q");
            let at = term.grid().cursor.point;
            term.grid()[at.line][at.column - 1].c
        };
        assert_eq!(restored_q(&mut term, &mut parser), 'q');
        // The other screen's keyboard modes are gone too.
        parser.advance(&mut term, b"\x1b[?1049h");
        assert_eq!(*term.mode() & !TermMode::ALT_SCREEN, TermMode::default());
        assert_eq!(restored_q(&mut term, &mut parser), 'q');
    }

    #[test]
    fn replies_held_in_a_synchronized_update_go_out_when_it_times_out() {
        let (tx, _rx) = crossbeam_channel::bounded(64);
        let env = PaneEnv {
            socket: "/nonexistent".into(),
            session: "t".into(),
            shell: "sh".into(),
            scrollback: 100,
            graphics: Default::default(),
        };
        // The program asks where the cursor is inside a synchronized update that never ends, then
        // prints the answer it reads.
        let spec = PaneSpec {
            command: vec![
                "sh".into(),
                "-c".into(),
                "stty raw -echo; printf '\\033[?2026h\\033[6n'; r=$(dd bs=1 count=6 2>/dev/null); \
                 printf 'got%s' \"$r\"; sleep 1"
                    .into(),
            ],
            ..Default::default()
        };
        let mut pane = Pane::spawn(1, None, spec, 40, 5, &env, &tx).unwrap();
        let start = Instant::now();
        let mut seen = String::new();
        while !seen.contains('R') && start.elapsed() < std::time::Duration::from_secs(5) {
            while let Some(data) = pane.take_output() {
                seen.push_str(&String::from_utf8_lossy(&data));
                pane.feed(&data);
            }
            pane.poll_sync_timeout();
            std::thread::sleep(std::time::Duration::from_millis(20));
        }
        assert!(seen.contains("got\x1b[1;1R"), "the reply never reached the program: {seen:?}");
    }

    #[test]
    fn a_command_never_runs_in_another_directory_than_its_own() {
        let (tx, _rx) = crossbeam_channel::bounded(64);
        let env = PaneEnv {
            socket: "/nonexistent".into(),
            session: "t".into(),
            shell: "sh".into(),
            scrollback: 100,
            graphics: Default::default(),
        };
        let gone = Some(PathBuf::from("/nonexistent/kraal-test-dir"));
        let command = PaneSpec { command: vec!["true".into()], cwd: gone.clone(), ..Default::default() };
        let err = Pane::spawn(1, None, command, 20, 5, &env, &tx).err().expect("refused");
        assert!(format!("{err:#}").contains("kraal-test-dir"), "{err:#}");
        let typed = PaneSpec { startup_input: Some("rm -rf build\r".into()), cwd: gone.clone(), ..Default::default() };
        assert!(Pane::spawn(2, None, typed, 20, 5, &env, &tx).is_err());
        // A plain shell starts at home instead.
        let mut shell = Pane::spawn(3, None, PaneSpec { cwd: gone, ..Default::default() }, 20, 5, &env, &tx).unwrap();
        shell.kill();
    }

    #[test]
    fn shells_are_told_from_other_programs() {
        for shell in ["zsh", "-zsh", "/bin/bash", "/usr/bin/fish", "sh"] {
            assert!(is_shell(shell), "{shell}");
        }
        for other in ["vim", "/usr/bin/nvim", "ssh", "python3", "zshell"] {
            assert!(!is_shell(other), "{other}");
        }
    }

    #[test]
    fn osc_scanner_finds_notifications_across_chunks() {
        let mut s = OscScanner::default();
        let data = b"hello\x1b]777;notify;Claude;needs you\x07 and \x1b]9;done!\x1b\\ \x1b]9;4;1;50\x07";
        let mut found = Vec::new();
        for chunk in data.chunks(3) {
            found.extend(s.scan(chunk).into_iter().map(|(_, o)| o));
        }
        assert_eq!(
            found,
            vec![
                Osc::Notify { title: "Claude".into(), body: "needs you".into() },
                Osc::Notify { title: String::new(), body: "done!".into() },
            ]
        );
    }

    #[test]
    fn osc7_cwd() {
        let mut s = OscScanner::default();
        let found: Vec<Osc> =
            s.scan(b"\x1b]7;file://laptop/home/me/My%20Dir\x07").into_iter().map(|(_, o)| o).collect();
        assert_eq!(found, vec![Osc::Cwd { host: "laptop".into(), path: PathBuf::from("/home/me/My Dir") }]);
    }

    #[test]
    fn render_copies_cells_and_styles() {
        let events = Rc::new(RefCell::new(Vec::new()));
        let mut term = Term::new(TermConfig::default(), &Size { cols: 10, rows: 3 }, Listener(events));
        let mut parser: Processor = Processor::new();
        parser.advance(&mut term, b"\x1b[1;31mhi\x1b[0m \xe6\x97\xa5x");
        let area = Rect::new(2, 1, 10, 3);
        let mut buf = Buffer::empty(Rect::new(0, 0, 14, 5));
        render_term(&term, area, &mut buf, None);
        assert_eq!(buf[(2, 1)].symbol(), "h");
        assert_eq!(buf[(2, 1)].fg, Color::Red);
        assert!(buf[(2, 1)].modifier.contains(Modifier::BOLD));
        assert_eq!(buf[(5, 1)].symbol(), "日");
        assert_eq!(buf[(7, 1)].symbol(), "x");
    }

    #[test]
    fn osc8_links_are_collected() {
        let events = Rc::new(RefCell::new(Vec::new()));
        let mut term = Term::new(TermConfig::default(), &Size { cols: 20, rows: 2 }, Listener(events));
        let mut parser: Processor = Processor::new();
        parser.advance(&mut term, b"see \x1b]8;;https://x.org\x1b\\link\x1b]8;;\x1b\\ ok");
        let mut buf = Buffer::empty(Rect::new(0, 0, 20, 2));
        let mut links = crate::ansi::Links::new();
        render_term(&term, Rect::new(0, 0, 20, 2), &mut buf, Some(&mut links));
        let mut cols: Vec<u16> = links.keys().map(|(x, _)| *x).collect();
        cols.sort();
        assert_eq!(cols, vec![4, 5, 6, 7]);
        assert_eq!(&*links[&(4, 0)], "https://x.org");
    }

    #[test]
    fn urls_in_text() {
        let line = "see https://example.org/a?b=1). and more";
        assert_eq!(url_in_text(line, 10).as_deref(), Some("https://example.org/a?b=1"));
        assert_eq!(url_in_text(line, 1), None);
        assert_eq!(url_in_text("(http://x.y/z)", 3).as_deref(), Some("http://x.y/z"));
        assert_eq!(url_in_text("nothing here", 3), None);
    }

    #[test]
    fn urls_are_found_under_wide_characters_columns() {
        let events = Rc::new(RefCell::new(Vec::new()));
        let mut term = Term::new(TermConfig::default(), &Size { cols: 30, rows: 1 }, Listener(events));
        let mut parser: Processor = Processor::new();
        parser.advance(&mut term, "日本 https://example.org".as_bytes());
        let (line, index) = super::screen::row_chars(&term, alacritty_terminal::index::Line(0));
        // The last column of the URL (two wide characters before it take four columns).
        let col = 4 + "https://example.org".len();
        assert_eq!(url_in_text(&line, index[col]).as_deref(), Some("https://example.org"));
        assert_eq!(url_in_text(&line, index[1]), None);
    }

    #[test]
    fn xcolor_parsing() {
        assert_eq!(parse_xcolor("rgb:ffff/8080/0000"), Some(Rgb { r: 255, g: 128, b: 0 }));
        assert_eq!(parse_xcolor("rgb:f/8/0"), Some(Rgb { r: 255, g: 136, b: 0 }));
        assert_eq!(parse_xcolor("#fff"), None);
    }

    #[test]
    fn default_palette_is_xterm_like() {
        assert_eq!(default_color(16), Rgb { r: 0, g: 0, b: 0 });
        assert_eq!(default_color(231), Rgb { r: 255, g: 255, b: 255 });
        assert_eq!(default_color(232), Rgb { r: 8, g: 8, b: 8 });
    }
}