Files
ghostty/src/terminal/focus.zig
Mitchell Hashimoto f50aa90ced terminal: add lib.zig to centralize lib target and re-exports
Previously every file in the terminal package independently imported
build_options and ../lib/main.zig, then computed the same
lib_target constant. This was repetitive and meant each file needed
both imports just to get the target.

Introduce src/terminal/lib.zig which computes the target once and
re-exports the commonly used lib types (Enum, TaggedUnion, Struct,
String, checkGhosttyHEnum, structSizedFieldFits). All terminal
package files now import lib.zig and use lib.target instead of the
local lib_target constant, removing the per-file boilerplate.
2026-03-25 07:25:16 -07:00

40 lines
1.2 KiB
Zig

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());
}