Skip to content

Commit

Permalink
feat(charm-and-friends#11): add open action
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroSuero committed Sep 6, 2024
1 parent 6f0b252 commit 5ae23a6
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lua/freeze/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 5ae23a6

Please sign in to comment.