Skip to content

Commit 1b7b35e

Browse files
authored
Merge pull request #4 from taigrr/cd/validate-crush-width
fix(commands): validate crush width input
2 parents 4a71467 + 1b765fb commit 1b7b35e

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

lua/neocrush/commands.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ function M.create(neocrush)
3131

3232
vim.api.nvim_create_user_command('CrushWidth', function(opts)
3333
local width = tonumber(opts.args)
34-
if width then
34+
if width and width >= 1 then
3535
terminal.set_width(width)
3636
else
37-
vim.notify('Usage: CrushWidth <number>', vim.log.levels.ERROR)
37+
vim.notify('Usage: CrushWidth <number >= 1>', vim.log.levels.ERROR)
3838
end
3939
end, { nargs = 1, desc = 'Set Crush terminal width' })
4040

lua/neocrush/terminal.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,19 @@ end
161161

162162
--- Set the terminal width.
163163
---@param width integer Width in columns
164+
---@return boolean updated True when the width was accepted
164165
function M.set_width(width)
166+
if width < 1 then
167+
vim.notify('Crush width must be at least 1 column', vim.log.levels.ERROR)
168+
return false
169+
end
170+
165171
config.terminal_width = width
166172
if crush_win and vim.api.nvim_win_is_valid(crush_win) then
167173
vim.api.nvim_win_set_width(crush_win, config.terminal_width)
168174
end
175+
176+
return true
169177
end
170178

171179
--- Run `crush logs` and load the output into a new buffer.

tests/commands_spec.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---@diagnostic disable: undefined-field
22

33
local neocrush = require 'neocrush'
4+
local terminal = require 'neocrush.terminal'
45

56
describe('neocrush.commands', function()
67
before_each(function()
@@ -38,6 +39,16 @@ describe('neocrush.commands', function()
3839
assert.is_not_nil(cmds.CrushFocusToggle)
3940
end)
4041

42+
it('should register CrushFocusOn command', function()
43+
local cmds = vim.api.nvim_get_commands {}
44+
assert.is_not_nil(cmds.CrushFocusOn)
45+
end)
46+
47+
it('should register CrushFocusOff command', function()
48+
local cmds = vim.api.nvim_get_commands {}
49+
assert.is_not_nil(cmds.CrushFocusOff)
50+
end)
51+
4152
it('should register CrushLogs command', function()
4253
local cmds = vim.api.nvim_get_commands {}
4354
assert.is_not_nil(cmds.CrushLogs)
@@ -68,4 +79,29 @@ describe('neocrush.commands', function()
6879
assert.is_not_nil(cmds.CrushCvmLocal)
6980
end)
7081
end)
82+
83+
describe('CrushWidth validation', function()
84+
it('should reject widths smaller than 1', function()
85+
local original_notify = vim.notify
86+
local original_set_width = terminal.set_width
87+
local messages = {}
88+
local called = false
89+
90+
vim.notify = function(msg)
91+
table.insert(messages, msg)
92+
end
93+
terminal.set_width = function(width)
94+
called = true
95+
return original_set_width(width)
96+
end
97+
98+
vim.cmd 'CrushWidth 0'
99+
100+
assert.is_false(called)
101+
assert.truthy(messages[1]:find 'number >= 1')
102+
103+
terminal.set_width = original_set_width
104+
vim.notify = original_notify
105+
end)
106+
end)
71107
end)

tests/terminal_spec.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ describe('neocrush.terminal', function()
5656
end)
5757
end)
5858

59+
describe('set_width', function()
60+
it('should reject widths smaller than 1', function()
61+
local original_notify = vim.notify
62+
local messages = {}
63+
64+
vim.notify = function(msg)
65+
table.insert(messages, msg)
66+
end
67+
68+
local ok = terminal.set_width(0)
69+
70+
assert.is_false(ok)
71+
assert.truthy(messages[1]:find 'at least 1 column')
72+
73+
vim.notify = original_notify
74+
end)
75+
76+
it('should accept positive widths', function()
77+
assert.is_true(terminal.set_width(42))
78+
end)
79+
end)
80+
5981
describe('close', function()
6082
it('should not error when no terminal is open', function()
6183
assert.has_no.errors(function()

0 commit comments

Comments
 (0)