par-term-config 0.11.0

Configuration system for par-term terminal emulator
Documentation
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
//! Core `Profile` struct and its direct implementation.

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::dynamic::ProfileSource;

/// Unique identifier for a profile
pub type ProfileId = Uuid;

/// How to connect this profile to a tmux session when it opens.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum TmuxConnectionMode {
    /// Full par-term integration via `tmux -CC` (control mode)
    #[default]
    ControlMode,
    /// Plain tmux running in the PTY — no par-term integration
    Normal,
}

impl TmuxConnectionMode {
    /// Returns true if this is the default ControlMode (used for skip_serializing_if)
    pub fn is_control_mode(v: &Self) -> bool {
        *v == Self::ControlMode
    }
}

/// A terminal session profile containing configuration for how to start a session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Profile {
    /// Unique identifier for this profile
    pub id: ProfileId,

    /// Display name for the profile
    pub name: String,

    /// Working directory for the session (if None, uses config default or inherits)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub working_directory: Option<String>,

    /// Shell to use for this profile (e.g. "/bin/zsh", "/usr/bin/fish")
    /// When set, overrides the global custom_shell / $SHELL for this profile.
    /// Takes precedence over global config but is overridden by `command`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shell: Option<String>,

    /// Per-profile login shell override.
    /// None = inherit global config.login_shell, Some(true/false) = override.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub login_shell: Option<bool>,

    /// Command to run instead of the default shell
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,

    /// Arguments for the command
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub command_args: Option<Vec<String>>,

    /// Custom tab name (if None, uses default naming)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tab_name: Option<String>,

    /// Icon identifier for the profile (emoji or icon name)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,

    /// Display order in the profile list
    #[serde(default)]
    pub order: usize,

    /// Searchable tags to organize and filter profiles
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,

    /// Parent profile ID for inheritance (child overrides parent settings)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<ProfileId>,

    /// Keyboard shortcut for quick launch (e.g., "Cmd+1", "Ctrl+Shift+1")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub keyboard_shortcut: Option<String>,

    /// Hostname patterns for automatic profile switching when SSH connects
    /// Supports glob patterns (e.g., "*.example.com", "server-*")
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hostname_patterns: Vec<String>,

    /// Tmux session name patterns for automatic profile switching when connecting via tmux control mode
    /// Supports glob patterns (e.g., "work-*", "dev-session", "*-production")
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tmux_session_patterns: Vec<String>,

    /// tmux session to auto-connect to when this profile is opened.
    /// Uses create-or-attach semantics (tmux new-session -A -s <name>).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tmux_session_name: Option<String>,

    /// How to connect: control mode (full integration) or normal (plain tmux in PTY).
    #[serde(default, skip_serializing_if = "TmuxConnectionMode::is_control_mode")]
    pub tmux_connection_mode: TmuxConnectionMode,

    /// Directory patterns for automatic profile switching based on CWD
    /// Supports glob patterns (e.g., "/Users/*/projects/work-*", "/home/user/dev/*")
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub directory_patterns: Vec<String>,

    /// Per-profile badge text (overrides global badge_format when this profile is active)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_text: Option<String>,

    /// Per-profile badge color [R, G, B] (overrides global badge_color)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_color: Option<[u8; 3]>,

    /// Per-profile badge opacity 0.0-1.0 (overrides global badge_color_alpha)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_color_alpha: Option<f32>,

    /// Per-profile badge font family (overrides global badge_font)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_font: Option<String>,

    /// Per-profile badge font bold (overrides global badge_font_bold)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_font_bold: Option<bool>,

    /// Per-profile badge top margin in pixels (overrides global badge_top_margin)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_top_margin: Option<f32>,

    /// Per-profile badge right margin in pixels (overrides global badge_right_margin)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_right_margin: Option<f32>,

    /// Per-profile badge max width as fraction 0.0-1.0 (overrides global badge_max_width)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_max_width: Option<f32>,

    /// Per-profile badge max height as fraction 0.0-1.0 (overrides global badge_max_height)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub badge_max_height: Option<f32>,

    /// Per-profile background shader path/name (overrides global while profile is active)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shader: Option<String>,

    /// Per-profile shader brightness override
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shader_brightness: Option<f32>,

    /// Per-profile shader text opacity override
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shader_text_opacity: Option<f32>,

    /// Per-profile shader animation speed override
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shader_animation_speed: Option<f32>,

    /// Per-profile shader texture set for iChannel0-3
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub shader_texture_set: Option<[Option<String>; 4]>,

    /// SSH hostname for direct connection (profile acts as SSH bookmark)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_host: Option<String>,

    /// SSH user for direct connection
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_user: Option<String>,

    /// SSH port for direct connection
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_port: Option<u16>,

    /// SSH identity file path for direct connection
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_identity_file: Option<String>,

    /// Extra SSH arguments (e.g., "-o StrictHostKeyChecking=no")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_extra_args: Option<String>,

    /// Where this profile was loaded from (runtime-only, not persisted to YAML)
    #[serde(skip)]
    pub source: ProfileSource,
}

impl Profile {
    /// Create a new profile with the given name
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: name.into(),
            working_directory: None,
            shell: None,
            login_shell: None,
            command: None,
            command_args: None,
            tab_name: None,
            icon: None,
            order: 0,
            tags: Vec::new(),
            parent_id: None,
            keyboard_shortcut: None,
            hostname_patterns: Vec::new(),
            tmux_session_patterns: Vec::new(),
            tmux_session_name: None,
            tmux_connection_mode: TmuxConnectionMode::default(),
            directory_patterns: Vec::new(),
            badge_text: None,
            badge_color: None,
            badge_color_alpha: None,
            badge_font: None,
            badge_font_bold: None,
            badge_top_margin: None,
            badge_right_margin: None,
            badge_max_width: None,
            badge_max_height: None,
            shader: None,
            shader_brightness: None,
            shader_text_opacity: None,
            shader_animation_speed: None,
            shader_texture_set: None,
            ssh_host: None,
            ssh_user: None,
            ssh_port: None,
            ssh_identity_file: None,
            ssh_extra_args: None,
            source: ProfileSource::default(),
        }
    }

    /// Create a profile with a specific ID (for testing or deserialization)
    pub fn with_id(id: ProfileId, name: impl Into<String>) -> Self {
        Self {
            id,
            name: name.into(),
            working_directory: None,
            shell: None,
            login_shell: None,
            command: None,
            command_args: None,
            tab_name: None,
            icon: None,
            order: 0,
            tags: Vec::new(),
            parent_id: None,
            keyboard_shortcut: None,
            hostname_patterns: Vec::new(),
            tmux_session_patterns: Vec::new(),
            tmux_session_name: None,
            tmux_connection_mode: TmuxConnectionMode::default(),
            directory_patterns: Vec::new(),
            badge_text: None,
            badge_color: None,
            badge_color_alpha: None,
            badge_font: None,
            badge_font_bold: None,
            badge_top_margin: None,
            badge_right_margin: None,
            badge_max_width: None,
            badge_max_height: None,
            shader: None,
            shader_brightness: None,
            shader_text_opacity: None,
            shader_animation_speed: None,
            shader_texture_set: None,
            ssh_host: None,
            ssh_user: None,
            ssh_port: None,
            ssh_identity_file: None,
            ssh_extra_args: None,
            source: ProfileSource::default(),
        }
    }

    /// Builder method to set working directory
    pub fn working_directory(mut self, dir: impl Into<String>) -> Self {
        self.working_directory = Some(dir.into());
        self
    }

    /// Builder method to set shell
    pub fn shell(mut self, shell: impl Into<String>) -> Self {
        self.shell = Some(shell.into());
        self
    }

    /// Builder method to set per-profile login shell
    pub fn login_shell(mut self, login: bool) -> Self {
        self.login_shell = Some(login);
        self
    }

    /// Builder method to set command
    pub fn command(mut self, cmd: impl Into<String>) -> Self {
        self.command = Some(cmd.into());
        self
    }

    /// Builder method to set command arguments
    pub fn command_args(mut self, args: Vec<String>) -> Self {
        self.command_args = Some(args);
        self
    }

    /// Builder method to set tab name
    pub fn tab_name(mut self, name: impl Into<String>) -> Self {
        self.tab_name = Some(name.into());
        self
    }

    /// Builder method to set icon
    pub fn icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Builder method to set order
    pub fn order(mut self, order: usize) -> Self {
        self.order = order;
        self
    }

    /// Builder method to set tags
    pub fn tags(mut self, tags: Vec<String>) -> Self {
        self.tags = tags;
        self
    }

    /// Builder method to set parent profile ID
    pub fn parent_id(mut self, parent_id: ProfileId) -> Self {
        self.parent_id = Some(parent_id);
        self
    }

    /// Builder method to set keyboard shortcut
    pub fn keyboard_shortcut(mut self, shortcut: impl Into<String>) -> Self {
        self.keyboard_shortcut = Some(shortcut.into());
        self
    }

    /// Builder method to set hostname patterns
    pub fn hostname_patterns(mut self, patterns: Vec<String>) -> Self {
        self.hostname_patterns = patterns;
        self
    }

    /// Builder method to set tmux session patterns
    pub fn tmux_session_patterns(mut self, patterns: Vec<String>) -> Self {
        self.tmux_session_patterns = patterns;
        self
    }

    /// Builder method to set tmux session name for auto-connect
    pub fn tmux_session_name(mut self, name: impl Into<String>) -> Self {
        self.tmux_session_name = Some(name.into());
        self
    }

    /// Builder method to set tmux connection mode
    pub fn tmux_connection_mode(mut self, mode: TmuxConnectionMode) -> Self {
        self.tmux_connection_mode = mode;
        self
    }

    /// Builder method to set directory patterns
    pub fn directory_patterns(mut self, patterns: Vec<String>) -> Self {
        self.directory_patterns = patterns;
        self
    }

    /// Builder method to set badge text
    pub fn badge_text(mut self, text: impl Into<String>) -> Self {
        self.badge_text = Some(text.into());
        self
    }

    /// Builder method to set badge color
    pub fn badge_color(mut self, color: [u8; 3]) -> Self {
        self.badge_color = Some(color);
        self
    }

    /// Builder method to set badge color alpha
    pub fn badge_color_alpha(mut self, alpha: f32) -> Self {
        self.badge_color_alpha = Some(alpha);
        self
    }

    /// Builder method to set badge font
    pub fn badge_font(mut self, font: impl Into<String>) -> Self {
        self.badge_font = Some(font.into());
        self
    }

    /// Builder method to set badge font bold
    pub fn badge_font_bold(mut self, bold: bool) -> Self {
        self.badge_font_bold = Some(bold);
        self
    }

    /// Builder method to set badge top margin
    pub fn badge_top_margin(mut self, margin: f32) -> Self {
        self.badge_top_margin = Some(margin);
        self
    }

    /// Builder method to set badge right margin
    pub fn badge_right_margin(mut self, margin: f32) -> Self {
        self.badge_right_margin = Some(margin);
        self
    }

    /// Builder method to set badge max width
    pub fn badge_max_width(mut self, width: f32) -> Self {
        self.badge_max_width = Some(width);
        self
    }

    /// Builder method to set badge max height
    pub fn badge_max_height(mut self, height: f32) -> Self {
        self.badge_max_height = Some(height);
        self
    }

    /// Builder method to set SSH host
    pub fn ssh_host(mut self, host: impl Into<String>) -> Self {
        self.ssh_host = Some(host.into());
        self
    }

    /// Builder method to set SSH user
    pub fn ssh_user(mut self, user: impl Into<String>) -> Self {
        self.ssh_user = Some(user.into());
        self
    }

    /// Builder method to set SSH port
    pub fn ssh_port(mut self, port: u16) -> Self {
        self.ssh_port = Some(port);
        self
    }

    /// Build the SSH command arguments for this profile's SSH connection.
    /// Returns None if ssh_host is not set.
    pub fn ssh_command_args(&self) -> Option<Vec<String>> {
        let host = self.ssh_host.as_ref()?;
        let mut args = Vec::new();

        if let Some(port) = self.ssh_port
            && port != 22
        {
            args.push("-p".to_string());
            args.push(port.to_string());
        }

        if let Some(ref identity) = self.ssh_identity_file {
            args.push("-i".to_string());
            args.push(identity.clone());
        }

        if let Some(ref extra) = self.ssh_extra_args {
            args.extend(extra.split_whitespace().map(String::from));
        }

        let target = if let Some(ref user) = self.ssh_user {
            format!("{}@{}", user, host)
        } else {
            host.clone()
        };
        args.push(target);

        Some(args)
    }

    /// Get the display label (icon + name if icon exists)
    pub fn display_label(&self) -> String {
        if let Some(icon) = &self.icon {
            format!("{} {}", icon, self.name)
        } else {
            self.name.clone()
        }
    }

    /// Validate the profile configuration
    /// Returns a list of validation warnings (not errors - profiles can be incomplete)
    pub fn validate(&self) -> Vec<String> {
        let mut warnings = Vec::new();

        if self.name.trim().is_empty() {
            warnings.push("Profile name is empty".to_string());
        }

        if let Some(dir) = &self.working_directory
            && !dir.is_empty()
            && !std::path::Path::new(dir).exists()
        {
            warnings.push(format!("Working directory does not exist: {}", dir));
        }

        warnings
    }
}

impl Default for Profile {
    fn default() -> Self {
        Self::new("New Profile")
    }
}