Skip to content

Commit 5e45018

Browse files
authored
feat(output): add fold support for tool output (#367)
* feat(output): add fold support for tool output This should resolves #351
1 parent 92ad9bf commit 5e45018

22 files changed

Lines changed: 367 additions & 55 deletions

.github/workflows/tests.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ jobs:
1414
name: lint
1515
steps:
1616
- uses: actions/checkout@v4
17-
1817
- uses: JohnnyMorganz/stylua-action@v4
1918
with:
20-
token: ${{ secrets.GITHUB_TOKEN }}
2119
version: latest
20+
token: ${{ secrets.GITHUB_TOKEN }}
2221
args: --check . -g '*.lua' -g '!deps/'
2322

2423
test:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ require('opencode').setup({
240240
tools = {
241241
show_output = true, -- Show tools output [diffs, cmd output, etc.] (default: true)
242242
show_reasoning_output = true, -- Show reasoning/thinking steps output (default: true)
243+
use_folds = true, -- Use folds for tool output (default: true)
244+
folding_threshold = 5, -- Number of lines to show before folding when show_output is true (default: 5)
243245
},
244246
rendering = {
245247
markdown_debounce_ms = 250, -- Debounce time for markdown rendering on new data (default: 250ms)

lua/opencode/config.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ M.defaults = {
163163
tools = {
164164
show_output = true,
165165
show_reasoning_output = true,
166+
use_folds = true,
167+
folding_threshold = 20,
166168
},
169+
167170
max_messages = nil,
168171
always_scroll_to_bottom = false,
169172
},

lua/opencode/types.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
---@field event_collapsing boolean
243243

244244
---@class OpencodeUIOutputConfig
245-
---@field tools { show_output: boolean, show_reasoning_output: boolean }
245+
---@field tools { show_output: boolean, show_reasoning_output: boolean, use_folds: boolean, folding_threshold: number }
246246
---@field rendering OpencodeUIOutputRenderingConfig
247247
---@field max_messages integer|nil
248248
---@field always_scroll_to_bottom boolean

lua/opencode/ui/formatter.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ function M._format_reasoning(output, part)
3535

3636
format_utils.format_action(output, icons.get('reasoning'), title, '')
3737

38-
if config.ui.output.tools.show_reasoning_output and text ~= '' then
38+
local show = config.ui.output.tools.show_reasoning_output
39+
local use_folds = config.ui.output.tools.use_folds
40+
if (show or use_folds) and text ~= '' then
3941
output:add_empty_line()
4042
output:add_lines(vim.split(text, '\n'))
4143
output:add_empty_line()
44+
output:add_fold_with_threshold(start_line, show, use_folds)
4245
end
4346

4447
local end_line = output:get_line_count()

lua/opencode/ui/formatter/tools/apply_patch.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ function M.format(output, part)
3838
)
3939

4040
local patch = file.diff or file.patch
41-
if config.ui.output.tools.show_output and patch then
41+
if (config.ui.output.tools.show_output or config.ui.output.tools.use_folds) and patch then
42+
local start_line = output:get_line_count() + 1
4243
local file_type = file and util.get_markdown_filetype(file.filePath) or ''
4344
formatter_utils.format_diff(output, patch, file_type)
45+
output:add_fold_with_threshold(start_line, config.ui.output.tools.show_output, config.ui.output.tools.use_folds)
4446
end
4547
end
4648
end

lua/opencode/ui/formatter/tools/bash.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function M.format(output, part)
2020
local icons = require('opencode.ui.icons')
2121
utils.format_action(output, icons.get('run'), 'run', input.description, utils.get_duration_text(part))
2222

23-
if not config.ui.output.tools.show_output then
23+
local start_line = output:get_line_count() + 1
24+
if not (config.ui.output.tools.show_output or config.ui.output.tools.use_folds) then
2425
return
2526
end
2627

@@ -29,6 +30,8 @@ function M.format(output, part)
2930
local command_output = metadata.output and metadata.output ~= '' and ('\n' .. metadata.output) or ''
3031
utils.format_code(output, vim.split('> ' .. command .. '\n' .. command_output, '\n'), 'bash')
3132
end
33+
34+
output:add_fold_with_threshold(start_line, config.ui.output.tools.show_output, config.ui.output.tools.use_folds)
3235
end
3336

3437
---@param _ OpencodeMessagePart

lua/opencode/ui/formatter/tools/file.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ function M.format(output, part)
6565
local icons = require('opencode.ui.icons')
6666
utils.format_action(output, icons.get(tool_type), tool_type, file_name, utils.get_duration_text(part))
6767

68-
if not config.ui.output.tools.show_output then
68+
local start_line = output:get_line_count() + 1
69+
if not (config.ui.output.tools.show_output or config.ui.output.tools.use_folds) then
6970
return
7071
end
7172

@@ -74,6 +75,8 @@ function M.format(output, part)
7475
elseif tool_type == 'write' and input.content then
7576
utils.format_code(output, vim.split(input.content, '\n'), file_type)
7677
end
78+
79+
output:add_fold_with_threshold(start_line, config.ui.output.tools.show_output, config.ui.output.tools.use_folds)
7780
end
7881

7982
---@param part OpencodeMessagePart

lua/opencode/ui/formatter/tools/glob.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ function M.format(output, part)
1616

1717
local icons = require('opencode.ui.icons')
1818
utils.format_action(output, icons.get('search'), 'glob', input.pattern, utils.get_duration_text(part))
19-
if not config.ui.output.tools.show_output then
19+
20+
local start_line = output:get_line_count() + 1
21+
if not (config.ui.output.tools.show_output or config.ui.output.tools.use_folds) then
2022
return
2123
end
2224

2325
local prefix = metadata.truncated and ' more than' or ''
2426
output:add_line(string.format('Found%s `%d` file(s):', prefix, metadata.count or 0))
27+
28+
output:add_fold_with_threshold(start_line, config.ui.output.tools.show_output, config.ui.output.tools.use_folds)
2529
end
2630

2731
---@param _ OpencodeMessagePart

lua/opencode/ui/formatter/tools/grep.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,18 @@ function M.format(output, part)
5353

5454
local icons = require('opencode.ui.icons')
5555
utils.format_action(output, icons.get('search'), 'grep', resolve_grep_string(input), utils.get_duration_text(part))
56-
if not config.ui.output.tools.show_output then
56+
57+
local start_line = output:get_line_count() + 1
58+
if not (config.ui.output.tools.show_output or config.ui.output.tools.use_folds) then
5759
return
5860
end
5961

6062
local prefix = metadata.truncated and ' more than' or ''
6163
output:add_line(
6264
string.format('Found%s `%d` match' .. (metadata.matches ~= 1 and 'es' or ''), prefix, metadata.matches or 0)
6365
)
66+
67+
output:add_fold_with_threshold(start_line, config.ui.output.tools.show_output, config.ui.output.tools.use_folds)
6468
end
6569

6670
---@param _ OpencodeMessagePart

0 commit comments

Comments
 (0)