|
| 1 | +local M = {} |
| 2 | + |
| 3 | +--- Creates a Github gist with the specified filename and description |
| 4 | +-- |
| 5 | +-- @param filename string The filename of the Gist |
| 6 | +-- @param description string The description of the Gist |
| 7 | +-- @param private boolean Wether the Gist should be private |
| 8 | +-- @return string|nil The URL of the created Gist |
| 9 | +-- @return number|nil The error of the command |
| 10 | +function M.create_gist(filename, description, private) |
| 11 | + local public_flag = private and "" or "--public" |
| 12 | + local escaped_description = vim.fn.shellescape(description) |
| 13 | + |
| 14 | + local cmd = string.format( |
| 15 | + "gh gist create %s %s --filename %s -d %s", |
| 16 | + vim.fn.expand("%"), |
| 17 | + public_flag, |
| 18 | + filename, |
| 19 | + escaped_description |
| 20 | + ) |
| 21 | + |
| 22 | + local handle = io.popen(cmd) |
| 23 | + |
| 24 | + -- null check on handle |
| 25 | + if handle == nil then |
| 26 | + return nil |
| 27 | + end |
| 28 | + |
| 29 | + local output = handle:read("*a") |
| 30 | + handle:close() |
| 31 | + |
| 32 | + if vim.v.shell_error ~= 0 then |
| 33 | + return output, vim.v.shell_error |
| 34 | + end |
| 35 | + |
| 36 | + local url = string.gsub(output, "\n", "") |
| 37 | + |
| 38 | + return url, nil |
| 39 | +end |
| 40 | + |
| 41 | +--- Reads the configuration from the user's vimrc |
| 42 | +-- @treturn table A table with the configuration properties |
| 43 | +function M.read_config() |
| 44 | + local is_private = vim.api.nvim_get_var("gist_is_private") or false |
| 45 | + local clipboard = vim.api.nvim_get_var("gist_clipboard") or "+" |
| 46 | + |
| 47 | + local config = { |
| 48 | + is_private = is_private, |
| 49 | + clipboard = clipboard, |
| 50 | + } |
| 51 | + |
| 52 | + return config |
| 53 | +end |
| 54 | + |
| 55 | +return M |
0 commit comments