aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/libghostty-vt/src/terminal/focus.zig
blob: 91c579e12f74574f06639da61a751bfe59b8706e (plain) (blame)
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
const std = @import("std");
const lib = @import("lib.zig");

/// Maximum number of bytes that `encode` will write. Any users of this
/// should be resilient to this changing, so this is always a specific
/// value (e.g. we don't add unnecessary padding).
pub const max_encode_size = 3;

/// A focus event that can be reported to the application running in the
/// terminal when focus reporting mode (mode 1004) is enabled.
pub const Event = lib.Enum(lib.target, &.{
    "gained",
    "lost",
});

/// Encode a focus in/out report (CSI I / CSI O).
pub fn encode(
    writer: *std.Io.Writer,
    event: Event,
) std.Io.Writer.Error!void {
    try writer.writeAll(switch (event) {
        .gained => "\x1B[I",
        .lost => "\x1B[O",
    });
}

test "encode focus gained" {
    var buf: [max_encode_size]u8 = undefined;
    var writer: std.Io.Writer = .fixed(&buf);
    try encode(&writer, .gained);
    try std.testing.expectEqualStrings("\x1B[I", writer.buffered());
}

test "encode focus lost" {
    var buf: [max_encode_size]u8 = undefined;
    var writer: std.Io.Writer = .fixed(&buf);
    try encode(&writer, .lost);
    try std.testing.expectEqualStrings("\x1B[O", writer.buffered());
}