code review:

- initialize custom_css_providers using a default value
- remove usage of buffered reader
- document maximum file size
- handle exceptions explicitly
This commit is contained in:
Maciej Bartczak
2024-12-31 21:21:44 +01:00
parent 973467b1ca
commit 4ccd564849
2 changed files with 31 additions and 10 deletions

View File

@ -82,7 +82,7 @@ transient_cgroup_base: ?[]const u8 = null,
css_provider: *c.GtkCssProvider, css_provider: *c.GtkCssProvider,
/// Providers for loading custom stylesheets defined by user /// Providers for loading custom stylesheets defined by user
custom_css_providers: std.ArrayListUnmanaged(*c.GtkCssProvider), custom_css_providers: std.ArrayListUnmanaged(*c.GtkCssProvider) = .{},
/// The timer used to quit the application after the last window is closed. /// The timer used to quit the application after the last window is closed.
quit_timer: union(enum) { quit_timer: union(enum) {
@ -428,7 +428,6 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
// our "activate" call above will open a window. // our "activate" call above will open a window.
.running = c.g_application_get_is_remote(gapp) == 0, .running = c.g_application_get_is_remote(gapp) == 0,
.css_provider = css_provider, .css_provider = css_provider,
.custom_css_providers = .{},
}; };
} }
@ -909,8 +908,33 @@ fn syncConfigChanges(self: *App) !void {
.{}, .{},
), ),
}; };
self.loadCustomCss() catch |err| { self.loadCustomCss() catch |err| switch (err) {
log.warn("Failed to load custom CSS, no custom CSS applied, err={}", .{err}); error.OutOfMemory => log.warn(
"out of memory loading custom CSS, no custom CSS applied",
.{},
),
error.StreamTooLong => log.warn(
"failed to load custom CSS, no custom CSS applied - encountered stream too long error: {}",
.{err},
),
error.Unexpected => log.warn(
"failed to load custom CSS, no custom CSS applied - encountered unexpected error: {}",
.{err},
),
std.fs.File.Reader.Error.InputOutput,
std.fs.File.Reader.Error.SystemResources,
std.fs.File.Reader.Error.IsDir,
std.fs.File.Reader.Error.OperationAborted,
std.fs.File.Reader.Error.BrokenPipe,
std.fs.File.Reader.Error.ConnectionResetByPeer,
std.fs.File.Reader.Error.ConnectionTimedOut,
std.fs.File.Reader.Error.NotOpenForReading,
std.fs.File.Reader.Error.SocketNotConnected,
std.fs.File.Reader.Error.WouldBlock,
std.fs.File.Reader.Error.AccessDenied => log.warn(
"failed to load custom CSS, no custom CSS applied - encountered error while reading file: {}",
.{err},
),
}; };
} }
@ -1075,8 +1099,7 @@ fn loadCustomCss(self: *App) !void {
defer file.close(); defer file.close();
log.info("loading gtk-custom-css path={s}", .{path}); log.info("loading gtk-custom-css path={s}", .{path});
var buf_reader = std.io.bufferedReader(file.reader()); const contents = try file.reader().readAllAlloc(
const contents = try buf_reader.reader().readAllAlloc(
self.core_app.alloc, self.core_app.alloc,
5 * 1024 * 1024 // 5MB 5 * 1024 * 1024 // 5MB
); );
@ -1100,10 +1123,7 @@ fn loadCssProviderFromData(provider: *c.GtkCssProvider, data: []const u8) void {
const g_bytes = c.g_bytes_new(data.ptr, data.len); const g_bytes = c.g_bytes_new(data.ptr, data.len);
defer c.g_bytes_unref(g_bytes); defer c.g_bytes_unref(g_bytes);
c.gtk_css_provider_load_from_bytes( c.gtk_css_provider_load_from_bytes(provider, g_bytes);
provider,
g_bytes
);
} else { } else {
c.gtk_css_provider_load_from_data( c.gtk_css_provider_load_from_data(
provider, provider,

View File

@ -1931,6 +1931,7 @@ keybind: Keybinds = .{},
/// Prepend a ? character to the file path to suppress errors if the file does /// Prepend a ? character to the file path to suppress errors if the file does
/// not exist. If you want to include a file that begins with a literal ? /// not exist. If you want to include a file that begins with a literal ?
/// character, surround the file path in double quotes ("). /// character, surround the file path in double quotes (").
/// The file size limit for a single stylesheet is 5MiB.
@"gtk-custom-css": RepeatablePath = .{}, @"gtk-custom-css": RepeatablePath = .{},
/// If `true` (default), applications running in the terminal can show desktop /// If `true` (default), applications running in the terminal can show desktop