Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lua/neocrush/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ function M.create(neocrush)

vim.api.nvim_create_user_command('CrushWidth', function(opts)
local width = tonumber(opts.args)
if width then
if width and width >= 1 then
terminal.set_width(width)
else
vim.notify('Usage: CrushWidth <number>', vim.log.levels.ERROR)
vim.notify('Usage: CrushWidth <number >= 1>', vim.log.levels.ERROR)
end
end, { nargs = 1, desc = 'Set Crush terminal width' })

Expand Down
8 changes: 8 additions & 0 deletions lua/neocrush/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,19 @@ end

--- Set the terminal width.
---@param width integer Width in columns
---@return boolean updated True when the width was accepted
function M.set_width(width)
if width < 1 then
vim.notify('Crush width must be at least 1 column', vim.log.levels.ERROR)
return false
end

config.terminal_width = width
if crush_win and vim.api.nvim_win_is_valid(crush_win) then
vim.api.nvim_win_set_width(crush_win, config.terminal_width)
end

return true
end

--- Run `crush logs` and load the output into a new buffer.
Expand Down
36 changes: 36 additions & 0 deletions tests/commands_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---@diagnostic disable: undefined-field

local neocrush = require 'neocrush'
local terminal = require 'neocrush.terminal'

describe('neocrush.commands', function()
before_each(function()
Expand Down Expand Up @@ -38,6 +39,16 @@ describe('neocrush.commands', function()
assert.is_not_nil(cmds.CrushFocusToggle)
end)

it('should register CrushFocusOn command', function()
local cmds = vim.api.nvim_get_commands {}
assert.is_not_nil(cmds.CrushFocusOn)
end)

it('should register CrushFocusOff command', function()
local cmds = vim.api.nvim_get_commands {}
assert.is_not_nil(cmds.CrushFocusOff)
end)

it('should register CrushLogs command', function()
local cmds = vim.api.nvim_get_commands {}
assert.is_not_nil(cmds.CrushLogs)
Expand Down Expand Up @@ -68,4 +79,29 @@ describe('neocrush.commands', function()
assert.is_not_nil(cmds.CrushCvmLocal)
end)
end)

describe('CrushWidth validation', function()
it('should reject widths smaller than 1', function()
local original_notify = vim.notify
local original_set_width = terminal.set_width
local messages = {}
local called = false

vim.notify = function(msg)
table.insert(messages, msg)
end
terminal.set_width = function(width)
called = true
return original_set_width(width)
end

vim.cmd 'CrushWidth 0'

assert.is_false(called)
assert.truthy(messages[1]:find 'number >= 1')

terminal.set_width = original_set_width
vim.notify = original_notify
end)
end)
end)
22 changes: 22 additions & 0 deletions tests/terminal_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ describe('neocrush.terminal', function()
end)
end)

describe('set_width', function()
it('should reject widths smaller than 1', function()
local original_notify = vim.notify
local messages = {}

vim.notify = function(msg)
table.insert(messages, msg)
end

local ok = terminal.set_width(0)

assert.is_false(ok)
assert.truthy(messages[1]:find 'at least 1 column')

vim.notify = original_notify
end)

it('should accept positive widths', function()
assert.is_true(terminal.set_width(42))
end)
end)

describe('close', function()
it('should not error when no terminal is open', function()
assert.has_no.errors(function()
Expand Down
Loading