From 334743e8a7182fd7f233cbcf7c4a057700d7c7fe Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 14 Nov 2022 17:41:15 -0800 Subject: [PATCH] Don't crash on huge padding, warn users if padding is absurd --- src/Window.zig | 8 ++++++++ src/config.zig | 5 +++++ src/renderer/size.zig | 8 ++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Window.zig b/src/Window.zig index a96cd73fb..ad1b9b836 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -581,6 +581,14 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void { // Recalculate our grid size win.grid_size = win.renderer.gridSize(screen_size); + if (win.grid_size.columns < 5 and (win.padding.left > 0 or win.padding.right > 0)) { + log.warn("WARNING: very small terminal grid detected with padding " ++ + "set. Is your padding reasonable?", .{}); + } + if (win.grid_size.rows < 2 and (win.padding.top > 0 or win.padding.bottom > 0)) { + log.warn("WARNING: very small terminal grid detected with padding " ++ + "set. Is your padding reasonable?", .{}); + } // Mail the IO thread _ = win.io_thread.mailbox.push(.{ diff --git a/src/config.zig b/src/config.zig index 796894d37..d7a60753e 100644 --- a/src/config.zig +++ b/src/config.zig @@ -93,6 +93,11 @@ pub const Config = struct { /// the window border. The "x" option applies to the left and right /// padding and the "y" option is top and bottom. The value is in points, /// meaning that it will be scaled appropriately for screen DPI. + /// + /// If this value is set too large, the screen will render nothing, because + /// the grid will be completely squished by the padding. It is up to you + /// as the user to pick a reasonable value. If you pick an unreasonable + /// value, a warning will appear in the logs. @"window-padding-x": u32 = 0, @"window-padding-y": u32 = 0, diff --git a/src/renderer/size.zig b/src/renderer/size.zig index 51a3b135c..a96ae160e 100644 --- a/src/renderer/size.zig +++ b/src/renderer/size.zig @@ -48,8 +48,8 @@ pub const ScreenSize = struct { /// Subtract padding from the screen size. pub fn subPadding(self: ScreenSize, padding: Padding) ScreenSize { return .{ - .width = self.width - @floatToInt(u32, padding.left + padding.right), - .height = self.height - @floatToInt(u32, padding.top + padding.bottom), + .width = self.width -| @floatToInt(u32, padding.left + padding.right), + .height = self.height -| @floatToInt(u32, padding.top + padding.bottom), }; } }; @@ -71,8 +71,8 @@ pub const GridSize = struct { /// Update the columns/rows for the grid based on the given screen and /// cell size. pub fn update(self: *GridSize, screen: ScreenSize, cell: CellSize) void { - self.columns = @floatToInt(Unit, @intToFloat(f32, screen.width) / cell.width); - self.rows = @floatToInt(Unit, @intToFloat(f32, screen.height) / cell.height); + self.columns = @max(1, @floatToInt(Unit, @intToFloat(f32, screen.width) / cell.width)); + self.rows = @max(1, @floatToInt(Unit, @intToFloat(f32, screen.height) / cell.height)); } };