Created
March 5, 2024 16:47
-
-
Save dharmx/0459c475621f8c51dcddebacab9f97d3 to your computer and use it in GitHub Desktop.
Kaomoji Picker!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- put this in ~/nvim/lua/telescope/_extensions/kao.lua | |
-- then call require("telescope").load_extension("kao") after configuring telescope | |
local actions = require("telescope.actions") | |
local pickers = require("telescope.pickers") | |
local finders = require("telescope.finders") | |
local config = require("telescope.config") | |
local entry_display = require("telescope.pickers.entry_display") | |
local make_entry = require("telescope.make_entry") | |
local actions_state = require("telescope.actions.state") | |
local curl = require("plenary.curl") | |
local Path = require("plenary.path") | |
local db = Path:new(vim.fn.stdpath("state") .. "/kao.db.json") | |
local results = nil | |
if not db:exists() then-- {{{ | |
curl.get("https://raw.githubusercontent.com/towzeur/Japanese-Emoticons-json/master/Japanese_Emoticons.json", { | |
accept = "application/json", | |
callback = function(response) | |
local parsed = vim.json.decode(response.body) | |
results = {} | |
---@diagnostic disable-next-line: param-type-mismatch | |
for annotation, emojis in pairs(parsed) do | |
for _, emoji in ipairs(emojis) do | |
table.insert(results, { annotation = annotation, emoji = emoji }) | |
end | |
end | |
db:write(vim.json.encode(results), "w") | |
end, | |
}) | |
end-- }}} | |
local function gen_from_kao(options) | |
local displayer = entry_display.create({ | |
separator = " ", | |
items = { | |
{ width = 20 }, | |
{ remaining = true }, | |
}, | |
}) | |
return function(entry) | |
return make_entry.set_default_entry_mt({ | |
value = entry, | |
ordinal = entry.annotation .. ":" .. entry.emoji, | |
display = function(entry) | |
return displayer({ | |
{ entry.value.annotation, "Question" }, | |
{ entry.value.emoji, "@text" }, | |
}) | |
end, | |
}, options) | |
end | |
end | |
return require("telescope").register_extension({ | |
exports = { | |
kao = function(options) | |
options = vim.F.if_nil(options, {}) | |
options.previewer = false | |
options.layout_strategy = vim.F.if_nil(options.layout_strategy, "cursor") | |
options.layout_config = vim.F.if_nil(options.layout_config, { | |
width = 0.3, | |
height = 0.5, | |
prompt_position = "top", | |
}) | |
-- inspo: discord | |
results = vim.F.if_nil(vim.json.decode(db:read()), results) | |
if not results then return end | |
options.prompt_prefix = vim.F.if_nil(options.prompt_prefix, (function() | |
return results[math.random(1, #results)].emoji .. " " | |
end)()) | |
pickers.new(options, { | |
finder = finders.new_table({ results = results, entry_maker = gen_from_kao(options) }), | |
sorter = config.values.generic_sorter(options), | |
attach_mappings = function(buffer) | |
actions.select_default:replace(function() | |
local current = actions_state.get_selected_entry() | |
vim.schedule(function() | |
vim.api.nvim_put({ current.value.emoji }, "c", false, true) | |
end) | |
actions.close(buffer) | |
end) | |
return true | |
end, | |
}):find() | |
end, | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo-.2.mp4