Skip to content

Commit 166d966

Browse files
committed
refactor(image_handler): pass all commands to run_shell_cmd
1 parent a8f638a commit 166d966

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lua/opencode/image_handler.lua

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ local function is_valid_file(path)
1212
return vim.fn.getfsize(path) > 0
1313
end
1414

15-
--- Run shell command and return success
16-
--- @param cmd string
15+
--- Run shell or powershell command and return success
16+
--- @param cmd string|table
17+
--- @param opts table?
1718
--- @return boolean
18-
local function run_shell_cmd(cmd)
19-
return vim.system({ 'sh', '-c', cmd }):wait().code == 0
19+
local function run_shell_cmd(cmd, opts)
20+
local sys_cmd
21+
if type(cmd) == 'string' then
22+
sys_cmd = { 'sh', '-c', cmd }
23+
else
24+
sys_cmd = cmd
25+
end
26+
return vim.system(sys_cmd, opts):wait().code == 0
2027
end
2128

2229
--- Save base64 data to file
@@ -27,11 +34,10 @@ local function save_base64(data, path)
2734
if vim.fn.has('win32') == 1 then
2835
local script =
2936
string.format('[System.IO.File]::WriteAllBytes("%s", [System.Convert]::FromBase64String("%s"))', path, data)
30-
return vim.system({ 'powershell.exe', '-command', '-' }, { stdin = script }):wait().code == 0
37+
return run_shell_cmd({ 'powershell.exe', '-command', '-' }, { stdin = script })
3138
else
3239
local decode_arg = vim.uv.os_uname().sysname == 'Darwin' and '-D' or '-d'
33-
return vim.system({ 'sh', '-c', string.format('base64 %s > "%s"', decode_arg, path) }, { stdin = data }):wait().code
34-
== 0
40+
return run_shell_cmd(string.format('base64 %s > "%s"', decode_arg, path), { stdin = data })
3541
end
3642
end
3743

@@ -79,7 +85,7 @@ local function handle_windows_clipboard(path)
7985
]],
8086
path
8187
)
82-
return vim.system({ 'powershell.exe', '-command', '-' }, { stdin = script }):wait().code == 0
88+
return run_shell_cmd({ 'powershell.exe', '-command', '-' }, { stdin = script })
8389
end
8490

8591
local handlers = {

0 commit comments

Comments
 (0)