Skip to content

Commit

Permalink
refactor(#8): command executions (#9)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
AlejandroSuero and github-actions[bot] authored May 19, 2024
1 parent 16b24ab commit e924331
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 96 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ return {
```lua
local opts = {
freeze_path = vim.fn.exepath("freeze"), -- where is freeze installed
copy_cmd = "pngcopy", -- the default copy commands are in the bin directory
copy_cmd = "gclip", -- the default copy commands `gclip` or native to your OS (see below)
copy = false, -- copy after screenshot option
open = false, -- open after screenshot option
dir = vim.env.PWD, -- where is the image going to be saved "." as default
Expand All @@ -89,8 +89,14 @@ local opts = {

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

This file was deleted.

8 changes: 0 additions & 8 deletions bin/pngcopy-macos

This file was deleted.

1 change: 0 additions & 1 deletion bin/pngcopy-windows.ps1

This file was deleted.

14 changes: 10 additions & 4 deletions doc/freeze-code.nvim.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*freeze-code.nvim.txt* For Neovim >= 0.9.0 Last change: 2024 May 14
*freeze-code.nvim.txt* For Neovim >= 0.9.0 Last change: 2024 May 19

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


[!note]
The commands to copy, as defaults per OS will be in the bin-directory
<https://github.com/AlejandroSuero/freeze-code.nvim/blob/main/bin>
The default command will be gclip <https://github.com/golang-design/clipboard>
if it is installed, otherwise …
The commands to copy, as defaults per OS will be, for example, for Windows:
`Add-Type -AssemblyName System.Windows.Forms;
[System.Windows.Forms.Clipboard]::SetImage(...)`, for Linux: `xclip -selection
clipboard -t image/png ...` if is an `X11` session, `wl-copy < ...` if is a
`Wayland` session, and for MacOS: `osascript -e 'to set the clipboard to (read
(POSIX file "...") as «class PNGf»)'`.
Once you have it installed, you can use `:checkhealt freeze-code` to see if
there are any problems with the installation or you need to install additional
tools.
Expand Down
71 changes: 31 additions & 40 deletions lua/freeze-code/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@ local logger = utils.logger
local os_utils = utils.os
local is_win = os_utils.is_win
local is_macos = os_utils.is_macos
-- local is_unix = os_utils.is_unix

local tmp_freeze_path = "/tmp/freeze-code.nvim"

local setup_bin_path = function()
if not vim.loop.fs_stat(tmp_freeze_path) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/AlejandroSuero/freeze-code.nvim.git",
"--branch=main", -- latest stable release
tmp_freeze_path,
})
end
end
local is_unix = os_utils.is_unix

M.job = {}

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

local copy_by_os = function(opts)
setup_bin_path()
local bin_path = tmp_freeze_path .. "/bin"
local binaries = {
macos = bin_path .. "/pngcopy-macos",
linux = bin_path .. "/pngcopy-linux",
windows = bin_path .. "/pngcopy-windows.ps1",
}

local cmd = ""
local cmd = {}
local filename = vim.fn.expand(opts.output)
if vim.fn.executable("gclip") == 0 then
cmd = { "gclip", "-copy", "-f", filename }
return vim.fn.system(table.concat(cmd, " "))
end
if is_win then
cmd = "pwsh " .. binaries.windows .. " " .. opts.output
return os.execute(cmd)
cmd = {
"Add-Type",
"-AssemblyName",
"System.Windows.Forms",
";",
"[Windows.Forms.Clipboard]::SetImage($[System.Drawing.Image]::FromFile(" .. filename .. "))",
}
elseif is_macos then
cmd = "sh " .. binaries.macos .. " " .. opts.output
return os.execute(cmd)
cmd = {
"osascript",
"-e",
"'set the clipboard to (read (POSIX file \"" .. filename .. "\") as {«class PNGf»})'",
}
end
cmd = "sh " .. binaries.linux .. " " .. opts.output
local ok = os.execute(cmd)
if ok then
logger.info_fmt("[freeze-code] image `%s` copied to the clipboard", opts.output)
if is_unix then
if vim.env.XDG_SESSION_TYPE == "x11" then
cmd = { "xclip", "-selection", "clipboard", "-t", "image/png", "-i", filename }
else
cmd = { "wl-copy", "<", filename }
end
end
return vim.fn.system(table.concat(cmd, " "))
end

M.copy = function(opts)
copy_by_os(opts)
local cmd = ""
if is_win then
cmd = "rm -r -Force " .. tmp_freeze_path
else
cmd = "rm -rf " .. tmp_freeze_path
if vim.v.shell_error ~= 0 then
logger.err_once("[freeze-code.nvim] error while copying image to clipboard")
return
end
os.execute(cmd)
logger.info("[freeze-code.nvim] image copied to clipboard")
end

return M
56 changes: 38 additions & 18 deletions lua/freeze-code/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,34 +109,53 @@ local optional_dependencies = {
{
cmd_name = "Add-Type",
package = {
name = "powershell",
cmd = { "pwsh" },
args = { "--version" },
url = "[PowerShell/PowerShell](https://github.com/PowerShell/PowerShell)",
optional = true,
platform = "windows",
{
name = "powershell",
cmd = { "pwsh" },
args = { "--version" },
url = "[PowerShell/PowerShell](https://github.com/PowerShell/PowerShell)",
optional = true,
platform = "windows",
},
},
},
{
cmd_name = "Clipboard",
package = {
{
name = "gclip",
cmd = { "gclip" },
args = nil,
url = "[golang-design/clipboard](https://github.com/golang-design/clipboard)",
optional = true,
platform = "all",
},
},
},
{
cmd_name = "open",
package = {
name = "open",
cmd = { "open" },
args = nil,
url = "[docs](https://www.man7.org/linux/man-pages/man2/open.2.html)",
optional = true,
platform = "linux",
{
name = "open",
cmd = { "open" },
args = nil,
url = "[docs](https://www.man7.org/linux/man-pages/man2/open.2.html)",
optional = true,
platform = "linux",
},
},
},
{
cmd_name = "explorer",
package = {
name = "explorer",
cmd = { "explorer" },
args = nil,
url = "[docs](https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-windows-explorer/)",
optional = true,
platform = "windows",
{
name = "explorer",
cmd = { "explorer" },
args = nil,
url = "[docs](https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-windows-explorer/)",
optional = true,
platform = "windows",
},
},
},
}
Expand Down Expand Up @@ -167,6 +186,7 @@ end
---@return string|any
---@return boolean needed
local check_binary_installed = function(pkg)
print(vim.inspect(pkg))
local needed = check_platform_needed(pkg)
local cmd = pkg.cmd or { pkg.name }
for _, binary in ipairs(cmd) do
Expand Down
41 changes: 29 additions & 12 deletions lua/freeze-code/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ freeze_code.go_install_freeze = function(opts)
"github.com/charmbracelet/freeze@latest",
}
local stdio = { stdout = "", stderr = "" }
local job = nil

local function on_output(err, data)
if err then
Expand All @@ -161,9 +162,10 @@ freeze_code.go_install_freeze = function(opts)
freeze_code.setup(opts)
logger.warn("[freeze-code] go install github.com/charmbracelet/freeze@latest completed")
create_autocmds()
vim.fn.jobstop(job)
end),
}
vim.fn.jobstart(cmd_args, callbacks)
job = vim.fn.jobstart(cmd_args, callbacks)
end

---@class FreezeCode
Expand Down Expand Up @@ -201,11 +203,12 @@ freeze_code.agnostic_install_freeze = function(opts)
if vim.fn.filereadable(binary_path) == 1 then
local success = vim.loop.fs_unlink(binary_path)
if not success then
logger.err("[freeze-code] ERROR: `freeze` binary could not be removed!")
logger.err("[freeze-code.nvim] ERROR: `freeze` binary could not be removed!")
return
end
end
local stdio = { stdout = "", stderr = "" }
local job = nil

local function on_output(err, data)
if err then
Expand All @@ -223,13 +226,17 @@ freeze_code.agnostic_install_freeze = function(opts)
on_output(out)
end),
on_exit = vim.schedule_wrap(function()
logger.info_fmt("[freeze-code] extracting release with `%s`", table.concat(extract_command, " "))
logger.info_fmt("[freeze-code.nvim] extracting release with `%s`", table.concat(extract_command, " "))
vim.fn.system(extract_command)
if vim.v.shell_error ~= 0 then
logger.err("[freeze-code.nvim] ERROR: extracting release failed")
return
end
-- remove the archive after completion
if vim.fn.filereadable(output_filename) == 1 then
local success = vim.loop.fs_unlink(output_filename)
if not success then
logger.err("[freeze-code] ERROR: existing archive could not be removed")
logger.err("[freeze-code.nvim] ERROR: existing archive could not be removed")
return
end
end
Expand All @@ -240,13 +247,17 @@ freeze_code.agnostic_install_freeze = function(opts)
opts.install_path = install_path
freeze_code.setup(opts)
vim.loop.spawn("rm", { args = rm_command_args })
logger.warn_fmt("[freeze-code] `freeze` binary installed in installed in path=%s", freeze_code.config.freeze_path)
logger.warn_fmt(
"[freeze-code.nvim] `freeze` binary installed in installed in path=%s",
freeze_code.config.freeze_path
)
freeze_code.setup(opts)
create_autocmds()
vim.fn.jobstop(job)
end),
}
logger.info_fmt("[freeze-code] downloading release from `%s`", release_url)
vim.fn.jobstart(download_command, callbacks)
logger.info_fmt("[freeze-code.nvim] downloading release from `%s`", release_url)
job = vim.fn.jobstart(download_command, callbacks)
end

---@class FreezeCode
Expand All @@ -255,11 +266,11 @@ end
---@param opts FreezeCodeConfig
freeze_code.install_freeze = function(opts)
if commands.check_executable("go", vim.fn.exepath("go")) then
logger.warn("[freeze-code] go install github.com/charmbracelet/freeze@latest completed")
logger.warn("[freeze-code.nvim] go install github.com/charmbracelet/freeze@latest completed")
freeze_code.go_install_freeze(opts)
return
end
logger.info("[freeze-code] Installing info with `curl`")
logger.info("[freeze-code.nvim] Installing info with `curl`")
freeze_code.agnostic_install_freeze(opts)
end

Expand All @@ -270,7 +281,7 @@ end
---@param e_line? number: line to start range
freeze_code.freeze = function(s_line, e_line)
if not freeze_code.config._installed then
logger.warn("[freeze-code] `freeze` not installed")
logger.warn("[freeze-code.nvim] `freeze` not installed")
freeze_code.install_freeze(freeze_code.config)
return
end
Expand All @@ -284,8 +295,14 @@ freeze_code.freeze = function(s_line, e_line)
return
end

local lang = vim.api.nvim_buf_get_option(0, "filetype")
local file = vim.api.nvim_buf_get_name(0)
local lang = ""
if vim.fn.has("nvim-0.10") == 1 then
lang = vim.api.nvim_get_option_value("filetype", { buf = vim.api.nvim_get_current_buf() })
else
---@diagnostic disable-next-line: deprecated
lang = vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "filetype")
end
local file = vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
local conf = freeze_code.config.freeze_config.config
local dir = freeze_code.config.dir
local theme = freeze_code.config.freeze_config.theme
Expand Down
6 changes: 3 additions & 3 deletions lua/freeze-code/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ local M = {}
M.logger = require("freeze-code.utils.logger")

M.os = {
is_win = vim.api.nvim_call_function("has", { "win32" }) == 1,
is_macos = vim.api.nvim_call_function("has", { "macunix" }) == 1,
is_unix = vim.api.nvim_call_function("has", { "unix" }) == 1,
is_win = vim.loop.os_uname().version:match("Windows"),
is_macos = vim.loop.os_uname().version:match("Darwin"),
is_unix = vim.loop.os_uname().version:match("Linux"),
}

return M
Loading

0 comments on commit e924331

Please sign in to comment.