From 5ae23a600b12fca377a0834e66a86fd9419731b3 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Fri, 6 Sep 2024 14:14:12 +0200 Subject: [PATCH 1/5] feat(#11): add `open` action --- lua/freeze/init.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lua/freeze/init.lua b/lua/freeze/init.lua index 6e2f057..70c415a 100644 --- a/lua/freeze/init.lua +++ b/lua/freeze/init.lua @@ -17,6 +17,7 @@ M.required_options = { -- We allow the user to provide custom options, if none are provided the command will use its default args M.allowed_opts = { command = "string", + open = "boolean", config = "string", output = { "string", "function" }, window = "boolean", @@ -81,6 +82,29 @@ M.parse_options = function(opts) return options end +-- Open the generated image based on the OS +---@param args FreezeOptions +---@return string +local function open_by_os(args) + local is_win = vim.loop.os_uname().sysname:match("Windows") + local output = vim.fn.expand(args.output) + local cmd = {} + if is_win then + table.insert(cmd, "explorer") + else + -- Maybe we should use xdg-open here too? + table.insert(cmd, "open") + end + table.insert(cmd, output) + return vim.fn.system(cmd) +end + +-- Open the generated image +---@param args FreezeOptions +M.open = function(args) + open_by_os(args) +end + ---@param cmd table ---@param args table ---@param tbl table @@ -193,10 +217,21 @@ M.setup = function(opts) vim.api.nvim_create_user_command("Freeze", function(args) M.start(args, options) + -- If the user wants to open the file, open it + if args.args == "open" then + if not options.output then + options.output = vim.fn.expand("freeze.png") + end + M.open(options) + end end, { desc = "convert range to code image representation", force = false, range = true, + nargs = "*", + complete = function() + return { "open" } + end, }) end From 06a9962798ccee9f6ec9e0d896c8ad0f9cf65696 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Fri, 6 Sep 2024 14:29:05 +0200 Subject: [PATCH 2/5] feat: implement `open` as config option --- lua/freeze/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/freeze/init.lua b/lua/freeze/init.lua index f00ce0e..3bbb38c 100644 --- a/lua/freeze/init.lua +++ b/lua/freeze/init.lua @@ -119,7 +119,7 @@ local function populate_cmd(cmd, args, tbl, prefix) elseif type(v) == "table" and not is_array(v) then populate_cmd(cmd, args, v, prefix .. k .. ".") -- Handle anything that is not the command or language option - elseif k ~= "command" and k ~= "language" then + elseif k ~= "command" and k ~= "language" and k ~= "open" then table.insert(cmd, "--" .. prefix .. string.gsub(k, "_", "-")) -- If the value is a function, call it with the args, otherwise just use the value @@ -209,7 +209,7 @@ M.setup = function(opts) vim.api.nvim_create_user_command("Freeze", function(args) M.start(args, options) -- If the user wants to open the file, open it - if args.args == "open" then + if args.args == "open" or options.open then if not options.output then options.output = vim.fn.expand("freeze.png") end From 8b87f68a428462465024eb473cd589b633b331f5 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Fri, 6 Sep 2024 14:29:34 +0200 Subject: [PATCH 3/5] feat(types): add `open` and minor fix to `setup` fun --- lua/freeze/types.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/freeze/types.lua b/lua/freeze/types.lua index 8f4b85f..47482d4 100644 --- a/lua/freeze/types.lua +++ b/lua/freeze/types.lua @@ -4,8 +4,9 @@ ---@class FreezeOptions ---@field command OptionsType +---@field open OptionsType ---@field config OptionsType ----@field output table +---@field output table|OptionsType ---@field window OptionsType ---@field padding table ---@field margin table @@ -25,4 +26,4 @@ ---@field get_arguments fun(args: table, options: FreezeOptions): table ---@field format_lines fun(cmdline: table, args: table): table, table ---@field start fun(args: table, options: FreezeOptions) ----@field setup fun(opts: FreezeOptions) +---@field setup fun(opts: table) From a814dca92c97b2e66af70ed7e297ec7e67596ecd Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Fri, 6 Sep 2024 14:30:00 +0200 Subject: [PATCH 4/5] docs(README): update information with `open` action --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index ded5160..365bb7b 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ config = function() require('freeze').setup({ command = "freeze", + open = true, -- Open the generated image after running the command output = function() return "./" .. os.date("%Y-%m-%d") .. "_freeze.png" end, @@ -35,6 +36,12 @@ You might also want to add a keybinding to call the `Freeze` command. vim.api.nvim_set_keymap('v', 'sc', 'Freeze', {}) ``` +You can also use the `Freeze` command to open the generated image. + +```lua +vim.api.nvim_set_keymap('n', 'so', 'Freeze open', {}) +``` + ### Thanks This project is heavily inspired by [nvim-silicon](https://github.com/michaelrommel/nvim-silicon). From 6214bd8d2187a72a52603ed7c257ff77e09d02db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?AoMe=20=C2=B7=20=E9=9D=92=E7=9B=AE?= <71392160+AlejandroSuero@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:16:31 +0200 Subject: [PATCH 5/5] feat: add `xdg-open` to support linux --- lua/freeze/init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/freeze/init.lua b/lua/freeze/init.lua index 1f04367..c33d192 100644 --- a/lua/freeze/init.lua +++ b/lua/freeze/init.lua @@ -88,12 +88,14 @@ end ---@return string local function open_by_os(args) local is_win = vim.loop.os_uname().sysname:match("Windows") + local is_linux = vim.loop.os_uname().sysname:match("Linux") local output = vim.fn.expand(args.output) local cmd = {} if is_win then table.insert(cmd, "explorer") + elseif is_linux then + table.insert(cmd, "xdg-open") else - -- Maybe we should use xdg-open here too? table.insert(cmd, "open") end table.insert(cmd, output)