Skip to content

Commit f0dadf1

Browse files
committed
feat: add tool type check for formatters
1 parent f20f99e commit f0dadf1

11 files changed

Lines changed: 65 additions & 17 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ end
2020
---@param output Output
2121
---@param part OpencodeMessagePart
2222
function M.format(output, part)
23+
if part.tool ~= 'apply_patch' then
24+
return
25+
end
26+
2327
local metadata = part.state and part.state.metadata or {}
2428
for _, file in ipairs(metadata.files or {}) do
2529
local utils = require('opencode.ui.formatter.utils')
@@ -48,7 +52,7 @@ function M.summary(_, _, metadata)
4852
local file = metadata.files and metadata.files[1]
4953
local others_count = metadata.files and #metadata.files - 1 or 0
5054
local suffix = others_count > 0 and string.format(' (+%d more)', others_count) or ''
51-
return 'edit', 'apply patch', file and resolve_file_name(file.filePath) .. suffix or ''
55+
return icons.get('edit'), 'apply patch', file and resolve_file_name(file.filePath) .. suffix or ''
5256
end
5357

5458
return M

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local icons = require('opencode.ui.icons')
12
local M = {}
23

34
---@param output Output
@@ -34,7 +35,7 @@ end
3435
---@param input BashToolInput
3536
---@return string, string, string
3637
function M.summary(_, input)
37-
return 'run', 'run', input.description or ''
38+
return icons.get('run'), 'run', input.description or ''
3839
end
3940

4041
return M

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local util = require('opencode.util')
2+
local icons = require('opencode.ui.icons')
23

34
local M = {}
45

@@ -82,9 +83,9 @@ function M.summary(part, input)
8283
local tool = part.tool
8384
if tool == 'read' then
8485
local tool_output = part.state and part.state.output or nil
85-
return 'read', 'read', resolve_display_file_name(input.filePath, tool_output)
86+
return icons.get('read'), 'read', resolve_display_file_name(input.filePath, tool_output)
8687
end
87-
return tool, tool, resolve_file_name(input.filePath)
88+
return icons.get(tool), tool, resolve_file_name(input.filePath)
8889
end
8990

9091
return M

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
local icons = require('opencode.ui.icons')
12
local M = {}
23

34
---@param output Output
45
---@param part OpencodeMessagePart
56
function M.format(output, part)
7+
if part.tool ~= 'glob' then
8+
return
9+
end
10+
611
local input = part.state and part.state.input or {}
712
local metadata = part.state and part.state.metadata or {}
813

@@ -23,7 +28,7 @@ end
2328
---@param input GlobToolInput
2429
---@return string, string, string
2530
function M.summary(_, input)
26-
return 'search', 'glob', input.pattern or ''
31+
return icons.get('search'), 'glob', input.pattern or ''
2732
end
2833

2934
return M

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local icons = require('opencode.ui.icons')
12
local M = {}
23

34
---@param input GrepToolInput|nil
@@ -19,6 +20,10 @@ end
1920
---@param output Output
2021
---@param part OpencodeMessagePart
2122
function M.format(output, part)
23+
if part.tool ~= 'grep' then
24+
return
25+
end
26+
2227
local metadata = part.state and part.state.metadata or {}
2328
local input = part.state and part.state.input or nil
2429

@@ -41,7 +46,7 @@ end
4146
---@param input GrepToolInput
4247
---@return string, string, string
4348
function M.summary(_, input)
44-
return 'search', 'grep', resolve_grep_string(input)
49+
return icons.get('search'), 'grep', resolve_grep_string(input)
4550
end
4651

4752
return M

lua/opencode/ui/formatter/tools/list.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ local M = {}
33
---@param output Output
44
---@param part OpencodeMessagePart
55
function M.format(output, part)
6+
if part.tool ~= 'list' then
7+
return
8+
end
69
local input = part.state and part.state.input or {}
710
local metadata = part.state and part.state.metadata or {}
811
local tool_output = part.state and part.state.output or ''
@@ -39,7 +42,7 @@ end
3942
---@param input ListToolInput
4043
---@return string, string, string
4144
function M.summary(_, input)
42-
return 'list', 'list', input.path or ''
45+
return icons.get('list'), 'list', input.path or ''
4346
end
4447

4548
return M

lua/opencode/ui/formatter/tools/question.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ local M = {}
33
---@param output Output
44
---@param part OpencodeMessagePart
55
function M.format(output, part)
6+
if part.tool ~= 'question' then
7+
return
8+
end
9+
610
local input = part.state and part.state.input or {}
711
local metadata = part.state and part.state.metadata or {}
812

lua/opencode/ui/formatter/tools/task.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@ function M.tool_action_line(part, status, utils)
1212
local metadata = part.state and part.state.metadata or {}
1313
local formatter = tool_formatters[tool] or tool_formatters.tool
1414
local summary = formatter.summary or tool_formatters.tool.summary
15-
local icon_name, tool_label, tool_value = summary(part, input, metadata)
15+
local icon, tool_label, tool_value = summary(part, input, metadata)
16+
1617
if status ~= 'completed' then
17-
icon_name = status
18+
icon = icons.get(status)
1819
end
1920

20-
local icon = icons.get(icon_name)
2121
return utils.build_action_line(icon, tool_label or tool or 'tool', tool_value)
2222
end
2323

2424
---@param output Output
2525
---@param part OpencodeMessagePart
2626
---@param get_child_parts? fun(session_id: string): OpencodeMessagePart[]?
2727
function M.format(output, part, get_child_parts)
28+
if part.tool ~= 'task' then
29+
return
30+
end
31+
2832
local input = part.state and part.state.input or {}
2933
local metadata = part.state and part.state.metadata or {}
3034
local tool_output = part.state and part.state.output or ''
@@ -84,7 +88,7 @@ end
8488
---@param input TaskToolInput
8589
---@return string, string, string
8690
function M.summary(_, input)
87-
return 'task', 'task', input.description or ''
91+
return icons.get('task'), 'task', input.description or ''
8892
end
8993

9094
return M

lua/opencode/ui/formatter/tools/todowrite.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
local icons = require('opencode.ui.icons')
12
local M = {}
23

34
---@param output Output
45
---@param part OpencodeMessagePart
56
function M.format(output, part)
7+
if part.tool ~= 'todowrite' then
8+
return
9+
end
610
local utils = require('opencode.ui.formatter.utils')
711
local config = require('opencode.config')
812

913
local icons = require('opencode.ui.icons')
10-
utils.format_action(output, icons.get('plan'), 'plan', (part.state and part.state.title or ''), utils.get_duration_text(part))
14+
utils.format_action(
15+
output,
16+
icons.get('plan'),
17+
'plan',
18+
(part.state and part.state.title or ''),
19+
utils.get_duration_text(part)
20+
)
1121
if not config.ui.output.tools.show_output then
1222
return
1323
end
@@ -22,7 +32,7 @@ end
2232
---@param part OpencodeMessagePart
2333
---@return string, string, string
2434
function M.summary(part)
25-
return 'plan', 'plan', part.state and part.state.title or ''
35+
return icons.get('plan'), 'plan', part.state and part.state.title or ''
2636
end
2737

2838
return M

lua/opencode/ui/formatter/tools/tool.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
local M = {}
21
local utils = require('opencode.ui.formatter.utils')
2+
local icons = require('opencode.ui.icons')
3+
local M = {}
34

45
---@param output Output
56
---@param part OpencodeMessagePart
@@ -12,7 +13,7 @@ end
1213
---@param input table
1314
---@return string, string, string
1415
function M.summary(_, input)
15-
return 'tool', 'tool', input.description or ''
16+
return icons.get('tool'), 'tool', input.description or ''
1617
end
1718

1819
return M

0 commit comments

Comments
 (0)