|
| 1 | +local M = {} |
| 2 | + |
| 3 | +local function is_visual() |
| 4 | + if vim.fn.mode() ~= "v" and vim.fn.mode() ~= "V" and vim.fn.mode() ~= "<C-V>" then |
| 5 | + return false |
| 6 | + end |
| 7 | + |
| 8 | + return true |
| 9 | +end |
| 10 | + |
| 11 | +function M.get_current_selection(start_line, end_line) |
| 12 | + local bufnr = vim.api.nvim_get_current_buf() |
| 13 | + |
| 14 | + start_line = start_line - 1 -- Convert to 0-based line number |
| 15 | + end_line = end_line - 1 -- Convert to 0-based line number |
| 16 | + |
| 17 | + local lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line + 1, false) |
| 18 | + |
| 19 | + return table.concat(lines, "\n") |
| 20 | +end |
| 21 | + |
| 22 | +function M.get_last_selection() |
| 23 | + local bufnr = vim.api.nvim_get_current_buf() |
| 24 | + |
| 25 | + -- Save the current cursor position |
| 26 | + local saved_cursor = vim.api.nvim_win_get_cursor(0) |
| 27 | + |
| 28 | + -- Get the start and end positions of the visual selection |
| 29 | + vim.cmd("normal! gv") |
| 30 | + local start_pos = vim.fn.getpos("'<") |
| 31 | + local end_pos = vim.fn.getpos("'>") |
| 32 | + |
| 33 | + -- Restore the cursor position |
| 34 | + vim.api.nvim_win_set_cursor(0, saved_cursor) |
| 35 | + |
| 36 | + local start_line = start_pos[2] - 1 |
| 37 | + local end_line = end_pos[2] |
| 38 | + local content_lines = vim.api.nvim_buf_get_lines(bufnr, start_line, end_line, false) |
| 39 | + local content = table.concat(content_lines, "\n") |
| 40 | + |
| 41 | + return content |
| 42 | +end |
| 43 | + |
| 44 | +local function read_file(path) |
| 45 | + local file = io.open(path, "rb") -- r read mode and b binary mode |
| 46 | + if not file then |
| 47 | + return nil |
| 48 | + end |
| 49 | + local content = file:read("*a") -- *a or *all reads the whole file |
| 50 | + file:close() |
| 51 | + return content |
| 52 | +end |
| 53 | + |
| 54 | +function M.exec(cmd, stdin) |
| 55 | + print(string.format("Executing: %s", cmd)) |
| 56 | + local tmp = os.tmpname() |
| 57 | + |
| 58 | + local pipe = io.popen(cmd .. "> " .. tmp, "w") |
| 59 | + |
| 60 | + if not pipe then |
| 61 | + return nil |
| 62 | + end |
| 63 | + |
| 64 | + if stdin then |
| 65 | + pipe:write(stdin) |
| 66 | + end |
| 67 | + |
| 68 | + pipe:close() |
| 69 | + |
| 70 | + local output = read_file(tmp) |
| 71 | + os.remove(tmp) |
| 72 | + |
| 73 | + return output |
| 74 | +end |
| 75 | + |
| 76 | +function M.extract_gist_url(output) |
| 77 | + local pattern = "https://gist.github.com/%S+" |
| 78 | + |
| 79 | + return output:match(pattern) |
| 80 | +end |
| 81 | + |
| 82 | +-- @param args string |
| 83 | +function M.parseArgs(args) |
| 84 | + -- parse args as key=value |
| 85 | + local parsed = {} |
| 86 | + |
| 87 | + for _, arg in ipairs(vim.split(args, " ", {})) do |
| 88 | + local key, value = unpack(vim.split(arg, "=", { plain = true })) |
| 89 | + |
| 90 | + if value == "true" then |
| 91 | + value = true |
| 92 | + elseif value == "false" then |
| 93 | + value = false |
| 94 | + end |
| 95 | + |
| 96 | + parsed[key] = value |
| 97 | + end |
| 98 | + |
| 99 | + return parsed |
| 100 | +end |
| 101 | + |
| 102 | +return M |
0 commit comments