themes: don't use arena directly and cleanup debug code

This commit is contained in:
Jeffrey C. Ollie
2024-08-12 12:18:48 -05:00
committed by Mitchell Hashimoto
parent 50c31ba173
commit 7de692c955
3 changed files with 39 additions and 43 deletions

View File

@ -83,17 +83,18 @@ pub fn run(gpa_alloc: std.mem.Allocator) !u8 {
var themes = std.ArrayList(ThemeListElement).init(alloc); var themes = std.ArrayList(ThemeListElement).init(alloc);
var it = themepkg.LocationIterator{ .arena = &arena }; var it = themepkg.LocationIterator{ .arena_alloc = arena.allocator() };
while (try it.next()) |loc| { while (try it.next()) |loc| {
var dir = std.fs.cwd().openDir(loc.dir, .{ .iterate = true }) catch |err| switch (err) { var dir = std.fs.cwd().openDir(loc.dir, .{ .iterate = true }) catch |err| switch (err) {
error.FileNotFound => continue, error.FileNotFound => continue,
else => { else => {
std.debug.print("err: {}\n", .{err}); std.debug.print("error trying to open {s}: {}\n", .{ loc.dir, err });
continue; continue;
}, },
}; };
defer dir.close(); defer dir.close();
var walker = dir.iterate(); var walker = dir.iterate();
while (try walker.next()) |entry| { while (try walker.next()) |entry| {

View File

@ -2207,7 +2207,7 @@ pub fn themeDir(alloc: std.mem.Allocator, type_: ThemeDirType) ?[]const u8 {
fn loadTheme(self: *Config, theme: []const u8) !void { fn loadTheme(self: *Config, theme: []const u8) !void {
// Find our theme file and open it. See the open function for details. // Find our theme file and open it. See the open function for details.
const file: std.fs.File = (try themepkg.open( const file: std.fs.File = (try themepkg.open(
&self._arena.?, self._arena.?.allocator(),
theme, theme,
&self._errors, &self._errors,
)) orelse return; )) orelse return;

View File

@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const assert = std.debug.assert; const assert = std.debug.assert;
const ArenaAllocator = std.heap.ArenaAllocator; const Allocator = std.mem.Allocator;
const global_state = &@import("../main.zig").state; const global_state = &@import("../main.zig").state;
const internal_os = @import("../os/main.zig"); const internal_os = @import("../os/main.zig");
const ErrorList = @import("ErrorList.zig"); const ErrorList = @import("ErrorList.zig");
@ -18,26 +18,21 @@ pub const Location = enum {
/// or is invalid for any reason. For example, it is perfectly valid to /// or is invalid for any reason. For example, it is perfectly valid to
/// install and run Ghostty without the resources directory. /// install and run Ghostty without the resources directory.
/// ///
/// Due to the way allocations are handled, a pointer to an Arena allocator /// Due to the way allocations are handled, an Arena allocator (or another
/// must be used. /// similar allocator implementation) should be used. It may not be safe to
/// free the returned allocations.
pub fn dir( pub fn dir(
self: Location, self: Location,
arena: *ArenaAllocator, arena_alloc: Allocator,
) error{OutOfMemory}!?[]const u8 { ) error{OutOfMemory}!?[]const u8 {
const alloc = arena.allocator();
// if (comptime std.debug.runtime_safety) {
// assert(!std.fs.path.isAbsolute(theme));
// }
return switch (self) { return switch (self) {
.user => user: { .user => user: {
const subdir = std.fs.path.join(alloc, &.{ const subdir = std.fs.path.join(arena_alloc, &.{
"ghostty", "themes", "ghostty", "themes",
}) catch return error.OutOfMemory; }) catch return error.OutOfMemory;
break :user internal_os.xdg.config( break :user internal_os.xdg.config(
alloc, arena_alloc,
.{ .subdir = subdir }, .{ .subdir = subdir },
) catch |err| switch (err) { ) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory, error.OutOfMemory => return error.OutOfMemory,
@ -50,7 +45,7 @@ pub const Location = enum {
}; };
}, },
.resources => try std.fs.path.join(alloc, &.{ .resources => try std.fs.path.join(arena_alloc, &.{
global_state.resources_dir orelse return null, global_state.resources_dir orelse return null,
"themes", "themes",
}), }),
@ -61,7 +56,10 @@ pub const Location = enum {
/// An iterator that returns all possible directories for finding themes in /// An iterator that returns all possible directories for finding themes in
/// order of priority. /// order of priority.
pub const LocationIterator = struct { pub const LocationIterator = struct {
arena: *ArenaAllocator, /// Due to the way allocations are handled, an Arena allocator (or another
/// similar allocator implementation) should be used. It may not be safe to
/// free the returned allocations.
arena_alloc: Allocator,
i: usize = 0, i: usize = 0,
pub fn next(self: *LocationIterator) !?struct { pub fn next(self: *LocationIterator) !?struct {
@ -69,12 +67,10 @@ pub const LocationIterator = struct {
dir: []const u8, dir: []const u8,
} { } {
const max = @typeInfo(Location).Enum.fields.len; const max = @typeInfo(Location).Enum.fields.len;
std.debug.print("a: {d} {d}\n", .{ self.i, max });
while (self.i < max) { while (self.i < max) {
std.debug.print("b: {d}\n", .{self.i});
const location: Location = @enumFromInt(self.i); const location: Location = @enumFromInt(self.i);
self.i += 1; self.i += 1;
if (try location.dir(self.arena)) |dir| if (try location.dir(self.arena_alloc)) |dir|
return .{ return .{
.location = location, .location = location,
.dir = dir, .dir = dir,
@ -95,28 +91,27 @@ pub const LocationIterator = struct {
/// One error that is not recoverable and may be returned is OOM. This is /// One error that is not recoverable and may be returned is OOM. This is
/// always a critical error for configuration loading so it is returned. /// always a critical error for configuration loading so it is returned.
/// ///
/// Due to the way allocations are handled, a pointer to an Arena allocator /// Due to the way allocations are handled, an Arena allocator (or another
/// must be used. /// similar allocator implementation) should be used. It may not be safe to
/// free the returned allocations.
pub fn open( pub fn open(
arena: *ArenaAllocator, arena_alloc: Allocator,
theme: []const u8, theme: []const u8,
errors: *ErrorList, errors: *ErrorList,
) error{OutOfMemory}!?std.fs.File { ) error{OutOfMemory}!?std.fs.File {
// Absolute themes are loaded a different path. // Absolute themes are loaded a different path.
if (std.fs.path.isAbsolute(theme)) return try openAbsolute( if (std.fs.path.isAbsolute(theme)) return try openAbsolute(
arena, arena_alloc,
theme, theme,
errors, errors,
); );
const alloc = arena.allocator();
const basename = std.fs.path.basename(theme); const basename = std.fs.path.basename(theme);
if (!std.mem.eql(u8, theme, basename)) { if (!std.mem.eql(u8, theme, basename)) {
try errors.add(alloc, .{ try errors.add(arena_alloc, .{
.message = try std.fmt.allocPrintZ( .message = try std.fmt.allocPrintZ(
alloc, arena_alloc,
"theme \"{s}\" cannot include path separators unless it is an absolute path", "theme \"{s}\" cannot include path separators unless it is an absolute path",
.{theme}, .{theme},
), ),
@ -126,10 +121,10 @@ pub fn open(
// Iterate over the possible locations to try to find the // Iterate over the possible locations to try to find the
// one that exists. // one that exists.
var it: LocationIterator = .{ .arena = arena }; var it: LocationIterator = .{ .arena_alloc = arena_alloc };
const cwd = std.fs.cwd(); const cwd = std.fs.cwd();
while (try it.next()) |loc| { while (try it.next()) |loc| {
const path = try std.fs.path.join(alloc, &.{ loc.dir, theme }); const path = try std.fs.path.join(arena_alloc, &.{ loc.dir, theme });
if (cwd.openFile(path, .{})) |file| { if (cwd.openFile(path, .{})) |file| {
return file; return file;
} else |err| switch (err) { } else |err| switch (err) {
@ -138,9 +133,9 @@ pub fn open(
// Anything else is an error we log and give up on. // Anything else is an error we log and give up on.
else => { else => {
try errors.add(alloc, .{ try errors.add(arena_alloc, .{
.message = try std.fmt.allocPrintZ( .message = try std.fmt.allocPrintZ(
alloc, arena_alloc,
"failed to load theme \"{s}\" from the file \"{s}\": {}", "failed to load theme \"{s}\" from the file \"{s}\": {}",
.{ theme, path, err }, .{ theme, path, err },
), ),
@ -157,10 +152,10 @@ pub fn open(
// fine. // fine.
it.reset(); it.reset();
while (try it.next()) |loc| { while (try it.next()) |loc| {
const path = try std.fs.path.join(alloc, &.{ loc.dir, theme }); const path = try std.fs.path.join(arena_alloc, &.{ loc.dir, theme });
try errors.add(alloc, .{ try errors.add(arena_alloc, .{
.message = try std.fmt.allocPrintZ( .message = try std.fmt.allocPrintZ(
alloc, arena_alloc,
"theme \"{s}\" not found, tried path \"{s}\"", "theme \"{s}\" not found, tried path \"{s}\"",
.{ theme, path }, .{ theme, path },
), ),
@ -175,26 +170,26 @@ pub fn open(
/// returned. If a non-null return value is returned, there are never any /// returned. If a non-null return value is returned, there are never any
/// errors added. /// errors added.
/// ///
/// Due to the way allocations are handled, a pointer to an Arena allocator /// Due to the way allocations are handled, an Arena allocator (or another
/// must be used. /// similar allocator implementation) should be used. It may not be safe to
/// free the returned allocations.
pub fn openAbsolute( pub fn openAbsolute(
arena: *ArenaAllocator, arena_alloc: Allocator,
theme: []const u8, theme: []const u8,
errors: *ErrorList, errors: *ErrorList,
) error{OutOfMemory}!?std.fs.File { ) error{OutOfMemory}!?std.fs.File {
const alloc = arena.allocator();
return std.fs.openFileAbsolute(theme, .{}) catch |err| { return std.fs.openFileAbsolute(theme, .{}) catch |err| {
switch (err) { switch (err) {
error.FileNotFound => try errors.add(alloc, .{ error.FileNotFound => try errors.add(arena_alloc, .{
.message = try std.fmt.allocPrintZ( .message = try std.fmt.allocPrintZ(
alloc, arena_alloc,
"failed to load theme from the path \"{s}\"", "failed to load theme from the path \"{s}\"",
.{theme}, .{theme},
), ),
}), }),
else => try errors.add(alloc, .{ else => try errors.add(arena_alloc, .{
.message = try std.fmt.allocPrintZ( .message = try std.fmt.allocPrintZ(
alloc, arena_alloc,
"failed to load theme from the path \"{s}\": {}", "failed to load theme from the path \"{s}\": {}",
.{ theme, err }, .{ theme, err },
), ),