Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#11): add open action #14

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,6 +36,12 @@ You might also want to add a keybinding to call the `Freeze` command.
vim.api.nvim_set_keymap('v', '<leader>sc', '<cmd>Freeze<cr>', {})
```

You can also use the `Freeze` command to open the generated image.

```lua
vim.api.nvim_set_keymap('n', '<leader>so', '<cmd>Freeze open<cr>', {})
```

### Thanks

This project is heavily inspired by [nvim-silicon](https://github.com/michaelrommel/nvim-silicon).
Expand Down
37 changes: 36 additions & 1 deletion lua/freeze/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -82,6 +83,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
isabelroses marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -95,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
Expand Down Expand Up @@ -184,10 +208,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" 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,
range = true,
nargs = "*",
complete = function()
return { "open" }
end,
})
end

Expand Down
5 changes: 3 additions & 2 deletions lua/freeze/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

---@class FreezeOptions
---@field command OptionsType
---@field open OptionsType
---@field config OptionsType
---@field output table<OptionsType>
---@field output table<OptionsType>|OptionsType
---@field window OptionsType
---@field padding table<OptionsType>
---@field margin table<OptionsType>
Expand All @@ -25,4 +26,4 @@
---@field get_arguments fun(args: table<string, number>, options: FreezeOptions): table<string>
---@field format_lines fun(cmdline: table<string>, args: table<string, number>): table<string>, table<string>
---@field start fun(args: table<string, number>, options: FreezeOptions)
---@field setup fun(opts: FreezeOptions)
---@field setup fun(opts: table<FreezeOptions>)