Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit d2ee219

Browse files
feat(#5): add copying to clipboard
feat: added copy action
1 parent 8512ceb commit d2ee219

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

lua/freeze/init.lua

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local freeze = {
88
theme = "default",
99
config = "base",
1010
open = false,
11+
copy = false,
1112
},
1213
output = nil,
1314
}
@@ -27,6 +28,10 @@ local function onReadStdOut(err, data)
2728
freeze.open(freeze.output)
2829
freeze.output = nil
2930
end
31+
if freeze.opts.copy and freeze.output ~= nil then
32+
freeze.copy(freeze.output)
33+
freeze.output = nil
34+
end
3035
end
3136

3237
---The callback for reading stderr.
@@ -48,7 +53,7 @@ end
4853
local function onExit(stdout, stderr)
4954
return vim.schedule_wrap(function(code, _)
5055
if code == 0 then
51-
vim.notify("Successfully frozen 🍦", vim.log.levels.INFO, { title = "Freeze" })
56+
vim.notify("Successfully frozen 🍦 " .. freeze.opts.output, vim.log.levels.INFO, { title = "Freeze" })
5257
else
5358
vim.notify(stdio.stdout, vim.log.levels.ERROR, { title = "Freeze" })
5459
end
@@ -145,6 +150,96 @@ function freeze.open(filename)
145150
end
146151
end
147152

153+
---Copy command for Windows OS
154+
---@param filename string
155+
local function copy_windows(filename)
156+
local cmd = {
157+
"Add-Type",
158+
"-AssemblyName",
159+
"System.Windows.Forms;",
160+
'[Windows.Forms.Clipboard]::SetImage($([System.Drawing.Image]::FromFile("'
161+
.. loop.cwd()
162+
.. "/"
163+
.. filename
164+
.. '")))',
165+
}
166+
local callback = {
167+
on_sterr = vim.schedule_wrap(function(_, data, _)
168+
local out = table.concat(data, "\n")
169+
onReadStdErr(out)
170+
end),
171+
on_exit = vim.schedule_wrap(function()
172+
vim.notify("frozen frame has been copied to the clipboard", vim.log.levels.INFO, { title = "Freeze" })
173+
end),
174+
}
175+
local job = vim.fn.jobstart(cmd, callback)
176+
vim.fn.jobstop(job)
177+
end
178+
179+
---Copy command for Mac OS
180+
---@param filename string
181+
local function copy_macos(filename)
182+
-- osascript -e 'set the clipboard to (read (POSIX file "/Users/aome/.dotfiles/freeze.png") as JPEG picture)'
183+
local cmd = {
184+
"osascript",
185+
"-e",
186+
"'set the clipboad to (read (POSIX file \"" .. loop.cwd() .. "/" .. filename .. "\") as JPEG picture)'",
187+
}
188+
local callback = {
189+
on_sterr = vim.schedule_wrap(function(_, data, _)
190+
local out = table.concat(data, "\n")
191+
onReadStdErr(out)
192+
end),
193+
on_exit = vim.schedule_wrap(function()
194+
vim.notify("frozen frame has been copied to the clipboard", vim.log.levels.INFO, { title = "Freeze" })
195+
end),
196+
}
197+
local job = vim.fn.jobstart(cmd, callback)
198+
vim.fn.jobstop(job)
199+
end
200+
201+
---Copy command for Unix OS
202+
---@param filename string
203+
local function copy_unix(filename)
204+
if vim.fn.exepath("xclip") == "" then
205+
vim.notify("`xclip` is not installed", vim.log.level.ERROR, { title = "Freeze" })
206+
return
207+
end
208+
local cmd = {
209+
"xclip",
210+
"-selection",
211+
"clipboard",
212+
"-t",
213+
"image/png",
214+
"-i",
215+
loop.cwd() .. "/" .. filename,
216+
}
217+
local callback = {
218+
on_sterr = vim.schedule_wrap(function(_, data, _)
219+
local out = table.concat(data, "\n")
220+
onReadStdErr(out)
221+
end),
222+
on_exit = vim.schedule_wrap(function()
223+
vim.notify("frozen frame has been copied to the clipboard", vim.log.levels.INFO, { title = "Freeze" })
224+
end),
225+
}
226+
local job = vim.fn.jobstart(cmd, callback)
227+
vim.fn.jobstop(job)
228+
end
229+
230+
---Copy the frozen frame to the clipboard
231+
---@param filename string
232+
function freeze.copy(filename)
233+
local os = vim.loop.os_uname().sysname
234+
235+
if os == "Windows" or os == "Window_NT" then
236+
copy_windows(filename)
237+
elseif os == "Darwin" then
238+
copy_macos(filename)
239+
end
240+
copy_unix(filename)
241+
end
242+
148243
--- Setup function for enabling both user commands.
149244
--- Sets up :Freeze for freezing a selection and :FreezeLine
150245
--- to freeze a single line.

0 commit comments

Comments
 (0)