Skip to content

Commit 0e7da03

Browse files
committed
fix(formatter): strip ansi from bash/code blocks
1 parent 761487e commit 0e7da03

File tree

5 files changed

+118
-4
lines changed

5 files changed

+118
-4
lines changed

lua/opencode/ui/formatter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ end
620620
function M._format_code(output, lines, language)
621621
output:add_empty_line()
622622
output:add_line('```' .. (language or ''))
623-
output:add_lines(lines)
623+
output:add_lines(util.strip_ansi_lines(lines))
624624
output:add_line('```')
625625
end
626626

lua/opencode/ui/output_window.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function M.setup(windows)
4848
vim.api.nvim_set_option_value('winfixheight', true, { win = windows.output_win })
4949
vim.api.nvim_set_option_value('winfixwidth', true, { win = windows.output_win })
5050
vim.api.nvim_set_option_value('signcolumn', 'yes', { scope = 'local', win = windows.output_win })
51+
vim.api.nvim_set_option_value('list', false, { scope = 'local', win = windows.output_win })
5152

5253
M.update_dimensions(windows)
5354
M.setup_keymaps(windows)

lua/opencode/util.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,23 @@ function M.ansi_reset()
133133
return '\27[0m'
134134
end
135135

136-
-- Remove ANSI escape sequences
137-
--- @param str string: Input string containing ANSI escape codes
136+
---Remove ANSI escape sequences
137+
---@param str string: Input string containing ANSI escape codes
138+
---@return string stripped_str
138139
function M.strip_ansi(str)
139-
return str:gsub('\27%[[%d;]*m', '')
140+
return (str:gsub('\27%[[%d;]*m', ''))
141+
end
142+
143+
---Strip ANSI escape sequences from all lines
144+
---@param lines table
145+
---@return table stripped_lines
146+
function M.strip_ansi_lines(lines)
147+
local stripped_lines = {}
148+
for _, line in pairs(lines) do
149+
table.insert(stripped_lines, M.strip_ansi(line))
150+
end
151+
152+
return stripped_lines
140153
end
141154

142155
--- Convert a datetime to a human-readable "time ago" format

tests/data/ansi-codes.expected.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

tests/data/ansi-codes.json

Lines changed: 99 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)