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). diff --git a/lua/freeze/init.lua b/lua/freeze/init.lua index 17f981f..c33d192 100644 --- a/lua/freeze/init.lua +++ b/lua/freeze/init.lua @@ -18,6 +18,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", @@ -82,6 +83,31 @@ 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 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 + 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 + -- Populate the command line arguments local function populate_cmd(cmd, args, tbl, prefix) for k, v in pairs(tbl) do @@ -95,7 +121,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 @@ -195,6 +221,13 @@ 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" or options.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, 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)