Skip to content

Commit

Permalink
Merge pull request #14 from AlejandroSuero/feature/open-command
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses authored Sep 6, 2024
2 parents 3b28674 + 6214bd8 commit cc8f094
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
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
35 changes: 34 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,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
Expand All @@ -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
Expand Down Expand Up @@ -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,
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>)

0 comments on commit cc8f094

Please sign in to comment.