From 8214471e2c3d3085829a126ca6e7de6bb9e70a03 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 23 Sep 2023 22:59:22 -0700 Subject: [PATCH] cli/list-fonts: dumb implementation --- src/cli/action.zig | 3 +-- src/cli/args.zig | 2 +- src/cli/list_fonts.zig | 60 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/cli/action.zig b/src/cli/action.zig index 26b3988be..4151d7aa2 100644 --- a/src/cli/action.zig +++ b/src/cli/action.zig @@ -49,10 +49,9 @@ pub const Action = enum { /// Run the action. This returns the exit code to exit with. pub fn run(self: Action, alloc: Allocator) !u8 { - _ = alloc; return switch (self) { .version => try version.run(), - .@"list-fonts" => try list_fonts.run(), + .@"list-fonts" => try list_fonts.run(alloc), }; } }; diff --git a/src/cli/args.zig b/src/cli/args.zig index 0a326fe89..833914c88 100644 --- a/src/cli/args.zig +++ b/src/cli/args.zig @@ -55,7 +55,7 @@ pub fn parse(comptime T: type, alloc: Allocator, dst: *T, iter: anytype) !void { break :arena dst._arena.?.allocator(); } else fail: { // Note: this is... not safe... - var fail = std.testing.FailingAllocator.init(alloc, 0); + var fail = std.testing.FailingAllocator.init(alloc, .{}); break :fail fail.allocator(); }; errdefer if (arena_available and arena_owned) { diff --git a/src/cli/list_fonts.zig b/src/cli/list_fonts.zig index eced0f472..df7432634 100644 --- a/src/cli/list_fonts.zig +++ b/src/cli/list_fonts.zig @@ -1,3 +1,61 @@ -pub fn run() !u8 { +const std = @import("std"); +const Allocator = std.mem.Allocator; +const ArenaAllocator = std.heap.ArenaAllocator; +const args = @import("args.zig"); +const font = @import("../font/main.zig"); + +const log = std.log.scoped(.list_fonts); + +pub const Config = struct { + /// This is set by the CLI parser for deinit. + _arena: ?ArenaAllocator = null, + + pub fn deinit(self: *Config) void { + if (self._arena) |arena| arena.deinit(); + self.* = undefined; + } +}; + +pub fn run(alloc: Allocator) !u8 { + var iter = try std.process.argsWithAllocator(alloc); + defer iter.deinit(); + return try runArgs(alloc, &iter); +} + +fn runArgs(alloc: Allocator, argsIter: anytype) !u8 { + var config: Config = .{}; + defer config.deinit(); + try args.parse(Config, alloc, &config, argsIter); + + // Its possible to build Ghostty without font discovery! + if (comptime font.Discover == void) { + const stderr = std.io.getStdErr().writer(); + try stderr.print( + \\Ghostty was built without a font discovery mechanism. This is a compile-time + \\option. Please review how Ghostty was built from source, contact the + \\maintainer to enable a font discovery mechanism, and try again. + , + .{}, + ); + return 1; + } + + const stdout = std.io.getStdOut().writer(); + + var disco = font.Discover.init(); + defer disco.deinit(); + + // Look up all available fonts + var disco_it = try disco.discover(.{}); + defer disco_it.deinit(); + while (try disco_it.next()) |face| { + var buf: [1024]u8 = undefined; + const name = face.name(&buf) catch |err| { + log.err("failed to get font name: {}", .{err}); + continue; + }; + try stdout.print("{s}\n", .{name}); + } + return 0; }