mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-18 21:40:29 +00:00
example/zig-formatter: show how to use formatters from libghostty (#9407)
This commit is contained in:
committed by
GitHub
parent
c0e483c49e
commit
4a88976ef9
41
example/zig-formatter/src/main.zig
Normal file
41
example/zig-formatter/src/main.zig
Normal file
@@ -0,0 +1,41 @@
|
||||
const std = @import("std");
|
||||
const ghostty_vt = @import("ghostty-vt");
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
const alloc = gpa.allocator();
|
||||
|
||||
// Create a terminal
|
||||
var t: ghostty_vt.Terminal = try .init(alloc, .{ .cols = 150, .rows = 80 });
|
||||
defer t.deinit(alloc);
|
||||
|
||||
// Create a read-only VT stream for parsing terminal sequences
|
||||
var stream = t.vtStream();
|
||||
defer stream.deinit();
|
||||
|
||||
// Read from stdin
|
||||
const stdin = std.fs.File.stdin();
|
||||
var buf: [4096]u8 = undefined;
|
||||
while (true) {
|
||||
const n = try stdin.readAll(&buf);
|
||||
if (n == 0) break;
|
||||
|
||||
// Replace \n with \r\n
|
||||
for (buf[0..n]) |byte| {
|
||||
if (byte == '\n') try stream.next('\r');
|
||||
try stream.next(byte);
|
||||
}
|
||||
}
|
||||
|
||||
// Use TerminalFormatter to emit HTML
|
||||
const formatter: ghostty_vt.formatter.TerminalFormatter = .init(&t, .{
|
||||
.emit = .html,
|
||||
.palette = &t.color_palette.colors,
|
||||
});
|
||||
|
||||
// Write to stdout
|
||||
var stdout_writer = std.fs.File.stdout().writer(&buf);
|
||||
const stdout = &stdout_writer.interface;
|
||||
try stdout.print("{f}", .{formatter});
|
||||
}
|
||||
Reference in New Issue
Block a user