chore(clipboard): separate logic to save as base64 and files (#329)
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
@@ -3,8 +3,10 @@ local Utils = require("avante.utils")
|
||||
---@class AvanteClipboard
|
||||
local M = {}
|
||||
|
||||
---@alias DarwinClipboardCommand "pngpaste" | "osascript"
|
||||
M.clip_cmd = nil
|
||||
|
||||
---@return DarwinClipboardCommand
|
||||
M.get_clip_cmd = function()
|
||||
if M.clip_cmd then
|
||||
return M.clip_cmd
|
||||
@@ -34,7 +36,31 @@ M.has_content = function()
|
||||
return false
|
||||
end
|
||||
|
||||
M.get_content = function()
|
||||
M.save_content = function(filepath)
|
||||
local cmd = M.get_clip_cmd()
|
||||
---@type vim.SystemCompleted
|
||||
local output
|
||||
|
||||
if cmd == "pngpaste" then
|
||||
output = Utils.shell_run(('pngpaste - > "%s"'):format(filepath))
|
||||
return output.code == 0
|
||||
elseif cmd == "osascript" then
|
||||
output = Utils.shell_run(
|
||||
string.format(
|
||||
[[osascript -e 'set theFile to (open for access POSIX file "%s" with write permission)' ]]
|
||||
.. [[-e 'try' -e 'write (the clipboard as «class PNGf») to theFile' -e 'end try' ]]
|
||||
.. [[-e 'close access theFile' -e 'do shell script "cat %s > %s"']],
|
||||
filepath,
|
||||
filepath,
|
||||
filepath
|
||||
)
|
||||
)
|
||||
return output.code == 0
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
M.get_base64_content = function()
|
||||
local cmd = M.get_clip_cmd()
|
||||
---@type vim.SystemCompleted
|
||||
local output
|
||||
|
||||
@@ -1,16 +1,28 @@
|
||||
---NOTE: this module is inspired by https://github.com/HakonHarnes/img-clip.nvim/tree/main
|
||||
---@see https://github.com/ekickx/clipboard-image.nvim/blob/main/lua/clipboard-image/paste.lua
|
||||
|
||||
local Path = require("plenary.path")
|
||||
local Utils = require("avante.utils")
|
||||
local Config = require("avante.config")
|
||||
|
||||
---@class AvanteClipboard
|
||||
---@field clip_cmd string
|
||||
---@field get_clip_cmd fun(): string
|
||||
---@field has_content fun(): boolean
|
||||
---@field get_content fun(): string
|
||||
---@field get_base64_content fun(): string
|
||||
---@field save_content fun(filename: string): boolean
|
||||
---
|
||||
---@class avante.Clipboard: AvanteClipboard
|
||||
local M = {}
|
||||
|
||||
M.paste_directory = Path:new(Config.history.storage_path):joinpath("pasted_images")
|
||||
|
||||
M.setup = function()
|
||||
if not M.paste_directory:exists() then
|
||||
M.paste_directory:mkdir({ parent = true })
|
||||
end
|
||||
end
|
||||
|
||||
return setmetatable(M, {
|
||||
__index = function(t, k)
|
||||
local os_mapping = Utils.get_os_name()
|
||||
|
||||
@@ -37,7 +37,22 @@ M.has_content = function()
|
||||
return false
|
||||
end
|
||||
|
||||
M.get_content = function()
|
||||
M.save_content = function(filepath)
|
||||
local cmd = M.get_clip_cmd()
|
||||
---@type vim.SystemCompleted
|
||||
local output
|
||||
|
||||
if cmd == "xclip" then
|
||||
output = Utils.shell_run(('xclip -selection clipboard -o -t image/png > "%s"'):format(filepath))
|
||||
return output.code == 0
|
||||
elseif cmd == "wl-paste" then
|
||||
output = Utils.shell_run(('wl-paste --type image/png > "%s"'):format(filepath))
|
||||
return output.code == 0
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
M.get_base64_content = function()
|
||||
local cmd = M.get_clip_cmd()
|
||||
---@type vim.SystemCompleted
|
||||
local output
|
||||
|
||||
@@ -30,7 +30,23 @@ M.has_content = function()
|
||||
return false
|
||||
end
|
||||
|
||||
M.get_content = function()
|
||||
M.save_content = function(filepath)
|
||||
local cmd = M.get_clip_cmd()
|
||||
---@type vim.SystemCompleted
|
||||
local output
|
||||
|
||||
if cmd == "powershell.exe" then
|
||||
output = Utils.shell_run(
|
||||
("Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::GetImage().Save('%s')"):format(
|
||||
filepath
|
||||
)
|
||||
)
|
||||
return output.code == 0
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
M.get_base64_content = function()
|
||||
local cmd = M.get_clip_cmd()
|
||||
---@type vim.SystemCompleted
|
||||
local output
|
||||
|
||||
Reference in New Issue
Block a user