par-term 0.30.10

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
//! Tests for automation trigger and action configuration — serialization,
//! deserialization, and core-action conversion.

use par_term::config::automation::PrettifyScope;
use par_term::config::{
    Config, CoprocessDefConfig, RestartPolicy, TriggerActionConfig, TriggerConfig,
};

#[test]
fn test_default_config_has_empty_triggers_and_coprocesses() {
    let config = Config::default();
    assert!(config.triggers.is_empty());
    assert!(config.coprocesses.is_empty());
}

#[test]
fn test_trigger_config_yaml_roundtrip() {
    let trigger = TriggerConfig {
        name: "error-detect".to_string(),
        pattern: r"ERROR:\s+(.+)".to_string(),
        enabled: true,
        actions: vec![
            TriggerActionConfig::Highlight {
                fg: Some([255, 0, 0]),
                bg: None,
                duration_ms: 5000,
            },
            TriggerActionConfig::Notify {
                title: "Error!".to_string(),
                message: "Found an error".to_string(),
            },
        ],
        prompt_before_run: true,
        i_accept_the_risk: false,
    };

    let yaml = serde_yaml_ng::to_string(&trigger).unwrap();
    let deserialized: TriggerConfig = serde_yaml_ng::from_str(&yaml).unwrap();
    assert_eq!(trigger, deserialized);
}

#[test]
fn test_trigger_config_disabled() {
    let yaml = r#"
name: test
pattern: "foo"
enabled: false
actions: []
"#;
    let trigger: TriggerConfig = serde_yaml_ng::from_str(yaml).unwrap();
    assert!(!trigger.enabled);
}

#[test]
fn test_trigger_config_defaults() {
    // enabled defaults to true, actions defaults to empty
    let yaml = r#"
name: test
pattern: "foo"
"#;
    let trigger: TriggerConfig = serde_yaml_ng::from_str(yaml).unwrap();
    assert!(trigger.enabled);
    assert!(trigger.actions.is_empty());
}

#[test]
fn test_all_trigger_action_variants_serialize_deserialize() {
    let actions = vec![
        TriggerActionConfig::Highlight {
            fg: Some([255, 0, 0]),
            bg: None,
            duration_ms: 5000,
        },
        TriggerActionConfig::Notify {
            title: "t".into(),
            message: "m".into(),
        },
        TriggerActionConfig::MarkLine {
            label: Some("mark".into()),
            color: None,
        },
        TriggerActionConfig::SetVariable {
            name: "n".into(),
            value: "v".into(),
        },
        TriggerActionConfig::RunCommand {
            command: "echo".into(),
            args: vec!["hi".into()],
        },
        TriggerActionConfig::PlaySound {
            sound_id: "bell".into(),
            volume: 80,
        },
        TriggerActionConfig::SendText {
            text: "hello".into(),
            delay_ms: 100,
        },
        TriggerActionConfig::Prettify {
            format: "json".into(),
            scope: PrettifyScope::CommandOutput,
            block_end: None,
            sub_format: None,
            command_filter: None,
        },
    ];

    for action in &actions {
        let yaml = serde_yaml_ng::to_string(action).unwrap();
        let deserialized: TriggerActionConfig = serde_yaml_ng::from_str(&yaml).unwrap();
        assert_eq!(action, &deserialized);
    }
}

#[test]
fn test_trigger_action_highlight_defaults() {
    let yaml = r#"
type: highlight
"#;
    let action: TriggerActionConfig = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(
        action,
        TriggerActionConfig::Highlight {
            fg: None,
            bg: None,
            duration_ms: 5000,
        }
    );
}

#[test]
fn test_trigger_action_play_sound_defaults() {
    let yaml = r#"
type: play_sound
"#;
    let action: TriggerActionConfig = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(
        action,
        TriggerActionConfig::PlaySound {
            sound_id: String::new(),
            volume: 50,
        }
    );
}

#[test]
fn test_trigger_action_to_core_action_highlight() {
    use par_term_emu_core_rust::terminal::TriggerAction;

    let config_action = TriggerActionConfig::Highlight {
        fg: Some([255, 0, 0]),
        bg: Some([0, 255, 0]),
        duration_ms: 3000,
    };
    let core_action = config_action.to_core_action();
    assert_eq!(
        core_action,
        TriggerAction::Highlight {
            fg: Some((255, 0, 0)),
            bg: Some((0, 255, 0)),
            duration_ms: 3000,
        }
    );
}

#[test]
fn test_trigger_action_to_core_action_all_variants() {
    use par_term_emu_core_rust::terminal::TriggerAction;

    let pairs: Vec<(TriggerActionConfig, TriggerAction)> = vec![
        (
            TriggerActionConfig::Notify {
                title: "t".into(),
                message: "m".into(),
            },
            TriggerAction::Notify {
                title: "t".into(),
                message: "m".into(),
            },
        ),
        (
            TriggerActionConfig::MarkLine {
                label: Some("L".into()),
                color: None,
            },
            TriggerAction::MarkLine {
                label: Some("L".into()),
                color: None,
            },
        ),
        (
            TriggerActionConfig::SetVariable {
                name: "n".into(),
                value: "v".into(),
            },
            TriggerAction::SetVariable {
                name: "n".into(),
                value: "v".into(),
            },
        ),
        (
            TriggerActionConfig::RunCommand {
                command: "echo".into(),
                args: vec!["hi".into()],
            },
            TriggerAction::RunCommand {
                command: "echo".into(),
                args: vec!["hi".into()],
            },
        ),
        (
            TriggerActionConfig::PlaySound {
                sound_id: "bell".into(),
                volume: 80,
            },
            TriggerAction::PlaySound {
                sound_id: "bell".into(),
                volume: 80,
            },
        ),
        (
            TriggerActionConfig::SendText {
                text: "hello".into(),
                delay_ms: 100,
            },
            TriggerAction::SendText {
                text: "hello".into(),
                delay_ms: 100,
            },
        ),
    ];

    for (config_action, expected_core) in pairs {
        let core = config_action.to_core_action();
        assert_eq!(core, expected_core);
    }
}

#[test]
fn test_coprocess_def_config_yaml_roundtrip() {
    let coproc = CoprocessDefConfig {
        name: "logger".to_string(),
        command: "/usr/bin/tee".to_string(),
        args: vec!["/tmp/log.txt".to_string()],
        auto_start: true,
        copy_terminal_output: true,
        restart_policy: RestartPolicy::Never,
        restart_delay_ms: 0,
    };

    let yaml = serde_yaml_ng::to_string(&coproc).unwrap();
    let deserialized: CoprocessDefConfig = serde_yaml_ng::from_str(&yaml).unwrap();
    assert_eq!(coproc, deserialized);
}

#[test]
fn test_coprocess_def_config_defaults() {
    let yaml = r#"
name: test
command: /bin/cat
"#;
    let coproc: CoprocessDefConfig = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(coproc.name, "test");
    assert_eq!(coproc.command, "/bin/cat");
    assert!(coproc.args.is_empty());
    assert!(!coproc.auto_start);
    assert!(coproc.copy_terminal_output); // defaults to true
    assert_eq!(coproc.restart_policy, RestartPolicy::Never); // defaults to Never
    assert_eq!(coproc.restart_delay_ms, 0); // defaults to 0
}

#[test]
#[allow(clippy::field_reassign_with_default)]
fn test_config_with_triggers_and_coprocesses_yaml_roundtrip() {
    let mut config = Config::default();
    config.triggers = vec![TriggerConfig {
        name: "error".to_string(),
        pattern: "ERROR".to_string(),
        enabled: true,
        actions: vec![TriggerActionConfig::Highlight {
            fg: Some([255, 0, 0]),
            bg: None,
            duration_ms: 5000,
        }],
        prompt_before_run: true,
        i_accept_the_risk: false,
    }];
    config.coprocesses = vec![CoprocessDefConfig {
        name: "logger".to_string(),
        command: "/usr/bin/tee".to_string(),
        args: vec!["/tmp/log.txt".to_string()],
        auto_start: false,
        copy_terminal_output: true,
        restart_policy: RestartPolicy::Never,
        restart_delay_ms: 0,
    }];

    let yaml = serde_yaml_ng::to_string(&config).unwrap();
    let deserialized: Config = serde_yaml_ng::from_str(&yaml).unwrap();
    assert_eq!(config.triggers, deserialized.triggers);
    assert_eq!(config.coprocesses, deserialized.coprocesses);
}

#[test]
fn test_prettify_action_yaml_roundtrip() {
    let action = TriggerActionConfig::Prettify {
        format: "markdown".into(),
        scope: PrettifyScope::Block,
        block_end: Some(r"^```$".into()),
        sub_format: Some("plantuml".into()),
        command_filter: Some(r"^myapi\s+".into()),
    };

    let yaml = serde_yaml_ng::to_string(&action).unwrap();
    let deserialized: TriggerActionConfig = serde_yaml_ng::from_str(&yaml).unwrap();
    assert_eq!(action, deserialized);
}

#[test]
fn test_prettify_action_defaults() {
    let yaml = r#"
type: prettify
format: json
"#;
    let action: TriggerActionConfig = serde_yaml_ng::from_str(yaml).unwrap();
    assert_eq!(
        action,
        TriggerActionConfig::Prettify {
            format: "json".into(),
            scope: PrettifyScope::CommandOutput, // default
            block_end: None,
            sub_format: None,
            command_filter: None,
        }
    );
}

#[test]
fn test_prettify_scope_deserialization() {
    // Line scope
    let yaml = r#"
type: prettify
format: json
scope: line
"#;
    let action: TriggerActionConfig = serde_yaml_ng::from_str(yaml).unwrap();
    match action {
        TriggerActionConfig::Prettify { scope, .. } => assert_eq!(scope, PrettifyScope::Line),
        _ => panic!("expected Prettify"),
    }

    // Block scope
    let yaml = r#"
type: prettify
format: json
scope: block
"#;
    let action: TriggerActionConfig = serde_yaml_ng::from_str(yaml).unwrap();
    match action {
        TriggerActionConfig::Prettify { scope, .. } => assert_eq!(scope, PrettifyScope::Block),
        _ => panic!("expected Prettify"),
    }

    // CommandOutput scope
    let yaml = r#"
type: prettify
format: json
scope: command_output
"#;
    let action: TriggerActionConfig = serde_yaml_ng::from_str(yaml).unwrap();
    match action {
        TriggerActionConfig::Prettify { scope, .. } => {
            assert_eq!(scope, PrettifyScope::CommandOutput);
        }
        _ => panic!("expected Prettify"),
    }
}

#[test]
fn test_prettify_to_core_action_relays_through_mark_line() {
    use par_term::config::automation::PRETTIFY_RELAY_PREFIX;
    use par_term_emu_core_rust::terminal::TriggerAction;

    let action = TriggerActionConfig::Prettify {
        format: "json".into(),
        scope: PrettifyScope::CommandOutput,
        block_end: None,
        sub_format: None,
        command_filter: Some(r"^myapi\s+".into()),
    };

    let core = action.to_core_action();

    // Should relay through MarkLine with __prettify__ label prefix.
    match core {
        TriggerAction::MarkLine { label, color } => {
            assert!(color.is_none());
            let lbl = label.expect("label should be set");
            assert!(
                lbl.starts_with(PRETTIFY_RELAY_PREFIX),
                "label should start with prettify prefix"
            );
            let json = lbl.strip_prefix(PRETTIFY_RELAY_PREFIX).unwrap();
            let payload: serde_json::Value = serde_json::from_str(json).unwrap();
            assert_eq!(payload["format"], "json");
            assert_eq!(payload["scope"], "command_output");
            assert_eq!(payload["command_filter"], r"^myapi\s+");
        }
        other => panic!("expected MarkLine relay, got {:?}", other),
    }
}

#[test]
fn test_prettify_none_format_serializes() {
    let action = TriggerActionConfig::Prettify {
        format: "none".into(),
        scope: PrettifyScope::CommandOutput,
        block_end: None,
        sub_format: None,
        command_filter: Some(r"^bat\s+".into()),
    };

    let yaml = serde_yaml_ng::to_string(&action).unwrap();
    let deserialized: TriggerActionConfig = serde_yaml_ng::from_str(&yaml).unwrap();
    assert_eq!(action, deserialized);

    match deserialized {
        TriggerActionConfig::Prettify { format, .. } => assert_eq!(format, "none"),
        _ => panic!("expected Prettify"),
    }
}

#[test]
fn test_trigger_with_prettify_action_roundtrip() {
    let trigger = TriggerConfig {
        name: "Prettify myapi output".to_string(),
        pattern: r#"^\{"api_version":"#.to_string(),
        enabled: true,
        actions: vec![TriggerActionConfig::Prettify {
            format: "json".into(),
            scope: PrettifyScope::CommandOutput,
            block_end: None,
            sub_format: None,
            command_filter: None,
        }],
        prompt_before_run: true,
        i_accept_the_risk: false,
    };

    let yaml = serde_yaml_ng::to_string(&trigger).unwrap();
    let deserialized: TriggerConfig = serde_yaml_ng::from_str(&yaml).unwrap();
    assert_eq!(trigger, deserialized);
}