par-term-settings-ui 0.12.2

Settings UI for par-term terminal emulator
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
//! Action list display, drag-to-reorder, delete, clone, and edit-state population.

use crate::SettingsUI;
use crate::input_tab::capture_key_combo;
use par_term_config::snippets::CustomActionConfig;

use super::action_editor::show_action_edit_form;

/// Show the actions section containing the prefix key config and the action list.
pub fn show_actions_section(
    ui: &mut egui::Ui,
    settings: &mut SettingsUI,
    changes_this_frame: &mut bool,
    collapsed: &mut std::collections::HashSet<String>,
) {
    crate::section::collapsing_section(
        ui,
        "Custom Actions",
        "actions_list",
        true,
        collapsed,
        |ui| {
            ui.label("Custom actions for shell commands, text insertion, or key sequences.");
            ui.add_space(4.0);

            ui.horizontal(|ui| {
                ui.add_sized(
                    [80.0, ui.spacing().interact_size.y],
                    egui::Label::new(egui::RichText::new("Prefix key:").strong()),
                );

                if settings.recording_custom_action_prefix_key {
                    ui.label(egui::RichText::new("🔴 Recording...").color(egui::Color32::RED));
                    if let Some(combo) = capture_key_combo(ui) {
                        settings.custom_action_prefix_key_recorded_combo = Some(combo.clone());
                        settings.config.custom_action_prefix_key = combo;
                        settings.recording_custom_action_prefix_key = false;
                        settings.has_changes = true;
                        *changes_this_frame = true;
                    }
                } else {
                    if ui
                        .text_edit_singleline(&mut settings.config.custom_action_prefix_key)
                        .changed()
                    {
                        settings.has_changes = true;
                        *changes_this_frame = true;
                    }

                    if ui
                        .small_button("🎤")
                        .on_hover_text("Record prefix key")
                        .clicked()
                    {
                        settings.recording_custom_action_prefix_key = true;
                        settings.custom_action_prefix_key_recorded_combo = None;
                    }
                }

                ui.label(
                    egui::RichText::new("Press this first, then a per-action prefix char")
                        .small()
                        .color(egui::Color32::GRAY),
                );
            });

            if !settings.config.custom_action_prefix_key.trim().is_empty() {
                if let Some(conflict) = settings
                    .check_keybinding_conflict(&settings.config.custom_action_prefix_key, None)
                {
                    ui.label(
                        egui::RichText::new(format!("⚠️ {}", conflict))
                            .color(egui::Color32::from_rgb(255, 180, 0))
                            .small(),
                    );
                }

                if settings.config.tmux_enabled
                    && settings.config.custom_action_prefix_key == settings.config.tmux_prefix_key
                {
                    ui.label(
                        egui::RichText::new(
                            "⚠️ Matches the tmux prefix key while tmux integration is enabled",
                        )
                        .color(egui::Color32::from_rgb(255, 180, 0))
                        .small(),
                    );
                }
            }

            ui.add_space(8.0);

            // Collect mutations to apply after iteration
            let mut delete_index: Option<usize> = None;
            let mut clone_index: Option<usize> = None;
            let mut start_edit_index: Option<usize> = None;

            let action_count = settings.config.actions.len();
            if action_count > 0 {
                egui::Frame::group(ui.style())
                    .inner_margin(egui::Margin::symmetric(8, 6))
                    .show(ui, |ui| {
                        // List existing actions
                        for i in 0..action_count {
                            let action = &settings.config.actions[i];
                            let is_editing =
                                settings.editing_action_index == Some(i) && !settings.adding_new_action;

                            if is_editing {
                                // Show inline edit form for this action
                                show_action_edit_form(ui, settings, changes_this_frame, Some(i));
                            } else {
                                let type_label = match action {
                                    CustomActionConfig::ShellCommand { .. } => "Shell".to_string(),
                                    CustomActionConfig::NewTab { .. } => "NewTab".to_string(),
                                    CustomActionConfig::InsertText { .. } => "Text".to_string(),
                                    CustomActionConfig::KeySequence { .. } => "Keys".to_string(),
                                    CustomActionConfig::SplitPane {
                                        direction,
                                        split_percent,
                                        ..
                                    } => {
                                        let dir = match direction {
                                            par_term_config::snippets::ActionSplitDirection::Horizontal => {
                                                "horiz"
                                            }
                                            par_term_config::snippets::ActionSplitDirection::Vertical => "vert",
                                        };
                                        format!("Split-{}-{}", dir, split_percent)
                                    }
                                    CustomActionConfig::Sequence { steps, .. } => {
                                        format!("Sequence ({} steps)", steps.len())
                                    }
                                    CustomActionConfig::Condition { check, .. } => {
                                        let check_label = match check {
                                            par_term_config::snippets::ConditionCheck::ExitCode { .. } => "exit_code",
                                            par_term_config::snippets::ConditionCheck::OutputContains { .. } => "output_contains",
                                            par_term_config::snippets::ConditionCheck::EnvVar { .. } => "env_var",
                                            par_term_config::snippets::ConditionCheck::DirMatches { .. } => "dir_matches",
                                            par_term_config::snippets::ConditionCheck::GitBranch { .. } => "git_branch",
                                        };
                                        format!("Condition ({})", check_label)
                                    }
                                    CustomActionConfig::Repeat { count, .. } => {
                                        format!("Repeat \u{d7}{}", count)
                                    }
                                };
                                let detail_text = match action {
                                    CustomActionConfig::ShellCommand { command, .. } => {
                                        command.to_string()
                                    }
                                    CustomActionConfig::NewTab { command, .. } => {
                                        command.clone().unwrap_or_default()
                                    }
                                    CustomActionConfig::InsertText { text, .. } => text.clone(),
                                    CustomActionConfig::KeySequence { keys, .. } => {
                                        format!("[{}]", keys)
                                    }
                                    CustomActionConfig::SplitPane { command, .. } => {
                                        command.clone().unwrap_or_default()
                                    }
                                    CustomActionConfig::Sequence { .. } => String::new(),
                                    CustomActionConfig::Condition { .. } => String::new(),
                                    CustomActionConfig::Repeat { action_id, .. } => {
                                        action_id.clone()
                                    }
                                };

                                // Reserve a fixed area for action buttons so the text segment
                                // can't push them outside the visible row.
                                ui.horizontal(|ui| {
                                    let button_area_width = 165.0;
                                    let row_height = ui.spacing().interact_size.y;
                                    let text_area_width =
                                        (ui.available_width() - button_area_width).max(0.0);

                                    ui.allocate_ui_with_layout(
                                        egui::vec2(text_area_width, row_height),
                                        egui::Layout::left_to_right(egui::Align::Center),
                                        |ui| {
                                            ui.label(egui::RichText::new(action.title()).strong());
                                            ui.label(
                                                egui::RichText::new(format!("[{}]", type_label))
                                                    .monospace()
                                                    .color(egui::Color32::from_rgb(150, 150, 200)),
                                            );
                                            if let Some(ch) = action.prefix_char() {
                                                ui.label(
                                                    egui::RichText::new(format!("pre:{}", ch))
                                                        .monospace()
                                                        .color(egui::Color32::from_rgb(100, 200, 100)),
                                                );
                                            }

                                            if !detail_text.is_empty() {
                                                ui.add(
                                                    egui::Label::new(
                                                        egui::RichText::new(detail_text)
                                                            .monospace()
                                                            .color(egui::Color32::GRAY),
                                                    )
                                                    .truncate(),
                                                );
                                            }
                                        },
                                    );

                                    ui.allocate_ui_with_layout(
                                        egui::vec2(button_area_width, row_height),
                                        egui::Layout::right_to_left(egui::Align::Center),
                                        |ui| {
                                            if ui
                                                .small_button(
                                                    egui::RichText::new("Delete")
                                                        .color(egui::Color32::from_rgb(200, 80, 80)),
                                                )
                                                .clicked()
                                            {
                                                delete_index = Some(i);
                                            }

                                            if ui
                                                .small_button("Clone")
                                                .on_hover_text("Duplicate this action")
                                                .clicked()
                                            {
                                                clone_index = Some(i);
                                            }

                                            if ui.small_button("Edit").clicked() {
                                                start_edit_index = Some(i);
                                            }
                                        },
                                    );
                                });
                            }
                        }
                    });
                ui.add_space(6.0);
            }

            // Apply mutations after iteration
            if let Some(i) = delete_index {
                settings.config.actions.remove(i);
                settings.has_changes = true;
                *changes_this_frame = true;
                // Reset editing state if we deleted the item being edited
                if settings.editing_action_index == Some(i) {
                    settings.editing_action_index = None;
                    settings.adding_new_action = false;
                }
            }

            if let Some(i) = clone_index {
                let cloned = clone_action(&settings.config.actions[i]);
                settings.config.actions.insert(i + 1, cloned);
                settings.has_changes = true;
                *changes_this_frame = true;
            }

            if let Some(i) = start_edit_index {
                populate_edit_fields(settings, i);
            }

            ui.separator();

            // Add new action button or form
            if settings.adding_new_action {
                show_action_edit_form(ui, settings, changes_this_frame, None);
            } else if ui.button("+ Add Action").clicked() {
                settings.adding_new_action = true;
                settings.editing_action_index = None;
                // Clear temp fields
                settings.temp_action_id = format!("action_{}", uuid::Uuid::new_v4());
                settings.temp_action_title = String::new();
                settings.temp_action_type = 0;
                settings.temp_action_command = String::new();
                settings.temp_action_args = String::new();
                settings.temp_action_new_tab_command = String::new();
                settings.temp_action_text = String::new();
                settings.temp_action_keys = String::new();
                settings.temp_action_keybinding = String::new();
                settings.temp_action_prefix_char = String::new();
                settings.temp_action_split_direction = 0;
                settings.temp_action_split_command = String::new();
                settings.temp_action_split_command_is_direct = false;
                settings.temp_action_split_focus_new = true;
                settings.temp_action_split_delay_ms = 200;
                settings.temp_action_split_percent = 66;
                settings.temp_action_keybinding_enabled = true;
                settings.temp_action_steps = Vec::new();
                settings.temp_action_check_type = 0;
                settings.temp_action_check_value = String::new();
                settings.temp_action_case_sensitive = false;
                settings.temp_action_env_name = String::new();
                settings.temp_action_env_value = String::new();
                settings.temp_action_env_check_existence = false;
                settings.temp_action_on_true_id = String::new();
                settings.temp_action_on_false_id = String::new();
                settings.temp_action_repeat_action_id = String::new();
                settings.temp_action_repeat_count = 3;
                settings.temp_action_repeat_delay_ms = 0;
                settings.temp_action_stop_on_success = false;
                settings.temp_action_stop_on_failure = false;
                settings.temp_action_capture_output = false;
            }
        },
    );
}

/// Populate temporary edit fields from an existing action at `index`.
fn populate_edit_fields(settings: &mut SettingsUI, index: usize) {
    settings.editing_action_index = Some(index);
    settings.adding_new_action = false;
    // Populate temp fields with current values
    let action = &settings.config.actions[index];
    settings.temp_action_id = action.id().to_string();
    settings.temp_action_title = action.title().to_string();
    settings.temp_action_keybinding = action.keybinding().unwrap_or_default().to_string();
    settings.temp_action_prefix_char = action
        .prefix_char()
        .map(|c| c.to_string())
        .unwrap_or_default();
    match action {
        CustomActionConfig::ShellCommand {
            command,
            args,
            capture_output,
            notify_on_success: _,
            ..
        } => {
            settings.temp_action_type = 0;
            settings.temp_action_command = command.clone();
            settings.temp_action_args = args.join(" ");
            settings.temp_action_capture_output = *capture_output;
        }
        CustomActionConfig::NewTab { command, .. } => {
            settings.temp_action_type = 1;
            settings.temp_action_new_tab_command = command.clone().unwrap_or_default();
        }
        CustomActionConfig::InsertText { text, .. } => {
            settings.temp_action_type = 2;
            settings.temp_action_text = text.clone();
        }
        CustomActionConfig::KeySequence { keys, .. } => {
            settings.temp_action_type = 3;
            settings.temp_action_keys = keys.clone();
        }
        CustomActionConfig::SplitPane {
            direction,
            command,
            command_is_direct,
            focus_new_pane,
            delay_ms,
            split_percent,
            ..
        } => {
            settings.temp_action_type = 4;
            settings.temp_action_split_direction = match direction {
                par_term_config::snippets::ActionSplitDirection::Horizontal => 0,
                par_term_config::snippets::ActionSplitDirection::Vertical => 1,
            };
            settings.temp_action_split_command = command.clone().unwrap_or_default();
            settings.temp_action_split_command_is_direct = *command_is_direct;
            settings.temp_action_split_focus_new = *focus_new_pane;
            settings.temp_action_split_delay_ms = *delay_ms;
            settings.temp_action_split_percent = *split_percent;
        }
        CustomActionConfig::Sequence {
            steps,
            keybinding_enabled,
            ..
        } => {
            settings.temp_action_type = 5;
            settings.temp_action_keybinding_enabled = *keybinding_enabled;
            settings.temp_action_steps = steps
                .iter()
                .map(|s| (s.action_id.clone(), s.delay_ms, s.on_failure))
                .collect();
            // Reset non-sequence fields
            settings.temp_action_check_type = 0;
            settings.temp_action_check_value = String::new();
            settings.temp_action_case_sensitive = false;
            settings.temp_action_env_name = String::new();
            settings.temp_action_env_value = String::new();
            settings.temp_action_env_check_existence = false;
            settings.temp_action_on_true_id = String::new();
            settings.temp_action_on_false_id = String::new();
            settings.temp_action_repeat_action_id = String::new();
            settings.temp_action_repeat_count = 3;
            settings.temp_action_repeat_delay_ms = 0;
            settings.temp_action_stop_on_success = false;
            settings.temp_action_stop_on_failure = false;
        }
        CustomActionConfig::Condition {
            check,
            on_true_id,
            on_false_id,
            keybinding_enabled,
            ..
        } => {
            settings.temp_action_type = 6;
            settings.temp_action_keybinding_enabled = *keybinding_enabled;
            match check {
                par_term_config::snippets::ConditionCheck::ExitCode { value } => {
                    settings.temp_action_check_type = 0;
                    settings.temp_action_check_value = value.to_string();
                    settings.temp_action_case_sensitive = false;
                }
                par_term_config::snippets::ConditionCheck::OutputContains {
                    pattern,
                    case_sensitive,
                } => {
                    settings.temp_action_check_type = 1;
                    settings.temp_action_check_value = pattern.clone();
                    settings.temp_action_case_sensitive = *case_sensitive;
                }
                par_term_config::snippets::ConditionCheck::EnvVar { name, value } => {
                    settings.temp_action_check_type = 2;
                    settings.temp_action_env_name = name.clone();
                    settings.temp_action_env_check_existence = value.is_none();
                    settings.temp_action_env_value = value.clone().unwrap_or_default();
                }
                par_term_config::snippets::ConditionCheck::DirMatches { pattern } => {
                    settings.temp_action_check_type = 3;
                    settings.temp_action_check_value = pattern.clone();
                }
                par_term_config::snippets::ConditionCheck::GitBranch { pattern } => {
                    settings.temp_action_check_type = 4;
                    settings.temp_action_check_value = pattern.clone();
                }
            }
            settings.temp_action_on_true_id = on_true_id.clone().unwrap_or_default();
            settings.temp_action_on_false_id = on_false_id.clone().unwrap_or_default();
            // Reset non-condition fields
            settings.temp_action_steps = Vec::new();
            settings.temp_action_env_name = if settings.temp_action_check_type != 2 {
                String::new()
            } else {
                settings.temp_action_env_name.clone()
            };
            settings.temp_action_repeat_action_id = String::new();
            settings.temp_action_repeat_count = 3;
            settings.temp_action_repeat_delay_ms = 0;
            settings.temp_action_stop_on_success = false;
            settings.temp_action_stop_on_failure = false;
        }
        CustomActionConfig::Repeat {
            action_id,
            count,
            delay_ms,
            stop_on_success,
            stop_on_failure,
            keybinding_enabled,
            ..
        } => {
            settings.temp_action_type = 7;
            settings.temp_action_keybinding_enabled = *keybinding_enabled;
            settings.temp_action_repeat_action_id = action_id.clone();
            settings.temp_action_repeat_count = *count;
            settings.temp_action_repeat_delay_ms = *delay_ms;
            settings.temp_action_stop_on_success = *stop_on_success;
            settings.temp_action_stop_on_failure = *stop_on_failure;
            // Reset non-repeat fields
            settings.temp_action_steps = Vec::new();
            settings.temp_action_check_type = 0;
            settings.temp_action_check_value = String::new();
            settings.temp_action_case_sensitive = false;
            settings.temp_action_env_name = String::new();
            settings.temp_action_env_value = String::new();
            settings.temp_action_env_check_existence = false;
            settings.temp_action_on_true_id = String::new();
            settings.temp_action_on_false_id = String::new();
        }
    }
}

/// Create a duplicate of `action` with a fresh id, title suffixed with "-copy",
/// and keybinding/prefix_char cleared to avoid immediate conflicts.
pub fn clone_action(action: &CustomActionConfig) -> CustomActionConfig {
    let new_id = format!("action_{}", uuid::Uuid::new_v4());
    match action {
        CustomActionConfig::ShellCommand {
            title,
            command,
            args,
            notify_on_success,
            timeout_secs,
            capture_output,
            keybinding_enabled,
            description,
            ..
        } => CustomActionConfig::ShellCommand {
            id: new_id,
            title: format!("{}-copy", title),
            command: command.clone(),
            args: args.clone(),
            notify_on_success: *notify_on_success,
            timeout_secs: *timeout_secs,
            capture_output: *capture_output,
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
        },
        CustomActionConfig::NewTab {
            title,
            command,
            keybinding_enabled,
            description,
            ..
        } => CustomActionConfig::NewTab {
            id: new_id,
            title: format!("{}-copy", title),
            command: command.clone(),
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
        },
        CustomActionConfig::InsertText {
            title,
            text,
            variables,
            keybinding_enabled,
            description,
            ..
        } => CustomActionConfig::InsertText {
            id: new_id,
            title: format!("{}-copy", title),
            text: text.clone(),
            variables: variables.clone(),
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
        },
        CustomActionConfig::KeySequence {
            title,
            keys,
            keybinding_enabled,
            description,
            ..
        } => CustomActionConfig::KeySequence {
            id: new_id,
            title: format!("{}-copy", title),
            keys: keys.clone(),
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
        },
        CustomActionConfig::SplitPane {
            title,
            direction,
            command,
            command_is_direct,
            focus_new_pane,
            delay_ms,
            split_percent,
            keybinding_enabled,
            description,
            ..
        } => CustomActionConfig::SplitPane {
            id: new_id,
            title: format!("{}-copy", title),
            direction: *direction,
            command: command.clone(),
            command_is_direct: *command_is_direct,
            focus_new_pane: *focus_new_pane,
            delay_ms: *delay_ms,
            split_percent: *split_percent,
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
        },
        CustomActionConfig::Sequence {
            title,
            keybinding_enabled,
            description,
            steps,
            ..
        } => CustomActionConfig::Sequence {
            id: new_id,
            title: format!("{}-copy", title),
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
            steps: steps.clone(),
        },
        CustomActionConfig::Condition {
            title,
            keybinding_enabled,
            description,
            check,
            on_true_id,
            on_false_id,
            ..
        } => CustomActionConfig::Condition {
            id: new_id,
            title: format!("{}-copy", title),
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
            check: check.clone(),
            on_true_id: on_true_id.clone(),
            on_false_id: on_false_id.clone(),
        },
        CustomActionConfig::Repeat {
            title,
            keybinding_enabled,
            description,
            action_id,
            count,
            delay_ms,
            stop_on_success,
            stop_on_failure,
            ..
        } => CustomActionConfig::Repeat {
            id: new_id,
            title: format!("{}-copy", title),
            keybinding: None,
            prefix_char: None,
            keybinding_enabled: *keybinding_enabled,
            description: description.clone(),
            action_id: action_id.clone(),
            count: *count,
            delay_ms: *delay_ms,
            stop_on_success: *stop_on_success,
            stop_on_failure: *stop_on_failure,
        },
    }
}