add --font-size flag for font size in pixels

This commit is contained in:
Mitchell Hashimoto
2022-07-21 21:35:04 -07:00
parent 578679fb35
commit 6641fcbd4c
6 changed files with 33 additions and 9 deletions

View File

@ -12,6 +12,7 @@ const Terminal = terminal.Terminal;
const gl = @import("opengl.zig");
const gb = @import("gb_math.zig");
const trace = @import("tracy/tracy.zig").trace;
const Config = @import("config.zig").Config;
const log = std.log.scoped(.grid);
@ -101,15 +102,15 @@ const GPUCell = struct {
mode: u8,
};
pub fn init(alloc: Allocator) !Grid {
pub fn init(alloc: Allocator, config: *const Config) !Grid {
// Initialize our font atlas. We will initially populate the
// font atlas with all the visible ASCII characters since they are common.
var atlas = try Atlas.init(alloc, 512);
errdefer atlas.deinit(alloc);
var fam = try font.Family.init(atlas);
errdefer fam.deinit(alloc);
try fam.loadFaceFromMemory(.regular, face_ttf, 32);
try fam.loadFaceFromMemory(.bold, face_bold_ttf, 32);
try fam.loadFaceFromMemory(.regular, face_ttf, config.@"font-size");
try fam.loadFaceFromMemory(.bold, face_bold_ttf, config.@"font-size");
// Load all visible ASCII characters and build our cell width based on
// the widest character that we see.

View File

@ -148,7 +148,7 @@ pub fn create(alloc: Allocator, loop: libuv.Loop, config: *const Config) !*Windo
// Create our terminal grid with the initial window size
const window_size = try window.getSize();
var grid = try Grid.init(alloc);
var grid = try Grid.init(alloc, config);
try grid.setScreenSize(.{ .width = window_size.width, .height = window_size.height });
grid.background = .{
.r = config.background.r,

View File

@ -72,17 +72,23 @@ fn parseIntoField(
const fieldInfo = @typeInfo(Field);
// If the type implements a parse function, call that.
if (fieldInfo == .Struct and @hasDecl(Field, "parseCLI"))
break :field try Field.parseCLI(value);
// Otherwise infer based on type
break :field switch (Field) {
// NOTE(mitchellh): this is a pretty nasty break statement.
// I split it into two at first and it failed with Zig as of
// July 21, 2022. I think stage2+ will fix this so lets clean
// this up when that comes out.
break :field if (fieldInfo == .Struct and @hasDecl(Field, "parseCLI"))
try Field.parseCLI(value)
else switch (Field) {
[]const u8 => if (value) |slice| value: {
const buf = try alloc.alloc(u8, slice.len);
mem.copy(u8, buf, slice);
break :value buf;
} else return error.ValueRequired,
bool => try parseBool(value orelse "t"),
u8 => try std.fmt.parseInt(u8, value orelse return error.ValueRequired, 0),
else => unreachable,
};
};
@ -177,6 +183,20 @@ test "parseIntoField: bool" {
try testing.expectEqual(false, data.a);
}
test "parseIntoField: unsigned numbers" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
var data: struct {
@"u8": u8,
} = undefined;
try parseIntoField(@TypeOf(data), alloc, &data, "u8", "1");
try testing.expectEqual(@as(u8, 1), data.@"u8");
}
test "parseIntoField: optional field" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);

View File

@ -2,6 +2,9 @@ const std = @import("std");
const ArenaAllocator = std.heap.ArenaAllocator;
pub const Config = struct {
/// Font size
@"font-size": u8 = 14,
/// Background color for the window.
background: Color = .{ .r = 0, .g = 0, .b = 0 },

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 38 KiB