diff --git a/README.md b/README.md index c3d78db..92d3c77 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/bin/pngcopy-linux b/bin/pngcopy-linux deleted file mode 100755 index afe7dea..0000000 --- a/bin/pngcopy-linux +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -set -e - -xclip -selection clipboard -t image/png -i "$@" diff --git a/bin/pngcopy-macos b/bin/pngcopy-macos deleted file mode 100755 index 16a3e24..0000000 --- a/bin/pngcopy-macos +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash -set -e - -osascript \ - -e "on run args" \ - -e "set the clipboard to (read (POSIX file (first item of args)) as JPEG picture)" \ - -e end \ - "$@" diff --git a/bin/pngcopy-windows.ps1 b/bin/pngcopy-windows.ps1 deleted file mode 100644 index 308c514..0000000 --- a/bin/pngcopy-windows.ps1 +++ /dev/null @@ -1 +0,0 @@ -Add-Type -AssemblyName System.Windows.Forms; [Windows.Forms.Clipboard]::SetImage($[System.Drawing.Image]::FromFile("$args[0]"))) diff --git a/doc/freeze-code.nvim.txt b/doc/freeze-code.nvim.txt index fcf69ca..8383fc3 100644 --- a/doc/freeze-code.nvim.txt +++ b/doc/freeze-code.nvim.txt @@ -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* @@ -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 @@ -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 - + The default command will be gclip + 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. diff --git a/lua/freeze-code/commands.lua b/lua/freeze-code/commands.lua index 72a788e..68fa5c6 100644 --- a/lua/freeze-code/commands.lua +++ b/lua/freeze-code/commands.lua @@ -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 = {} @@ -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 @@ -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 diff --git a/lua/freeze-code/health.lua b/lua/freeze-code/health.lua index 111bf4b..afd5a30 100644 --- a/lua/freeze-code/health.lua +++ b/lua/freeze-code/health.lua @@ -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", + }, }, }, } @@ -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 diff --git a/lua/freeze-code/init.lua b/lua/freeze-code/init.lua index feb5d57..321bb5a 100644 --- a/lua/freeze-code/init.lua +++ b/lua/freeze-code/init.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lua/freeze-code/utils/init.lua b/lua/freeze-code/utils/init.lua index 480c228..07a63ca 100644 --- a/lua/freeze-code/utils/init.lua +++ b/lua/freeze-code/utils/init.lua @@ -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 diff --git a/tests/freeze-code/freeze-code_spec.lua b/tests/freeze-code/freeze-code_spec.lua index 3ed48f6..d4cea0c 100644 --- a/tests/freeze-code/freeze-code_spec.lua +++ b/tests/freeze-code/freeze-code_spec.lua @@ -25,9 +25,15 @@ local function create_buffer() buf = api.nvim_create_buf(false, true) win = api.nvim_open_win(buf, true, win_opts) - api.nvim_win_set_option(win, "winblend", 0) - api.nvim_buf_set_option(buf, "bufhidden", "wipe") - api.nvim_buf_set_option(buf, "filetype", "typescript") + if vim.fn.has("nvim-0.10") == 1 then + api.nvim_set_option_value("winblend", 0, { scope = "local" }) + api.nvim_set_option_value("bufhidden", "wipe", { scope = "local" }) + api.nvim_set_option_value("filetype", "typescript", { scope = "local" }) + else + api.nvim_win_set_option(win, "winblend", 0) + api.nvim_buf_set_option(buf, "bufhidden", "wipe") + api.nvim_buf_set_option(buf, "filetype", "typescript") + end api.nvim_buf_set_name(buf, "testing.ts") return buf