par-term 0.49.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
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
//! The par-mux half of the unified session picker (UX.md A16, A22).
//!
//! Plain data and the egui section only — the socket I/O that fills a
//! [`MuxDirectory`] lives in the mux-gated `mux_directory` module, so this
//! file compiles in every build and the picker draws the same with or
//! without par-mux support (the section is simply absent without it).
//!
//! A session is identified by the daemon socket that serves it plus its
//! name: a daemon can hold several sessions, and a session whose name is
//! not its daemon's name is still reached through that daemon.

use egui::RichText;
use std::path::{Path, PathBuf};

/// One session a running daemon holds.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MuxSessionRow {
    /// The daemon socket that serves the session.
    pub socket: PathBuf,
    /// The daemon's name for the socket (`par-mux-<name>.sock`).
    pub daemon: String,
    /// Session id (`$N`) within its daemon.
    pub id: u64,
    /// Session name.
    pub name: String,
    /// The workspace that owns the session (carried by `list-sessions`
    /// rows since the workspace-aware wire).
    pub workspace_name: String,
}

impl MuxSessionRow {
    /// Whether the next launch can reattach this session: restore stores
    /// the session name and reaches it through `par-mux-<name>.sock`, so
    /// only a session named after its own daemon round-trips.
    pub fn survives_restore(&self) -> bool {
        self.daemon == self.name
    }
}

/// One workspace a running daemon holds.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MuxWorkspaceRow {
    /// The daemon socket that serves the workspace.
    pub socket: PathBuf,
    /// The daemon's name for the socket (`par-mux-<name>.sock`).
    pub daemon: String,
    /// Workspace id (`+N`) within its daemon.
    pub id: u64,
    /// Workspace name.
    pub name: String,
    /// The daemon's active workspace (the one `select-workspace` resumes).
    pub active: bool,
}

/// What a directory scan found: sessions, plus one message per daemon that
/// could not be read (a broken daemon is visible, not silently absent).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MuxDirectory {
    pub sessions: Vec<MuxSessionRow>,
    pub workspaces: Vec<MuxWorkspaceRow>,
    pub errors: Vec<String>,
}

/// A session operation chosen in the picker or the palette.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MuxPickerAction {
    /// Attach this window to a listed session (switching if attached).
    Attach(MuxSessionRow),
    /// Create a new session and attach this window to it.
    Create(String),
    /// Rename a listed session daemon-side.
    Rename(MuxSessionRow, String),
    /// End a listed session (`kill-session`), after confirmation.
    Kill(MuxSessionRow),
    /// Detach this window from its session.
    Detach,
    /// Create a named workspace on a listed daemon.
    NewWorkspace {
        socket: PathBuf,
        daemon: String,
        name: String,
    },
    /// Land on a listed daemon's workspace: daemon-side select, and for
    /// this window's own daemon, follow to the resumed session.
    SwitchWorkspace(MuxWorkspaceRow),
    /// Rename a listed workspace daemon-side.
    RenameWorkspace(MuxWorkspaceRow, String),
    /// End a listed workspace (`kill-workspace`), after confirmation.
    KillWorkspace(MuxWorkspaceRow),
}

/// The daemon name a socket path encodes, or `None` for a file that is not
/// a par-mux socket.
pub fn daemon_name_of(path: &Path) -> Option<String> {
    let file = path.file_name()?.to_str()?;
    let stem = file.strip_prefix("par-mux-")?.strip_suffix(".sock")?;
    (!stem.is_empty()).then(|| stem.to_string())
}

/// A name for a new session that no listed session or daemon already uses:
/// `session-1`, `session-2`, …
pub fn free_session_name(existing: &[MuxSessionRow]) -> String {
    (1..)
        .map(|n| format!("session-{n}"))
        .find(|candidate| {
            !existing
                .iter()
                .any(|row| &row.name == candidate || &row.daemon == candidate)
        })
        .unwrap_or_else(|| "session".to_string())
}

/// Why `name` cannot name a new session, or `None` when it can. The name
/// becomes the daemon's socket file name and a wire token.
pub fn session_name_refusal(name: &str) -> Option<&'static str> {
    if name.is_empty() {
        return Some("a session name is required");
    }
    if name
        .chars()
        .any(|c| c.is_whitespace() || matches!(c, '/' | '\\' | '\'' | '"' | ':'))
    {
        return Some("a session name cannot contain spaces, quotes, slashes, or colons");
    }
    None
}

/// Why `name` cannot name a workspace, or `None` when it can. Workspace
/// names ride the wire quoted (the `new-workspace -n` grammar), so only
/// the line-framed wire itself constrains them.
pub fn workspace_name_refusal(name: &str) -> Option<&'static str> {
    if name.is_empty() {
        return Some("a workspace name is required");
    }
    if name.chars().any(|c| c == '\n' || c == '\r') {
        return Some("a workspace name cannot contain line breaks");
    }
    None
}

/// What the picker's par-mux section shows this frame.
pub struct MuxPickerInput<'a> {
    /// The last finished scan, or `None` before the first one lands.
    pub directory: Option<&'a MuxDirectory>,
    /// A scan is in flight.
    pub loading: bool,
    /// `(daemon, session)` this window is attached to, if any.
    pub attached: Option<(&'a str, &'a str)>,
}

/// Per-picker editing state for the par-mux section.
#[derive(Default)]
pub struct MuxPickerSection {
    new_name: String,
    /// Row being renamed, with its edit buffer.
    renaming: Option<(MuxSessionRow, String)>,
    /// The rename field was just opened: its opening click is not a click
    /// away, and focus is requested once (OV4).
    rename_first_frame: bool,
    /// Row whose End button was pressed once; a second press ends it.
    confirm_kill: Option<MuxSessionRow>,
    /// Refusal for the typed new-session name, shown under the field.
    name_error: Option<&'static str>,
    /// Workspace being renamed, with its edit buffer.
    ws_renaming: Option<(MuxWorkspaceRow, String)>,
    /// The workspace rename field was just opened (OV4).
    ws_rename_first_frame: bool,
    /// Workspace whose End was pressed once; a second press ends it.
    ws_confirm_kill: Option<MuxWorkspaceRow>,
    /// The daemon a pending new-workspace name targets: `(socket, daemon,
    /// edit buffer)` while the inline new-workspace prompt is open.
    ws_new_target: Option<(PathBuf, String, String)>,
    /// Refusal for the typed new-workspace name, shown under the field.
    ws_name_error: Option<&'static str>,
}

impl MuxPickerSection {
    /// Reset per-open state.
    pub fn reset(&mut self) {
        *self = Self::default();
    }

    /// Draw the par-mux section and return the chosen operation, if any.
    pub fn show(
        &mut self,
        ui: &mut egui::Ui,
        input: &MuxPickerInput<'_>,
    ) -> Option<MuxPickerAction> {
        let mut action = None;
        ui.heading("par-mux Sessions");
        ui.separator();

        if let Some((daemon, session)) = input.attached {
            ui.horizontal(|ui| {
                ui.label(RichText::new(format!("This window: {session}")).strong());
                if daemon != session {
                    ui.label(RichText::new(format!("(daemon {daemon})")).weak());
                }
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    if ui
                        .button("Detach")
                        .on_hover_text("Leave the session running in the daemon")
                        .clicked()
                    {
                        action = Some(MuxPickerAction::Detach);
                    }
                });
            });
            ui.add_space(4.0);
        }

        let sessions: &[MuxSessionRow] = input.directory.map_or(&[], |d| &d.sessions);
        if sessions.is_empty() {
            if input.loading || input.directory.is_none() {
                // UX.md OV10: the list loads off the frame; show it working.
                ui.horizontal(|ui| {
                    ui.spinner();
                    ui.label(RichText::new("Loading sessions...").italics());
                });
            } else {
                ui.label(RichText::new("No par-mux sessions running").italics());
            }
        }
        for row in sessions {
            if let Some(chosen) = self.show_row(ui, row, input.attached) {
                action = Some(chosen);
            }
        }

        // Workspaces subsection, grouped per daemon: each daemon's
        // workspaces with Switch/Rename/End and a New affordance —
        // create/switch/kill workspaces end to end from par-term.
        if let Some(dir) = input.directory {
            let mut seen: Vec<PathBuf> = Vec::new();
            for ws in &dir.workspaces {
                if seen.contains(&ws.socket) {
                    continue;
                }
                seen.push(ws.socket.clone());
                ui.add_space(8.0);
                ui.separator();
                ui.label(
                    RichText::new(format!("Workspaces — {}", ws.daemon))
                        .weak()
                        .small(),
                );
                for w in dir.workspaces.iter().filter(|w| w.socket == ws.socket) {
                    if let Some(chosen) =
                        self.show_workspace_row(ui, w, input.attached, &dir.sessions)
                    {
                        action = Some(chosen);
                    }
                }
                if let Some(chosen) = self.show_new_workspace_row(ui, &ws.socket, &ws.daemon) {
                    action = Some(chosen);
                }
            }
        }
        if let Some(dir) = input.directory {
            for error in &dir.errors {
                ui.colored_label(crate::app::overlay::theme::DANGER, error);
            }
        }

        ui.add_space(8.0);
        ui.horizontal(|ui| {
            ui.label("New session:");
            let response = ui.add(
                egui::TextEdit::singleline(&mut self.new_name)
                    .desired_width(160.0)
                    .hint_text(free_session_name(sessions)),
            );
            let submit = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
            if ui.button("Create").clicked() || submit {
                let name = if self.new_name.trim().is_empty() {
                    free_session_name(sessions)
                } else {
                    self.new_name.trim().to_string()
                };
                match session_name_refusal(&name) {
                    Some(reason) => self.name_error = Some(reason),
                    None => {
                        self.name_error = None;
                        action = Some(MuxPickerAction::Create(name));
                    }
                }
            }
        });
        if let Some(reason) = self.name_error {
            ui.colored_label(crate::app::overlay::theme::DANGER, reason);
        }
        action
    }

    fn show_row(
        &mut self,
        ui: &mut egui::Ui,
        row: &MuxSessionRow,
        attached: Option<(&str, &str)>,
    ) -> Option<MuxPickerAction> {
        let mut action = None;
        let is_here = attached == Some((row.daemon.as_str(), row.name.as_str()));
        if let Some((target, buffer)) = self.renaming.as_mut()
            && target == row
        {
            // UX.md OV4: Enter submits, Escape or a click away cancels.
            use crate::app::overlay::inline_edit::{self, InlineEditOutcome};
            let first_frame = std::mem::take(&mut self.rename_first_frame);
            let mut outcome = InlineEditOutcome::Editing;
            ui.horizontal(|ui| {
                let response = ui.add(egui::TextEdit::singleline(buffer).desired_width(160.0));
                outcome = inline_edit::resolve(ui, &response, first_frame);
                if ui.button("Cancel").clicked() {
                    outcome = InlineEditOutcome::Cancel;
                }
            });
            match outcome {
                InlineEditOutcome::Submit => {
                    action = Some(MuxPickerAction::Rename(
                        row.clone(),
                        buffer.trim().to_string(),
                    ));
                    self.renaming = None;
                }
                InlineEditOutcome::Cancel => self.renaming = None,
                InlineEditOutcome::Editing => {}
            }
            return action;
        }
        ui.horizontal(|ui| {
            let name = if is_here {
                RichText::new(&row.name).strong()
            } else {
                RichText::new(&row.name)
            };
            ui.label(name);
            if !row.survives_restore() {
                ui.label(RichText::new(format!("(daemon {})", row.daemon)).weak())
                    .on_hover_text("Attaches now; not reattached on the next launch");
            }
            if is_here {
                ui.label(RichText::new("(this window)").color(crate::app::overlay::theme::SUCCESS));
            }
            ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                let armed = self.confirm_kill.as_ref() == Some(row);
                let end = if armed { "Confirm End" } else { "End" };
                let end_btn = ui
                    .button(RichText::new(end).color(crate::app::overlay::theme::DANGER))
                    .on_hover_text("End the session and every program in it");
                if end_btn.clicked() {
                    if armed {
                        self.confirm_kill = None;
                        action = Some(MuxPickerAction::Kill(row.clone()));
                    } else {
                        self.confirm_kill = Some(row.clone());
                    }
                }
                if ui.button("Rename").clicked() {
                    self.confirm_kill = None;
                    self.renaming = Some((row.clone(), row.name.clone()));
                    self.rename_first_frame = true;
                }
                if !is_here && ui.button("Attach").clicked() {
                    self.confirm_kill = None;
                    action = Some(MuxPickerAction::Attach(row.clone()));
                }
            });
        });
        action
    }

    /// One workspace row: name with the active marker, then Switch /
    /// Rename / End on the right.
    fn show_workspace_row(
        &mut self,
        ui: &mut egui::Ui,
        ws: &MuxWorkspaceRow,
        attached: Option<(&str, &str)>,
        sessions: &[MuxSessionRow],
    ) -> Option<MuxPickerAction> {
        let mut action = None;
        let ours = attached.is_some_and(|(daemon, _)| daemon == ws.daemon);
        if let Some((target, buffer)) = self.ws_renaming.as_mut()
            && target == ws
        {
            // UX.md OV4: Enter submits, Escape or a click away cancels.
            use crate::app::overlay::inline_edit::{self, InlineEditOutcome};
            let first_frame = std::mem::take(&mut self.ws_rename_first_frame);
            let mut outcome = InlineEditOutcome::Editing;
            ui.horizontal(|ui| {
                let response = ui.add(egui::TextEdit::singleline(buffer).desired_width(160.0));
                outcome = inline_edit::resolve(ui, &response, first_frame);
                if ui.button("Cancel").clicked() {
                    outcome = InlineEditOutcome::Cancel;
                }
            });
            match outcome {
                InlineEditOutcome::Submit => {
                    action = Some(MuxPickerAction::RenameWorkspace(
                        ws.clone(),
                        buffer.trim().to_string(),
                    ));
                    self.ws_renaming = None;
                }
                InlineEditOutcome::Cancel => self.ws_renaming = None,
                InlineEditOutcome::Editing => {}
            }
            return action;
        }
        ui.horizontal(|ui| {
            let label = if ws.active {
                RichText::new(format!("{} (active)", ws.name)).strong()
            } else {
                RichText::new(&ws.name)
            };
            ui.label(label);
            if ours {
                ui.label(RichText::new("(this daemon)").weak());
            }
            let session_count = sessions
                .iter()
                .filter(|s| s.socket == ws.socket && s.workspace_name == ws.name)
                .count();
            if session_count > 0 {
                ui.label(
                    RichText::new(format!(
                        "({session_count} session{})",
                        if session_count == 1 { "" } else { "s" }
                    ))
                    .weak(),
                );
            }
            ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                let armed = self.ws_confirm_kill.as_ref() == Some(ws);
                let end = if armed { "Confirm End" } else { "End" };
                if ui
                    .button(RichText::new(end).color(crate::app::overlay::theme::DANGER))
                    .on_hover_text("End the workspace and every session in it")
                    .clicked()
                {
                    if armed {
                        self.ws_confirm_kill = None;
                        action = Some(MuxPickerAction::KillWorkspace(ws.clone()));
                    } else {
                        self.ws_confirm_kill = Some(ws.clone());
                    }
                }
                if ui.button("Rename").clicked() {
                    self.ws_confirm_kill = None;
                    self.ws_renaming = Some((ws.clone(), ws.name.clone()));
                    self.ws_rename_first_frame = true;
                }
                if !ws.active && ui.button("Switch").clicked() {
                    self.ws_confirm_kill = None;
                    action = Some(MuxPickerAction::SwitchWorkspace(ws.clone()));
                }
            });
        });
        action
    }

    /// The per-daemon new-workspace affordance: a button that opens an
    /// inline name prompt, and the prompt row while it is open.
    fn show_new_workspace_row(
        &mut self,
        ui: &mut egui::Ui,
        socket: &Path,
        daemon: &str,
    ) -> Option<MuxPickerAction> {
        let open = self
            .ws_new_target
            .as_ref()
            .is_some_and(|(s, d, _)| s == socket && d == daemon);
        if !open {
            if ui.button("New workspace").clicked() {
                self.ws_new_target =
                    Some((socket.to_path_buf(), daemon.to_string(), String::new()));
                self.ws_name_error = None;
            }
            return None;
        }
        let mut buffer = self
            .ws_new_target
            .as_ref()
            .map(|(_, _, b)| b.clone())
            .unwrap_or_default();
        let mut chosen: Option<MuxPickerAction> = None;
        ui.horizontal(|ui| {
            let response = ui.add(
                egui::TextEdit::singleline(&mut buffer)
                    .desired_width(160.0)
                    .hint_text("workspace name"),
            );
            let submit = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter));
            if ui.button("Create").clicked() || submit {
                let name = buffer.trim().to_string();
                match workspace_name_refusal(&name) {
                    Some(reason) => self.ws_name_error = Some(reason),
                    None => {
                        self.ws_new_target = None;
                        chosen = Some(MuxPickerAction::NewWorkspace {
                            socket: socket.to_path_buf(),
                            daemon: daemon.to_string(),
                            name,
                        });
                    }
                }
            }
            if ui.button("Cancel").clicked() {
                self.ws_new_target = None;
            }
        });
        if let Some((_, _, b)) = self.ws_new_target.as_mut() {
            *b = buffer;
        }
        if let Some(reason) = self.ws_name_error {
            ui.colored_label(crate::app::overlay::theme::DANGER, reason);
        }
        chosen
    }
}

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

    fn row(daemon: &str, name: &str) -> MuxSessionRow {
        MuxSessionRow {
            socket: PathBuf::from(format!("/s/par-mux-{daemon}.sock")),
            daemon: daemon.to_string(),
            id: 0,
            name: name.to_string(),
            workspace_name: String::new(),
        }
    }

    #[test]
    fn socket_names_decode_to_their_daemon() {
        assert_eq!(
            daemon_name_of(Path::new("/tmp/x/par-mux-work.sock")),
            Some("work".to_string())
        );
        assert_eq!(daemon_name_of(Path::new("/tmp/x/par-mux-.sock")), None);
        assert_eq!(daemon_name_of(Path::new("/tmp/x/other.sock")), None);
        assert_eq!(daemon_name_of(Path::new("/tmp/x/par-mux-work.state")), None);
    }

    #[test]
    fn free_names_skip_taken_sessions_and_daemons() {
        assert_eq!(free_session_name(&[]), "session-1");
        assert_eq!(
            free_session_name(&[row("session-1", "session-1"), row("x", "session-2")]),
            "session-3"
        );
    }

    #[test]
    fn session_names_that_cannot_be_socket_names_are_refused() {
        assert!(session_name_refusal("").is_some());
        assert!(session_name_refusal("a/b").is_some());
        assert!(session_name_refusal("a b").is_some());
        assert!(session_name_refusal("it's").is_some());
        assert_eq!(session_name_refusal("work-2"), None);
    }

    #[test]
    fn only_a_session_named_after_its_daemon_survives_restore() {
        assert!(row("work", "work").survives_restore());
        assert!(!row("default", "work").survives_restore());
    }
}