Skip to content

Commit e924331

Browse files
refactor(#8): command executions (#9)
* refactor(copy): to use `vim.fn.system` and not `bin/<copy-command>` folder * feat(health): updated dependencies * docs(README): updated `copy` information * docs: update `doc/freeze-code.nvim.txt` skip-checks: true --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 16b24ab commit e924331

File tree

10 files changed

+129
-96
lines changed

10 files changed

+129
-96
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ return {
7575
```lua
7676
local opts = {
7777
freeze_path = vim.fn.exepath("freeze"), -- where is freeze installed
78-
copy_cmd = "pngcopy", -- the default copy commands are in the bin directory
78+
copy_cmd = "gclip", -- the default copy commands `gclip` or native to your OS (see below)
7979
copy = false, -- copy after screenshot option
8080
open = false, -- open after screenshot option
8181
dir = vim.env.PWD, -- where is the image going to be saved "." as default
@@ -89,8 +89,14 @@ local opts = {
8989

9090
> [!note]
9191
>
92-
> The commands to copy, as defaults per OS will be in the
93-
> [bin-directory](https://github.com/AlejandroSuero/freeze-code.nvim/blob/main/bin)
92+
> The default command will be [gclip](https://github.com/golang-design/clipboard)
93+
> if it is installed, otherwise ...
94+
>
95+
> The commands to copy, as defaults per OS will be, for example, for Windows:
96+
> `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::SetImage(...)`,
97+
> for Linux: `xclip -selection clipboard -t image/png ...` if is an `X11` session,
98+
> `wl-copy < ...` if is a `Wayland` session, and for MacOS:
99+
> `osascript -e 'to set the clipboard to (read (POSIX file "...") as «class PNGf»)'`.
94100
95101
Once you have it installed, you can use `:checkhealt freeze-code` to see if there
96102
are any problems with the installation or you need to install additional tools.

bin/pngcopy-linux

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

bin/pngcopy-macos

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

bin/pngcopy-windows.ps1

Lines changed: 0 additions & 1 deletion
This file was deleted.

doc/freeze-code.nvim.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*freeze-code.nvim.txt* For Neovim >= 0.9.0 Last change: 2024 May 14
1+
*freeze-code.nvim.txt* For Neovim >= 0.9.0 Last change: 2024 May 19
22

33
==============================================================================
44
Table of Contents *freeze-code.nvim-table-of-contents*
@@ -61,7 +61,7 @@ Using your plugin manager at your disposal, in the example lazy
6161
>lua
6262
local opts = {
6363
freeze_path = vim.fn.exepath("freeze"), -- where is freeze installed
64-
copy_cmd = "pngcopy", -- the default copy commands are in the bin directory
64+
copy_cmd = "gclip", -- the default copy commands `gclip` or native to your OS (see below)
6565
copy = false, -- copy after screenshot option
6666
open = false, -- open after screenshot option
6767
dir = vim.env.PWD, -- where is the image going to be saved "." as default
@@ -75,8 +75,14 @@ Using your plugin manager at your disposal, in the example lazy
7575

7676

7777
[!note]
78-
The commands to copy, as defaults per OS will be in the bin-directory
79-
<https://github.com/AlejandroSuero/freeze-code.nvim/blob/main/bin>
78+
The default command will be gclip <https://github.com/golang-design/clipboard>
79+
if it is installed, otherwise …
80+
The commands to copy, as defaults per OS will be, for example, for Windows:
81+
`Add-Type -AssemblyName System.Windows.Forms;
82+
[System.Windows.Forms.Clipboard]::SetImage(...)`, for Linux: `xclip -selection
83+
clipboard -t image/png ...` if is an `X11` session, `wl-copy < ...` if is a
84+
`Wayland` session, and for MacOS: `osascript -e 'to set the clipboard to (read
85+
(POSIX file "...") as «class PNGf»)'`.
8086
Once you have it installed, you can use `:checkhealt freeze-code` to see if
8187
there are any problems with the installation or you need to install additional
8288
tools.

lua/freeze-code/commands.lua

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@ local logger = utils.logger
55
local os_utils = utils.os
66
local is_win = os_utils.is_win
77
local is_macos = os_utils.is_macos
8-
-- local is_unix = os_utils.is_unix
9-
10-
local tmp_freeze_path = "/tmp/freeze-code.nvim"
11-
12-
local setup_bin_path = function()
13-
if not vim.loop.fs_stat(tmp_freeze_path) then
14-
vim.fn.system({
15-
"git",
16-
"clone",
17-
"--filter=blob:none",
18-
"https://github.com/AlejandroSuero/freeze-code.nvim.git",
19-
"--branch=main", -- latest stable release
20-
tmp_freeze_path,
21-
})
22-
end
23-
end
8+
local is_unix = os_utils.is_unix
249

2510
M.job = {}
2611

@@ -59,7 +44,7 @@ function M.on_exit(msg, opts)
5944
local freeze_code = require("freeze-code")
6045
return vim.schedule_wrap(function(code, _)
6146
if code == 0 then
62-
vim.notify("[freeze-code] " .. msg, vim.log.levels.INFO, { title = "FreezeCode" })
47+
vim.notify("[freeze-code.nvim] " .. msg, vim.log.levels.INFO, { title = "FreezeCode" })
6348
else
6449
vim.notify(M.stdio.stdout, vim.log.levels.ERROR, { title = "Freeze" })
6550
end
@@ -102,38 +87,44 @@ function M.check_executable(cmd, path_to_check)
10287
end
10388

10489
local copy_by_os = function(opts)
105-
setup_bin_path()
106-
local bin_path = tmp_freeze_path .. "/bin"
107-
local binaries = {
108-
macos = bin_path .. "/pngcopy-macos",
109-
linux = bin_path .. "/pngcopy-linux",
110-
windows = bin_path .. "/pngcopy-windows.ps1",
111-
}
112-
113-
local cmd = ""
90+
local cmd = {}
91+
local filename = vim.fn.expand(opts.output)
92+
if vim.fn.executable("gclip") == 0 then
93+
cmd = { "gclip", "-copy", "-f", filename }
94+
return vim.fn.system(table.concat(cmd, " "))
95+
end
11496
if is_win then
115-
cmd = "pwsh " .. binaries.windows .. " " .. opts.output
116-
return os.execute(cmd)
97+
cmd = {
98+
"Add-Type",
99+
"-AssemblyName",
100+
"System.Windows.Forms",
101+
";",
102+
"[Windows.Forms.Clipboard]::SetImage($[System.Drawing.Image]::FromFile(" .. filename .. "))",
103+
}
117104
elseif is_macos then
118-
cmd = "sh " .. binaries.macos .. " " .. opts.output
119-
return os.execute(cmd)
105+
cmd = {
106+
"osascript",
107+
"-e",
108+
"'set the clipboard to (read (POSIX file \"" .. filename .. "\") as {«class PNGf»})'",
109+
}
120110
end
121-
cmd = "sh " .. binaries.linux .. " " .. opts.output
122-
local ok = os.execute(cmd)
123-
if ok then
124-
logger.info_fmt("[freeze-code] image `%s` copied to the clipboard", opts.output)
111+
if is_unix then
112+
if vim.env.XDG_SESSION_TYPE == "x11" then
113+
cmd = { "xclip", "-selection", "clipboard", "-t", "image/png", "-i", filename }
114+
else
115+
cmd = { "wl-copy", "<", filename }
116+
end
125117
end
118+
return vim.fn.system(table.concat(cmd, " "))
126119
end
127120

128121
M.copy = function(opts)
129122
copy_by_os(opts)
130-
local cmd = ""
131-
if is_win then
132-
cmd = "rm -r -Force " .. tmp_freeze_path
133-
else
134-
cmd = "rm -rf " .. tmp_freeze_path
123+
if vim.v.shell_error ~= 0 then
124+
logger.err_once("[freeze-code.nvim] error while copying image to clipboard")
125+
return
135126
end
136-
os.execute(cmd)
127+
logger.info("[freeze-code.nvim] image copied to clipboard")
137128
end
138129

139130
return M

lua/freeze-code/health.lua

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,53 @@ local optional_dependencies = {
109109
{
110110
cmd_name = "Add-Type",
111111
package = {
112-
name = "powershell",
113-
cmd = { "pwsh" },
114-
args = { "--version" },
115-
url = "[PowerShell/PowerShell](https://github.com/PowerShell/PowerShell)",
116-
optional = true,
117-
platform = "windows",
112+
{
113+
name = "powershell",
114+
cmd = { "pwsh" },
115+
args = { "--version" },
116+
url = "[PowerShell/PowerShell](https://github.com/PowerShell/PowerShell)",
117+
optional = true,
118+
platform = "windows",
119+
},
120+
},
121+
},
122+
{
123+
cmd_name = "Clipboard",
124+
package = {
125+
{
126+
name = "gclip",
127+
cmd = { "gclip" },
128+
args = nil,
129+
url = "[golang-design/clipboard](https://github.com/golang-design/clipboard)",
130+
optional = true,
131+
platform = "all",
132+
},
118133
},
119134
},
120135
{
121136
cmd_name = "open",
122137
package = {
123-
name = "open",
124-
cmd = { "open" },
125-
args = nil,
126-
url = "[docs](https://www.man7.org/linux/man-pages/man2/open.2.html)",
127-
optional = true,
128-
platform = "linux",
138+
{
139+
name = "open",
140+
cmd = { "open" },
141+
args = nil,
142+
url = "[docs](https://www.man7.org/linux/man-pages/man2/open.2.html)",
143+
optional = true,
144+
platform = "linux",
145+
},
129146
},
130147
},
131148
{
132149
cmd_name = "explorer",
133150
package = {
134-
name = "explorer",
135-
cmd = { "explorer" },
136-
args = nil,
137-
url = "[docs](https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-windows-explorer/)",
138-
optional = true,
139-
platform = "windows",
151+
{
152+
name = "explorer",
153+
cmd = { "explorer" },
154+
args = nil,
155+
url = "[docs](https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-windows-explorer/)",
156+
optional = true,
157+
platform = "windows",
158+
},
140159
},
141160
},
142161
}
@@ -167,6 +186,7 @@ end
167186
---@return string|any
168187
---@return boolean needed
169188
local check_binary_installed = function(pkg)
189+
print(vim.inspect(pkg))
170190
local needed = check_platform_needed(pkg)
171191
local cmd = pkg.cmd or { pkg.name }
172192
for _, binary in ipairs(cmd) do

lua/freeze-code/init.lua

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ freeze_code.go_install_freeze = function(opts)
140140
"github.com/charmbracelet/freeze@latest",
141141
}
142142
local stdio = { stdout = "", stderr = "" }
143+
local job = nil
143144

144145
local function on_output(err, data)
145146
if err then
@@ -161,9 +162,10 @@ freeze_code.go_install_freeze = function(opts)
161162
freeze_code.setup(opts)
162163
logger.warn("[freeze-code] go install github.com/charmbracelet/freeze@latest completed")
163164
create_autocmds()
165+
vim.fn.jobstop(job)
164166
end),
165167
}
166-
vim.fn.jobstart(cmd_args, callbacks)
168+
job = vim.fn.jobstart(cmd_args, callbacks)
167169
end
168170

169171
---@class FreezeCode
@@ -201,11 +203,12 @@ freeze_code.agnostic_install_freeze = function(opts)
201203
if vim.fn.filereadable(binary_path) == 1 then
202204
local success = vim.loop.fs_unlink(binary_path)
203205
if not success then
204-
logger.err("[freeze-code] ERROR: `freeze` binary could not be removed!")
206+
logger.err("[freeze-code.nvim] ERROR: `freeze` binary could not be removed!")
205207
return
206208
end
207209
end
208210
local stdio = { stdout = "", stderr = "" }
211+
local job = nil
209212

210213
local function on_output(err, data)
211214
if err then
@@ -223,13 +226,17 @@ freeze_code.agnostic_install_freeze = function(opts)
223226
on_output(out)
224227
end),
225228
on_exit = vim.schedule_wrap(function()
226-
logger.info_fmt("[freeze-code] extracting release with `%s`", table.concat(extract_command, " "))
229+
logger.info_fmt("[freeze-code.nvim] extracting release with `%s`", table.concat(extract_command, " "))
227230
vim.fn.system(extract_command)
231+
if vim.v.shell_error ~= 0 then
232+
logger.err("[freeze-code.nvim] ERROR: extracting release failed")
233+
return
234+
end
228235
-- remove the archive after completion
229236
if vim.fn.filereadable(output_filename) == 1 then
230237
local success = vim.loop.fs_unlink(output_filename)
231238
if not success then
232-
logger.err("[freeze-code] ERROR: existing archive could not be removed")
239+
logger.err("[freeze-code.nvim] ERROR: existing archive could not be removed")
233240
return
234241
end
235242
end
@@ -240,13 +247,17 @@ freeze_code.agnostic_install_freeze = function(opts)
240247
opts.install_path = install_path
241248
freeze_code.setup(opts)
242249
vim.loop.spawn("rm", { args = rm_command_args })
243-
logger.warn_fmt("[freeze-code] `freeze` binary installed in installed in path=%s", freeze_code.config.freeze_path)
250+
logger.warn_fmt(
251+
"[freeze-code.nvim] `freeze` binary installed in installed in path=%s",
252+
freeze_code.config.freeze_path
253+
)
244254
freeze_code.setup(opts)
245255
create_autocmds()
256+
vim.fn.jobstop(job)
246257
end),
247258
}
248-
logger.info_fmt("[freeze-code] downloading release from `%s`", release_url)
249-
vim.fn.jobstart(download_command, callbacks)
259+
logger.info_fmt("[freeze-code.nvim] downloading release from `%s`", release_url)
260+
job = vim.fn.jobstart(download_command, callbacks)
250261
end
251262

252263
---@class FreezeCode
@@ -255,11 +266,11 @@ end
255266
---@param opts FreezeCodeConfig
256267
freeze_code.install_freeze = function(opts)
257268
if commands.check_executable("go", vim.fn.exepath("go")) then
258-
logger.warn("[freeze-code] go install github.com/charmbracelet/freeze@latest completed")
269+
logger.warn("[freeze-code.nvim] go install github.com/charmbracelet/freeze@latest completed")
259270
freeze_code.go_install_freeze(opts)
260271
return
261272
end
262-
logger.info("[freeze-code] Installing info with `curl`")
273+
logger.info("[freeze-code.nvim] Installing info with `curl`")
263274
freeze_code.agnostic_install_freeze(opts)
264275
end
265276

@@ -270,7 +281,7 @@ end
270281
---@param e_line? number: line to start range
271282
freeze_code.freeze = function(s_line, e_line)
272283
if not freeze_code.config._installed then
273-
logger.warn("[freeze-code] `freeze` not installed")
284+
logger.warn("[freeze-code.nvim] `freeze` not installed")
274285
freeze_code.install_freeze(freeze_code.config)
275286
return
276287
end
@@ -284,8 +295,14 @@ freeze_code.freeze = function(s_line, e_line)
284295
return
285296
end
286297

287-
local lang = vim.api.nvim_buf_get_option(0, "filetype")
288-
local file = vim.api.nvim_buf_get_name(0)
298+
local lang = ""
299+
if vim.fn.has("nvim-0.10") == 1 then
300+
lang = vim.api.nvim_get_option_value("filetype", { buf = vim.api.nvim_get_current_buf() })
301+
else
302+
---@diagnostic disable-next-line: deprecated
303+
lang = vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "filetype")
304+
end
305+
local file = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
289306
local conf = freeze_code.config.freeze_config.config
290307
local dir = freeze_code.config.dir
291308
local theme = freeze_code.config.freeze_config.theme

lua/freeze-code/utils/init.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ local M = {}
1414
M.logger = require("freeze-code.utils.logger")
1515

1616
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,
17+
is_win = vim.loop.os_uname().version:match("Windows"),
18+
is_macos = vim.loop.os_uname().version:match("Darwin"),
19+
is_unix = vim.loop.os_uname().version:match("Linux"),
2020
}
2121

2222
return M

0 commit comments

Comments
 (0)