mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 00:21:15 +01:00
Add: Pastebin
This commit is contained in:
@@ -40,6 +40,10 @@ A collection of custom keymaps and configurations for Neovim aimed at improved b
|
||||
- Paste from clipboard: `<leader>P` (at cursor, or over selection without yanking it)
|
||||
- Paste over selection without yanking replaced text: `<leader>p`
|
||||
|
||||
### Pastebin
|
||||
|
||||
- Use :Pastebin command to upload the filetext to a server (check lua/pastebin.lua)
|
||||
|
||||
### Diagnostics
|
||||
|
||||
- `[d` / `]d` — Navigate diagnostics
|
||||
|
||||
3
init.lua
3
init.lua
@@ -385,5 +385,8 @@ require('message_auto_clear').setup()
|
||||
-- When a file is deleted externally, rename all its buffers to "[file]: file removed"
|
||||
require 'buffer_deleted'
|
||||
|
||||
-- Pastebin
|
||||
require('pastebin').setup()
|
||||
|
||||
-- The line beneath this is called `modeline`. See `:help modeline`
|
||||
-- vim: ts=2 sts=2 sw=2 et
|
||||
|
||||
63
lua/pastebin.lua
Normal file
63
lua/pastebin.lua
Normal file
@@ -0,0 +1,63 @@
|
||||
local M = {}
|
||||
|
||||
M.upload_url = 'https://user:pass@upload.freedoms4.top/index.php'
|
||||
M.auth = 'user:pass'
|
||||
|
||||
-- Generate random filename
|
||||
local function random_name(len)
|
||||
local chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
||||
local name = {}
|
||||
math.randomseed(os.time() + vim.loop.hrtime())
|
||||
for i = 1, len do
|
||||
local idx = math.random(#chars)
|
||||
name[#name + 1] = chars:sub(idx, idx)
|
||||
end
|
||||
return table.concat(name)
|
||||
end
|
||||
|
||||
function M.upload(text)
|
||||
local fname = random_name(12) .. '.txt'
|
||||
local form_field = string.format('file=@-;filename=%s', fname)
|
||||
|
||||
local cmd = {
|
||||
'curl',
|
||||
'-s',
|
||||
'--user',
|
||||
M.auth,
|
||||
'--form',
|
||||
form_field,
|
||||
M.upload_url,
|
||||
}
|
||||
|
||||
local output = vim.fn.system(cmd, text)
|
||||
|
||||
if vim.v.shell_error ~= 0 then
|
||||
vim.notify('[Pastebin] Upload failed:\n' .. output, vim.log.levels.ERROR)
|
||||
return nil
|
||||
end
|
||||
|
||||
local url = output:match "https?://[%w%-%._~:/%?#%[%]@%%&+='*,]+"
|
||||
if not url then
|
||||
vim.notify('[Pastebin] No URL found.\nServer replied:\n' .. output, vim.log.levels.ERROR)
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Clipboard
|
||||
vim.fn.setreg('+', url)
|
||||
vim.fn.setreg('*', url)
|
||||
|
||||
vim.notify(string.format('[Pastebin]\nURL copied:\n%s', url), vim.log.levels.INFO)
|
||||
|
||||
return url
|
||||
end
|
||||
|
||||
function M.pastebin_cmd()
|
||||
local text = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), '\n')
|
||||
M.upload(text)
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
vim.api.nvim_create_user_command('Pastebin', M.pastebin_cmd, {})
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user