cli/list-fonts: dumb implementation

This commit is contained in:
Mitchell Hashimoto
2023-09-23 22:59:22 -07:00
parent 9421bec3a1
commit 8214471e2c
3 changed files with 61 additions and 4 deletions

View File

@ -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),
};
}
};

View File

@ -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) {

View File

@ -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;
}