Created
July 28, 2024 22:49
-
-
Save meleu/338bb1babd2945bcf8dd01bc36ae012d to your computer and use it in GitHub Desktop.
Lua code for taking screenshots with freeze from inside neovim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- inspired by the code here: | |
-- https://www.justinrassier.com/blog/posts/2024-04-17-how-to-make-a-code-snapshot-plugin-in-neovim | |
vim.api.nvim_create_user_command("Freeze", function() | |
-- get file type from the buffer | |
local file_type = vim.bo.filetype | |
-- get the text from the visual selection as a table | |
local text = vim.fn.getline(vim.fn.getpos("'<")[2], vim.fn.getpos("'>")[2]) | |
-- join it together into one string | |
local full_text = table.concat(text, "\n") | |
-- write the file to /tmp/freeze...probably could find a better place to put this so it's | |
-- cross platform, but it works for me ¯\_(ツ)_/¯ | |
local file = io.open("/tmp/freeze", "w") | |
if file == nil then | |
print("could not open file") | |
return | |
end | |
file:write(full_text) | |
file:close() | |
-- call the freeze command with the file type we grabbed earlier | |
vim.fn.system("freeze /tmp/freeze --output /tmp/freeze.png --config user --language " .. file_type) | |
if vim.fn.filereadable("/tmp/freeze.png") then | |
vim.fn.system("open /tmp/freeze.png; rm -f /tmp/freeze{,png}") | |
vim.notify("Image generated", vim.log.levels.INFO) | |
else | |
vim.notify("Unable to generate the image", vim.log.levels.INFO) | |
end | |
end, { | |
-- make sure the command is only available in visual mode | |
range = true, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment