Skip to content

Commit 2f80ec1

Browse files
committed
fix(term): handle terminal size query failure gracefully
Changes: - replace assert on ioctl failure with graceful return and user notification in term.lua - guard against nil TIOCGWINSZ on unsupported OS before calling ioctl - initialize cached_size as nil instead of all-zero struct to avoid silent division-by-zero - add nil guards for get_size() in renderer.lua, kitty/init.lua, and document.lua Refs: #210, #339
1 parent 193e878 commit 2f80ec1

4 files changed

Lines changed: 21 additions & 9 deletions

File tree

lua/image/backends/kitty/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ backend.render = function(image, x, y, width, height)
111111
-- crop
112112
if backend.features.crop then
113113
local term_size = utils.term.get_size()
114+
if not term_size then return end
114115
local pixel_width = width * term_size.cell_width
115116
local pixel_height = height * term_size.cell_height
116117
local pixel_top = 0

lua/image/renderer.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local cache = {}
1919
local render = function(image)
2020
local state = image.global_state
2121
local term_size = utils.term.get_size()
22+
if not term_size then return end
2223
local scale_factor = 1.0
2324
if type(state.options.scale_factor) == "number" then scale_factor = state.options.scale_factor end
2425
local image_rows = math.floor(image.image_height / term_size.cell_height * scale_factor)

lua/image/utils/document.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ local create_document_integration = function(config)
114114

115115
-- Create a floating window for the image
116116
local term_size = utils.term.get_size()
117+
if not term_size then return end
117118
local width, height = utils.math.adjust_to_aspect_ratio(
118119
term_size,
119120
image.image_width,

lua/image/utils/term.lua

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
local cached_size = {
2-
screen_x = 0,
3-
screen_y = 0,
4-
screen_cols = 0,
5-
screen_rows = 0,
6-
cell_width = 0,
7-
cell_height = 0,
8-
}
1+
---@type { screen_x: number, screen_y: number, screen_cols: number, screen_rows: number, cell_width: number, cell_height: number }|nil
2+
local cached_size = nil
3+
local size_warned = false
94

105
-- https://github.com/edluffy/hologram.nvim/blob/main/lua/hologram/state.lua#L15
116
local update_size = function()
@@ -29,9 +24,23 @@ local update_size = function()
2924
TIOCGWINSZ = 0x40087468
3025
end
3126

27+
if not TIOCGWINSZ then
28+
if not size_warned then
29+
size_warned = true
30+
vim.notify("image.nvim: unsupported OS — cannot query terminal size", vim.log.levels.WARN)
31+
end
32+
return
33+
end
34+
3235
---@type { row: number, col: number, xpixel: number, ypixel: number }
3336
local sz = ffi.new("winsize")
34-
assert(ffi.C.ioctl(1, TIOCGWINSZ, sz) == 0, "Failed to get terminal size")
37+
if ffi.C.ioctl(1, TIOCGWINSZ, sz) ~= 0 then
38+
if not size_warned then
39+
size_warned = true
40+
vim.notify("image.nvim: cannot query terminal size (non-terminal environment?)", vim.log.levels.WARN)
41+
end
42+
return
43+
end
3544

3645
local xpixel = sz.xpixel
3746
local ypixel = sz.ypixel

0 commit comments

Comments
 (0)