config: move optional path parsing into RepeatablePath

This commit refactors RepeatablePath to contain a list of tagged unions
containing "optional" and "required" variants. Both variants have a null
terminated file path as their payload, but the tag dictates whether the
path must exist or not. This implemenation is used to force consumers to
handle the optional vs. required distinction.

This also moves the parsing of optional file paths into RepeatablePath's
parseCLI function. This allows the code to be better unit tested. Since
RepeatablePath no longer contains a simple list of RepeatableStrings,
many other of its methods needed to be reimplemented as well.

Because all of this functionality is built into the RepeatablePath type,
other config options which also use RepeatablePath gain the ability to
specify optional paths as well. Right now this is only the
"custom-shaders" option. The code paths in the renderer to load shader
files has been updated accordingly.

In the original optional config file parsing, the leading ? character
was removed when paths were expanded. Thus, when config files were
actually loaded recursively, they appeared to be regular (required)
config files and an error occurred if the file did not exist. **This
issue was not found during testing because the presence of the
"theme" option masks the error**. I am not sure why the presence of
"theme" does this, I did not dig into that.

Now because the "optional" or "required" state of each path is tracked
in the enum tag the "optional" status of the path is preserved after
being expanded to an absolute path.

Finally, this commit fixes a bug where missing "config-file" files were
not included in the +show-config command (i.e. if a user had
`config-file = foo.conf` and `foo.conf` did not exist, then `ghostty
+show-config` would only display `config-file =`). This bug applied to
`custom-shaders` too, where it has also been fixed.
This commit is contained in:
Gregory Anders
2024-09-17 19:42:42 -05:00
parent 0ac29783b9
commit 64abbd0ea6
5 changed files with 183 additions and 49 deletions

View File

@@ -347,7 +347,7 @@ pub const DerivedConfig = struct {
bold_is_bright: bool,
min_contrast: f32,
padding_color: configpkg.WindowPaddingColor,
custom_shaders: std.ArrayListUnmanaged([:0]const u8),
custom_shaders: configpkg.RepeatablePath,
links: link.Set,
vsync: bool,
@@ -360,7 +360,7 @@ pub const DerivedConfig = struct {
const alloc = arena.allocator();
// Copy our shaders
const custom_shaders = try config.@"custom-shader".value.list.clone(alloc);
const custom_shaders = try config.@"custom-shader".value.clone(alloc);
// Copy our font features
const font_features = try config.@"font-feature".list.clone(alloc);
@@ -540,7 +540,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !Metal {
// Load our custom shaders
const custom_shaders: []const [:0]const u8 = shadertoy.loadFromFiles(
arena_alloc,
options.config.custom_shaders.items,
options.config.custom_shaders,
.msl,
) catch |err| err: {
log.warn("error loading custom shaders err={}", .{err});
@@ -549,7 +549,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !Metal {
// If we have custom shaders then setup our state
var custom_shader_state: ?CustomShaderState = state: {
if (custom_shaders.len == 0) break :state null;
if (custom_shaders.value.items.len == 0) break :state null;
// Build our sampler for our texture
var sampler = try mtl_sampler.Sampler.init(gpu_state.device);

View File

@@ -301,7 +301,7 @@ pub const DerivedConfig = struct {
bold_is_bright: bool,
min_contrast: f32,
padding_color: configpkg.WindowPaddingColor,
custom_shaders: std.ArrayListUnmanaged([:0]const u8),
custom_shaders: configpkg.RepeatablePath,
links: link.Set,
pub fn init(
@@ -313,7 +313,7 @@ pub const DerivedConfig = struct {
const alloc = arena.allocator();
// Copy our shaders
const custom_shaders = try config.@"custom-shader".value.list.clone(alloc);
const custom_shaders = try config.@"custom-shader".clone(alloc);
// Copy our font features
const font_features = try config.@"font-feature".list.clone(alloc);
@@ -2327,7 +2327,7 @@ const GLState = struct {
const custom_state: ?custom.State = custom: {
const shaders: []const [:0]const u8 = shadertoy.loadFromFiles(
arena_alloc,
config.custom_shaders.items,
config.custom_shaders,
.glsl,
) catch |err| err: {
log.warn("error loading custom shaders err={}", .{err});

View File

@@ -5,6 +5,7 @@ const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const glslang = @import("glslang");
const spvcross = @import("spirv_cross");
const configpkg = @import("../config.zig");
const log = std.log.scoped(.shadertoy);
@@ -15,15 +16,26 @@ pub const Target = enum { glsl, msl };
/// format. The shader order is preserved.
pub fn loadFromFiles(
alloc_gpa: Allocator,
paths: []const []const u8,
paths: configpkg.RepeatablePath,
target: Target,
) ![]const [:0]const u8 {
var list = std.ArrayList([:0]const u8).init(alloc_gpa);
defer list.deinit();
errdefer for (list.items) |shader| alloc_gpa.free(shader);
for (paths) |path| {
const shader = try loadFromFile(alloc_gpa, path, target);
for (paths.value.items) |item| {
const path, const optional = switch (item) {
.optional => |path| .{ path, true },
.required => |path| .{ path, false },
};
const shader = loadFromFile(alloc_gpa, path, target) catch |err| {
if (err == error.FileNotFound and optional) {
continue;
}
return err;
};
log.info("loaded custom shader path={s}", .{path});
try list.append(shader);
}