From 633af20799fb3beaf4841d02abe9efb2e90f7cef Mon Sep 17 00:00:00 2001 From: Morten Olsen Date: Mon, 8 Nov 2021 11:39:02 +0100 Subject: [PATCH] updates --- README.md | 33 ++++++++++++++++++++++ lua/share/init.lua | 28 ++++++++++++++++-- lua/share/providers/slack.lua | 53 +++++++++++++++++++++++++++++++++-- 3 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..1da3857 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# share.nvim + +## Setup + +```lua +use({ + "morten-olsen/share.nvim", + requires = { + {'nvim-lua/plenary.nvim'}, + {'nvim-telescope/telescope.nvim'} + }, + config = function() + local share = require("share") + local slack = require("share.providers.slack") + share.config({ + providers = { + my_slack = slack("your-slack-token") + } + }) + end +}) +``` + +## Usage + +```vim +" share as markdown +vnoremap ss :!lua require("share").share("my_slack") + +" share as code block +vnoremap ssm :!lua require("share").share("my_slack", { format: "code" }) + +``` diff --git a/lua/share/init.lua b/lua/share/init.lua index de8203e..014e917 100644 --- a/lua/share/init.lua +++ b/lua/share/init.lua @@ -1,6 +1,7 @@ local pickers = require "telescope.pickers" local finders = require "telescope.finders" local actions = require "telescope.actions" +local previewers = require "telescope.previewers" local action_state = require "telescope.actions.state" local conf = require("telescope.config").values @@ -24,8 +25,15 @@ end M.share = function(provider_name, opts) local content = get_visual_selection() + local type = vim.api.nvim_buf_get_option(0, "filetype") + if content == "" then + error("No content selected") + end local provider = providers[provider_name] - local users = provider.fetch_users(opts) + if provider == nil then + error("Provider " .. provider_name .. " not found") + end + local users = provider.fetch_recipients(opts) pickers.new(nil, { prompt_title = "Users", finder = finders.new_table { @@ -33,8 +41,8 @@ M.share = function(provider_name, opts) entry_maker = function(entry) return { value = entry, - display = entry.real_name or entry.name, - ordinal= entry.real_name or entry.name, + display = entry.name, + ordinal= entry.name, } end }, @@ -47,6 +55,20 @@ M.share = function(provider_name, opts) end) return true end, + previewer = previewers.new_buffer_previewer({ + title = "snippet", + define_preview = function(self, entry, status) + local lines = {} + for k in content:gmatch("([^\n]*)\n?") do + table.insert(lines, k) + end + vim.api.nvim_buf_set_option(self.state.bufnr, "filetype", type) + for row,display in pairs(lines) do + vim.api.nvim_buf_set_lines(self.state.bufnr, row, row + 1, false, { display }) + end + + end + }), }):find() end diff --git a/lua/share/providers/slack.lua b/lua/share/providers/slack.lua index 2683fb6..c44c439 100644 --- a/lua/share/providers/slack.lua +++ b/lua/share/providers/slack.lua @@ -1,10 +1,24 @@ local curl = require "plenary.curl" +local function merge_lists(t1, t2) + local result = {} + table.foreach(t1, function(_, v) table.insert(result, v) end) + table.foreach(t2, function(_, v) table.insert(result, v) end) + return result +end + +local function test_http_response(res) + local body = vim.fn.json_decode(res.body) + if body.error then + error("Slack error " .. body.error) + end +end + local M = function(slack_token) local token = slack_token local function post(receiver, content, opts) local text = content - if opts.format == "markdown" then + if opts.format == "code" then text = "```\n" .. text .. "```" end local body = { @@ -19,6 +33,27 @@ local M = function(slack_token) content_type = "application/json" } }) + test_http_response(res) + end + + local function fetch_channels() + local query = { + types = "public_channel,private_channel", + exclude_archived = "true", + limit = "1000", + } + local res = curl.get("https://slack.com/api/conversations.list", { + query = query, + headers = { + Authorization = "Bearer " .. token + } + }) + test_http_response(res) + local channels = vim.fn.json_decode(res.body).channels + table.foreach(channels, function(_, k) + k.name = "# " .. k.name + end) + return channels end local function fetch_users() @@ -27,12 +62,26 @@ local M = function(slack_token) Authorization = "Bearer " .. token } }) + test_http_response(res) local users = vim.fn.json_decode(res.body).members + table.foreach(users, function(_, k) + local name = k.real_name or k.name + if k.real_name then + name = name .. " (" .. k.name .. ")" + end + k.name = "@ " .. name + end) return users end + local function fetch_recipients() + local users = fetch_users() + local channels = fetch_channels() + return merge_lists(users, channels) + end + return { - fetch_users = fetch_users, + fetch_recipients = fetch_recipients, post = post, } end