Skip to content

Commit 451655c

Browse files
feat: added copy image to clipboard command
1 parent 4551cfd commit 451655c

File tree

7 files changed

+205
-129
lines changed

7 files changed

+205
-129
lines changed

lua/freeze-code/commands.lua

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
local M = {}
22

3-
local config = require("freeze-code.config")
3+
local os_utils = require("freeze-code.utils").os
4+
local is_win = os_utils.is_win
5+
local is_macos = os_utils.is_macos
6+
-- local is_unix = os_utils.is_unix
7+
8+
local tmp_freeze_path = "/tmp/freeze-code.nvim"
9+
10+
local setup_bin_path = function()
11+
if not vim.loop.fs_stat(tmp_freeze_path) then
12+
vim.fn.system({
13+
"git",
14+
"clone",
15+
"--filter=blob:none",
16+
"https://github.com/AlejandroSuero/freeze-code.nvim.git",
17+
"--branch=main", -- latest stable release
18+
tmp_freeze_path,
19+
})
20+
end
21+
end
422

5-
local job = {}
23+
M.job = {}
624

7-
local stdio = { stdout = "", stderr = "" }
25+
M.stdio = { stdout = "", stderr = "" }
826

927
---Closing job in a safely way
1028
---@param h uv_pipe_t|uv_process_t: `stdout|stderr|handle`
@@ -15,52 +33,56 @@ local function safe_close(h)
1533
end
1634

1735
local function stop_job()
18-
if job == nil then
36+
if M.job == nil then
1937
return
2038
end
21-
if not job.stdout == nil then
22-
job.stdout:read_stop()
23-
safe_close(job.stdout)
39+
if not M.job.stdout == nil then
40+
M.job.stdout:read_stop()
41+
safe_close(M.job.stdout)
2442
end
25-
if not job.stderr == nil then
26-
job.stderr:read_stop()
27-
safe_close(job.stderr)
43+
if not M.job.stderr == nil then
44+
M.job.stderr:read_stop()
45+
safe_close(M.job.stderr)
2846
end
29-
if not job.handle == nil then
30-
safe_close(job.handle)
47+
if not M.job.handle == nil then
48+
safe_close(M.job.handle)
3149
end
32-
job = nil
50+
M.job = nil
3351
end
3452

3553
---The function called on exit of from the event loop
3654
---@param msg string: Message to display if success
3755
---@return function cb: Schedule wrap callback function
38-
local function on_exit(msg)
56+
function M.on_exit(msg)
57+
local freeze_code = require("freeze-code")
3958
return vim.schedule_wrap(function(code, _)
4059
if code == 0 then
4160
vim.notify("[freeze-code] " .. msg, vim.log.levels.INFO, { title = "FreezeCode" })
4261
else
43-
vim.notify(stdio.stdout, vim.log.levels.ERROR, { title = "Freeze" })
62+
vim.notify(M.stdio.stdout, vim.log.levels.ERROR, { title = "Freeze" })
63+
end
64+
if freeze_code.config.copy == true then
65+
freeze_code.copy(freeze_code.config)
4466
end
4567
stop_job()
4668
end)
4769
end
4870

49-
local function on_output(err, data)
71+
function M.on_output(err, data)
5072
if err then
5173
-- what should we really do here?
5274
vim.api.nvim_err_writeln(vim.inspect(err))
5375
end
5476
if data then
55-
stdio.stdout = stdio.stdout .. data
77+
M.stdio.stdout = M.stdio.stdout .. data
5678
end
5779
end
5880

5981
---Checks if the given cmd executes.
6082
---@param cmd string
6183
---@param path_to_check string
6284
---@return boolean success: true if executes, false otherwise
63-
local function check_executable(cmd, path_to_check)
85+
function M.check_executable(cmd, path_to_check)
6486
if vim.fn.executable(cmd) == 0 then
6587
vim.api.nvim_err_write(
6688
string.format(
@@ -73,64 +95,36 @@ local function check_executable(cmd, path_to_check)
7395
return true
7496
end
7597

76-
---Freeze file with a range
77-
---@param s_line? number: line to start range
78-
---@param e_line? number: line to start range
79-
---@param opts? FreezeConfig
80-
M.freeze = function(s_line, e_line, opts)
81-
config = vim.tbl_extend("force", {}, config, opts or {})
82-
s_line = s_line or 1
83-
e_line = e_line or vim.api.nvim_buf_line_count(0)
84-
85-
local cmd = config.freeze_path
98+
local copy_by_os = function(opts)
99+
setup_bin_path()
100+
local bin_path = tmp_freeze_path .. "/bin"
101+
local binaries = {
102+
macos = bin_path .. "/pngcopy-macos",
103+
linux = bin_path .. "/pngcopy-linux",
104+
windows = bin_path .. "/pngcopy-windows.ps1",
105+
}
86106

87-
if not check_executable("freeze", cmd) then
88-
return
107+
local cmd = ""
108+
if is_win then
109+
cmd = "pwsh " .. binaries.windows .. " " .. opts.output
110+
return os.execute(cmd)
111+
elseif is_macos then
112+
cmd = "sh " .. binaries.macos .. " " .. opts.output
113+
return os.execute(cmd)
89114
end
115+
cmd = "sh " .. binaries.linux .. " " .. opts.output
116+
os.execute(cmd)
117+
end
90118

91-
local lang = vim.api.nvim_buf_get_option(0, "filetype")
92-
local file = vim.api.nvim_buf_get_name(0)
93-
local conf = config.freeze_config.config
94-
local dir = config.dir
95-
local theme = config.freeze_config.theme
96-
local output = config.freeze_config.output
97-
98-
if output ~= "freeze" then
99-
local t_stamp = os.date("%Y%m%d%H%M%S")
100-
local filename = file:match("^.+/(.*)$") or file
101-
102-
output = string.format("%s_%s_%s", tostring(t_stamp), filename, output)
119+
M.copy = function(opts)
120+
copy_by_os(opts)
121+
local cmd = ""
122+
if is_win then
123+
cmd = "rm -r -Force " .. tmp_freeze_path
124+
else
125+
cmd = "rm -rf " .. tmp_freeze_path
103126
end
104-
105-
config.output = dir .. "/" .. output .. ".png"
106-
107-
local cmd_args = {
108-
"--output",
109-
config.output,
110-
"--language",
111-
lang,
112-
"--lines",
113-
s_line .. "," .. e_line,
114-
"--config",
115-
conf,
116-
"--theme",
117-
theme,
118-
file,
119-
}
120-
121-
job = {}
122-
job.stdout = vim.loop.new_pipe(false)
123-
job.stderr = vim.loop.new_pipe(false)
124-
125-
local job_opts = {
126-
args = cmd_args,
127-
stdio = { nil, job.stdout, job.stderr },
128-
}
129-
130-
local msg = "frozen frame in path=" .. config.output
131-
job.handle = vim.loop.spawn(cmd, job_opts, on_exit(msg))
132-
vim.loop.read_start(job.stdout, vim.schedule_wrap(on_output))
133-
vim.loop.read_start(job.stderr, vim.schedule_wrap(on_output))
127+
os.execute(cmd)
134128
end
135129

136130
return M

lua/freeze-code/config.lua

Lines changed: 0 additions & 34 deletions
This file was deleted.

lua/freeze-code/health.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ local ok = health.ok or health.report_ok
55
local warn = health.warn or health.report_warn
66
local error = health.error or health.report_error
77

8-
local is_win = vim.api.nvim_call_function("has", { "win32" }) == 1
9-
local is_macos = vim.api.nvim_call_function("has", { "macunix" }) == 1
8+
local os_util = require("freeze-code.utils").os
9+
10+
local is_win = os_util.is_win
11+
local is_macos = os_util.is_macos
1012

1113
---@class FreezeCodeHealthPackage
1214
---@field name string: package name

lua/freeze-code/init.lua

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,129 @@
1+
---@meta
2+
---@class FreezeConfig
3+
---@field output string: Freeze output filename `--output "freeze.png"`
4+
---@field theme string: Freeze theme `--theme "default"`
5+
---@field config string: Freeze configuration `--config "base"`
6+
7+
---@class FreezeCodeConfig
8+
---@field freeze_path string: Path to `freeze` executable
9+
---@field copy_cmd string: Path to copy `image/png` to clipboard command
10+
---@field copy boolean: Open image after creation option
11+
---@field open boolean: Open image after creation option
12+
---@field dir string: Directory to create image
13+
---@field freeze_config FreezeConfig
14+
---@field output string|nil: output filename
15+
16+
---@type FreezeConfig
17+
local freeze_config = {
18+
output = "freeze",
19+
config = "base",
20+
theme = "default",
21+
}
22+
23+
---@type FreezeCodeConfig
24+
local config = {
25+
freeze_path = vim.fn.exepath("freeze"),
26+
copy_cmd = vim.env.HOME .. "/dev/nvim_plugins/freeze-code.nvim/bin/pngcopy-macos",
27+
copy = false,
28+
open = false,
29+
dir = vim.env.PWD,
30+
freeze_config = freeze_config,
31+
output = nil,
32+
}
33+
134
---@class FreezeCode
235
---@field setup function
336
---@field config FreezeCodeConfig
437
---@field freeze function: Running `freeze` function
38+
---@field copy function: Copying image to clipboard
539

640
---@type FreezeCode|{}
741
local freeze_code = {}
842

9-
freeze_code.config = require("freeze-code.config")
10-
freeze_code.freeze = require("freeze-code.commands").freeze
43+
local commands = require("freeze-code.commands")
44+
45+
freeze_code.config = config
46+
47+
freeze_code.copy = function(opts)
48+
commands.copy(opts)
49+
end
50+
51+
---Freeze file with a range
52+
---@param s_line? number: line to start range
53+
---@param e_line? number: line to start range
54+
freeze_code.freeze = function(s_line, e_line)
55+
s_line = s_line or 1
56+
e_line = e_line or vim.api.nvim_buf_line_count(0)
57+
58+
local cmd = freeze_code.config.freeze_path
59+
60+
if not commands.check_executable("freeze", cmd) then
61+
return
62+
end
63+
64+
local lang = vim.api.nvim_buf_get_option(0, "filetype")
65+
local file = vim.api.nvim_buf_get_name(0)
66+
local conf = freeze_code.config.freeze_config.config
67+
local dir = freeze_code.config.dir
68+
local theme = freeze_code.config.freeze_config.theme
69+
local output = freeze_code.config.freeze_config.output
70+
71+
if output ~= "freeze" then
72+
local t_stamp = os.date("%Y%m%d%H%M%S")
73+
local filename = file:match("^.+/(.*)$") or file
74+
75+
output = string.format("%s_%s_%s", tostring(t_stamp), filename, output)
76+
end
77+
78+
freeze_code.config.output = dir .. "/" .. output .. ".png"
79+
80+
local cmd_args = {
81+
"--output",
82+
freeze_code.config.output,
83+
"--language",
84+
lang,
85+
"--lines",
86+
s_line .. "," .. e_line,
87+
"--config",
88+
conf,
89+
"--theme",
90+
theme,
91+
file,
92+
}
93+
94+
commands.job = {}
95+
commands.job.stdout = vim.loop.new_pipe(false)
96+
commands.job.stderr = vim.loop.new_pipe(false)
97+
98+
local job_opts = {
99+
args = cmd_args,
100+
stdio = { nil, commands.job.stdout, commands.job.stderr },
101+
}
102+
103+
local msg = "🍧 frozen frame in path=" .. freeze_code.config.output
104+
commands.job.handle = vim.loop.spawn(cmd, job_opts, commands.on_exit(msg))
105+
vim.loop.read_start(commands.job.stdout, vim.schedule_wrap(commands.on_output))
106+
vim.loop.read_start(commands.job.stderr, vim.schedule_wrap(commands.on_output))
107+
end
108+
109+
local create_autocmds = function()
110+
vim.api.nvim_create_user_command("Freeze", function(opts)
111+
vim.api.nvim_out_write("[freeze-code] Freeze called")
112+
if opts.count > 0 then
113+
freeze_code.freeze(opts.line1, opts.line2)
114+
else
115+
freeze_code.freeze()
116+
end
117+
end, {
118+
range = true,
119+
})
120+
end
11121

12122
---freeze-code's set up function
13123
---@param opts {}|nil
14124
freeze_code.setup = function(opts)
15-
freeze_code.config = require("freeze-code.config")
16-
freeze_code.config = vim.tbl_extend("force", {}, freeze_code.config, opts or {})
125+
freeze_code.config = vim.tbl_deep_extend("force", {}, freeze_code.config, opts or {})
126+
create_autocmds()
17127
end
18128

19129
return freeze_code

lua/freeze-code/utils/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
---@meta
2+
3+
---@class FreezeCodeOS
4+
---@field is_win boolean
5+
---@field is_macos boolean
6+
---@field is_unix boolean
7+
18
---@class FreezeCodeUtils
29
---@field logger FreezeCodeLogger
10+
---@fiels os FreezeCodeOS
311
---@type FreezeCodeUtils|{}
412
local M = {}
513

614
M.logger = require("freeze-code.utils.logger")
715

16+
M.os = {
17+
is_win = vim.api.nvim_call_function("has", { "win32" }) == 1,
18+
is_macos = vim.api.nvim_call_function("has", { "macunix" }) == 1,
19+
is_unix = vim.api.nvim_call_function("has", { "unix" }) == 1,
20+
}
21+
822
return M

0 commit comments

Comments
 (0)