aboutsummaryrefslogtreecommitdiff
path: root/vendor/libghostty-vt/src/terminal/osc/parsers/change_window_title.zig
blob: 5bd84dc38a9114260b421c86874a70b386d865f8 (plain)
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
const std = @import("std");

const Parser = @import("../../osc.zig").Parser;
const Command = @import("../../osc.zig").Command;

/// Parse OSC 0 and OSC 2
pub fn parse(parser: *Parser, _: ?u8) ?*Command {
    const cap = if (parser.capture) |*c| c else {
        parser.state = .invalid;
        return null;
    };
    cap.writer.writeByte(0) catch {
        parser.state = .invalid;
        return null;
    };
    const data = cap.trailing();
    parser.command = .{
        .change_window_title = data[0 .. data.len - 1 :0],
    };
    return &parser.command;
}

test "OSC 0: change_window_title" {
    const testing = std.testing;

    var p: Parser = .init(null);
    p.next('0');
    p.next(';');
    p.next('a');
    p.next('b');
    const cmd = p.end(null).?.*;
    try testing.expect(cmd == .change_window_title);
    try testing.expectEqualStrings("ab", cmd.change_window_title);
}

test "OSC 0: longer than buffer" {
    const testing = std.testing;

    var p: Parser = .init(null);

    const input = "0;" ++ "a" ** (Parser.MAX_BUF + 2);
    for (input) |ch| p.next(ch);

    try testing.expect(p.end(null) == null);
}

test "OSC 0: one shorter than buffer length" {
    const testing = std.testing;

    var p: Parser = .init(null);

    const prefix = "0;";
    const title = "a" ** (Parser.MAX_BUF - 1);
    const input = prefix ++ title;
    for (input) |ch| p.next(ch);

    const cmd = p.end(null).?.*;
    try testing.expect(cmd == .change_window_title);
    try testing.expectEqualStrings(title, cmd.change_window_title);
}

test "OSC 0: exactly at buffer length" {
    const testing = std.testing;

    var p: Parser = .init(null);

    const prefix = "0;";
    const title = "a" ** Parser.MAX_BUF;
    const input = prefix ++ title;
    for (input) |ch| p.next(ch);

    // This should be null because we always reserve space for a null terminator.
    try testing.expect(p.end(null) == null);
}
test "OSC 2: change_window_title with 2" {
    const testing = std.testing;

    var p: Parser = .init(null);
    p.next('2');
    p.next(';');
    p.next('a');
    p.next('b');
    const cmd = p.end(null).?.*;
    try testing.expect(cmd == .change_window_title);
    try testing.expectEqualStrings("ab", cmd.change_window_title);
}

test "OSC 2: change_window_title with utf8" {
    const testing = std.testing;

    var p: Parser = .init(null);
    p.next('2');
    p.next(';');
    // '—' EM DASH U+2014 (E2 80 94)
    p.next(0xE2);
    p.next(0x80);
    p.next(0x94);

    p.next(' ');
    // '‐' HYPHEN U+2010 (E2 80 90)
    // Intententionally chosen to conflict with the 0x90 C1 control
    p.next(0xE2);
    p.next(0x80);
    p.next(0x90);
    const cmd = p.end(null).?.*;
    try testing.expect(cmd == .change_window_title);
    try testing.expectEqualStrings("— ‐", cmd.change_window_title);
}

test "OSC 2: change_window_title empty" {
    const testing = std.testing;

    var p: Parser = .init(null);
    p.next('2');
    p.next(';');
    const cmd = p.end(null).?.*;
    try testing.expect(cmd == .change_window_title);
    try testing.expectEqualStrings("", cmd.change_window_title);
}