Created
November 20, 2025 22:10
-
-
Save Velrok/ff928c32b66aea9d409fbc943896aec2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| -- Shared function to create GitHub gists | |
| local function create_gist(opts, is_public) | |
| local lines | |
| local filetype = vim.bo.filetype | |
| -- Check if command was called with a range (visual selection) | |
| if opts.range > 0 then | |
| -- Get visual selection | |
| lines = vim.fn.getline(opts.line1, opts.line2) | |
| else | |
| -- Get entire buffer | |
| lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) | |
| end | |
| -- Create MD5 hash of content | |
| local content = table.concat(lines, "\n") | |
| local hash = vim.fn.system("echo -n " .. vim.fn.shellescape(content) .. " | md5"):gsub("%s+", "") | |
| -- Create temporary file with hash-based name and appropriate extension | |
| local extension = filetype ~= "" and "." .. filetype or "" | |
| local tmpfile = "/tmp/gist_" .. hash .. extension | |
| -- Write lines to temporary file | |
| vim.fn.writefile(lines, tmpfile) | |
| -- Create the gist with appropriate visibility | |
| local visibility_flag = is_public and "--public" or "--private" | |
| local cmd = "gh gist create " .. visibility_flag .. " " .. tmpfile | |
| vim.notify("Creating gist...", vim.log.levels.INFO) | |
| -- Run command and capture output | |
| vim.fn.jobstart(cmd, { | |
| on_stdout = function(_, data, _) | |
| if data then | |
| -- gh gist create outputs the URL on stdout | |
| for _, line in ipairs(data) do | |
| if line and line ~= "" and line:match("^https://") then | |
| vim.notify("Gist created: " .. line, vim.log.levels.INFO) | |
| -- Open the URL in default browser | |
| vim.fn.jobstart("open " .. line, { detach = true }) | |
| break | |
| end | |
| end | |
| end | |
| end, | |
| on_stderr = function(_, data, _) | |
| if data and #data > 0 then | |
| local err = table.concat(data, "\n") | |
| if err ~= "" then | |
| vim.notify("Gist error: " .. err, vim.log.levels.ERROR) | |
| end | |
| end | |
| end, | |
| on_exit = function(_, code, _) | |
| if code ~= 0 then | |
| vim.notify("Gist creation failed with exit code: " .. code, vim.log.levels.ERROR) | |
| end | |
| end, | |
| }) | |
| end | |
| -- Create a private GitHub gist from visual selection or entire file | |
| vim.api.nvim_create_user_command("GistCreate", function(opts) | |
| create_gist(opts, false) | |
| end, { range = true, desc = "Create a private GitHub gist from visual selection or entire file" }) | |
| -- Create a public GitHub gist from visual selection or entire file | |
| vim.api.nvim_create_user_command("GistCreatePublic", function(opts) | |
| create_gist(opts, true) | |
| end, { range = true, desc = "Create a public GitHub gist from visual selection or entire file" }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment