From 0c52a449b44bc754f5560425c7a735b78c14baac Mon Sep 17 00:00:00 2001 From: Morten Olsen Date: Sun, 7 Nov 2021 22:39:54 +0100 Subject: [PATCH] init --- lua/share/init.lua | 87 ++++++++++++++++++++++++++++++++++++++++++++++ plugin/main.vim | 11 ++++++ 2 files changed, 98 insertions(+) create mode 100644 lua/share/init.lua create mode 100644 plugin/main.vim diff --git a/lua/share/init.lua b/lua/share/init.lua new file mode 100644 index 0000000..a55b42c --- /dev/null +++ b/lua/share/init.lua @@ -0,0 +1,87 @@ +local pickers = require "telescope.pickers" +local finders = require "telescope.finders" +local actions = require "telescope.actions" +local action_state = require "telescope.actions.state" +local conf = require("telescope.config").values +local curl = require "plenary.curl" +local M = {} + +local providers = {} + +local function get_visual_selection() + local first = vim.api.nvim_buf_get_mark(0, "<") + local last = vim.api.nvim_buf_get_mark(0, ">") + local content = vim.api.nvim_buf_get_lines(0, first[1] - 1, last[1], 0) + return content +end + +M.providers = {} + +M.providers.slack = function(slack_token) + local token = slack_token + print(vim.inspect(token)) + local function post(receiver, content) + local body = { + text = "```\n" .. table.concat(content, "\n") .. "\n```", + channel = receiver.id, + as_user = true, + } + local res = curl.post("https://slack.com/api/chat.postMessage", { + body = vim.fn.json_encode(body), + headers = { + Authorization = "Bearer " .. token, + content_type = "application/json" + } + }) + end + + local function fetch_users() + local res = curl.get("https://slack.com/api/users.list", { + headers = { + Authorization = "Bearer " .. token + } + }) + local users = vim.fn.json_decode(res.body).members + return users + end + + return { + fetch_users = fetch_users, + post = post, + } +end + +M.config = function(opts) + providers = opts.providers or {} +end + +M.share = function(provider_name) + local content = get_visual_selection() + local provider = providers[provider_name] + local users = provider.fetch_users() + pickers.new(nil, { + prompt_title = "Users", + finder = finders.new_table { + results = users, + entry_maker = function(entry) + return { + value = entry, + display = entry.real_name or entry.name, + ordinal= entry.real_name or entry.name, + } + end + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + post_to(selection.value, content) + end) + return true + end, + }):find() + +end + +return M diff --git a/plugin/main.vim b/plugin/main.vim new file mode 100644 index 0000000..ba2f994 --- /dev/null +++ b/plugin/main.vim @@ -0,0 +1,11 @@ +" function! ReloadAlpha() +" lua << EOF +" for k in pairs(package.loaded) do +" if k:match("^share.nvim") then +" package.loaded[k] = nil +" end +" end +" EOF +" endfunction +" nnoremap pr :call ReloadAlpha() +" vnoremap ps :lua require("share").share()