diff --git a/lua/share/init.lua b/lua/share/init.lua index a55b42c..ce346fe 100644 --- a/lua/share/init.lua +++ b/lua/share/init.lua @@ -3,9 +3,8 @@ 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 M = {} local providers = {} local function get_visual_selection() @@ -15,50 +14,14 @@ local function get_visual_selection() 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) +M.share = function(provider_name, opts) local content = get_visual_selection() local provider = providers[provider_name] - local users = provider.fetch_users() + local users = provider.fetch_users(opts) pickers.new(nil, { prompt_title = "Users", finder = finders.new_table { @@ -76,7 +39,7 @@ M.share = function(provider_name) actions.select_default:replace(function() actions.close(prompt_bufnr) local selection = action_state.get_selected_entry() - post_to(selection.value, content) + provider.post(selection.value, table.concat(content, "\n"), opts) end) return true end, diff --git a/lua/share/providers/slack.lua b/lua/share/providers/slack.lua new file mode 100644 index 0000000..73302cc --- /dev/null +++ b/lua/share/providers/slack.lua @@ -0,0 +1,40 @@ +local curl = require "plenary.curl" + +local M = function(slack_token) + local token = slack_token + local function post(receiver, content, opts) + local text = content + if opts.format = "markdown" then + text = "```\n" .. text .. "```" + end + local body = { + text = text, + 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 + +return M