Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusey committed Jan 3, 2025
1 parent 2cecac2 commit 4840a8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
23 changes: 15 additions & 8 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3179,7 +3179,7 @@ fn expandPaths(self: *Config, base: []const u8) !void {

// Expand all of our paths
inline for (@typeInfo(Config).Struct.fields) |field| {
if (field.type == RepeatablePath) {
if (field.type == RepeatablePath or field.type == SinglePath) {
try @field(self, field.name).expand(
arena_alloc,
base,
Expand Down Expand Up @@ -4258,26 +4258,34 @@ pub const SinglePath = struct {
const Self = @This();

/// The actual value that is updated as we parse.
value: []const u8 = "",
value: ?[]const u8 = null,

/// Parse a single path.
pub fn parseCLI(self: *Self, alloc: Allocator, input: ?[]const u8) !void {
const value = input orelse return error.ValueRequired;
// If the value is empty, we set the value to null
if (value.len == 0) {
self.value = null;
return;
}
const copy = try alloc.dupe(u8, value);
self.value = copy;
}

/// Deep copy of the struct. Required by Config.
pub fn clone(self: Self, alloc: Allocator) Allocator.Error!Self {
const copy_path = try alloc.dupe(u8, self.value);
const value = self.value orelse return .{};

const copy_path = try alloc.dupe(u8, value);
return .{
.value = copy_path,
};
}

/// Used by Formatter
pub fn formatEntry(self: Self, formatter: anytype) !void {
try formatter.formatEntry([]const u8, self.value);
const value = self.value orelse return;
try formatter.formatEntry([]const u8, value);
}

pub fn expand(
Expand All @@ -4290,10 +4298,9 @@ pub const SinglePath = struct {
var dir = try std.fs.cwd().openDir(base, .{});
defer dir.close();

const path = self.value;

// If it is already absolute we can ignore it.
if (path.len == 0 or std.fs.path.isAbsolute(path)) return;
const path = self.value orelse return;
if (std.fs.path.isAbsolute(path)) return;

// If it isn't absolute, we need to make it absolute relative
// to the base.
Expand All @@ -4317,7 +4324,7 @@ pub const SinglePath = struct {
});

// Blank this path so that we don't attempt to resolve it again
self.value = "";
self.value = null;

return;
};
Expand Down
13 changes: 6 additions & 7 deletions src/renderer/OpenGL.zig
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ pub fn updateFrame(
}

if (self.current_background_image == null and
self.background_image.value.len > 0)
self.background_image.value != null)
{
if (single_threaded_draw) self.draw_mutex.lock();
defer if (single_threaded_draw) self.draw_mutex.unlock();
Expand Down Expand Up @@ -1247,8 +1247,7 @@ fn prepKittyImage(
/// Prepares the current background image for upload
pub fn prepBackgroundImage(self: *OpenGL) !void {
// If the user doesn't have a background image, do nothing...
if (self.background_image.value.len == 0) return;
const path = self.background_image.value;
const path = self.background_image.value orelse return;

// Read the file content
const file_content = try self.readImageContent(path);
Expand All @@ -1273,8 +1272,9 @@ pub fn prepBackgroundImage(self: *OpenGL) !void {

/// Reads the content of the given image path and returns it
pub fn readImageContent(self: *OpenGL, path: []const u8) ![]u8 {
assert(std.fs.path.isAbsolute(path));
// Open the file
var file = std.fs.cwd().openFile(path, .{}) catch |err| {
var file = std.fs.openFileAbsolute(path, .{}) catch |err| {
log.warn("failed to open file: {}", .{err});
return error.InvalidData;
};
Expand All @@ -1297,13 +1297,12 @@ pub fn readImageContent(self: *OpenGL, path: []const u8) ![]u8 {
// Read the file
var managed = std.ArrayList(u8).init(self.alloc);
errdefer managed.deinit();
const size: usize = max_image_size;
reader.readAllArrayList(&managed, size) catch |err| {
reader.readAllArrayList(&managed, max_image_size) catch |err| {
log.warn("failed to read file: {}", .{err});
return error.InvalidData;
};

return managed.items;
return managed.toOwnedSlice();
}

/// rebuildCells rebuilds all the GPU cells from our CPU state. This is a
Expand Down

0 comments on commit 4840a8c

Please sign in to comment.