Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 9b46900

Browse files
authored
Update init.lua
html_colors
1 parent 713a438 commit 9b46900

File tree

1 file changed

+153
-150
lines changed

1 file changed

+153
-150
lines changed

lua/icon-picker/init.lua

+153-150
Original file line numberDiff line numberDiff line change
@@ -1,210 +1,213 @@
1+
local ICON_TYPE = { "alt_font", "emoji", "html_colors", "nerd_font", "symbols", }
2+
13
local M = {}
24

35
local icon_type_data = { --{{{
4-
["alt_font"] = {
5-
icons = require("icon-picker.icons.alt-fonts"),
6-
spaces = 1,
7-
},
8-
["emoji"] = {
9-
icons = require("icon-picker.icons.emoji-list"),
10-
spaces = 1,
11-
},
12-
["nerd_font"] = {
13-
icons = require("icon-picker.icons.nf-icon-list"),
14-
spaces = 2,
15-
},
16-
["symbols"] = {
17-
icons = require("icon-picker.icons.symbol-list"),
18-
spaces = 2,
19-
},
6+
["alt_font"] = {
7+
icons = require("icon-picker.icons.alt-fonts"),
8+
spaces = 1,
9+
},
10+
["emoji"] = {
11+
icons = require("icon-picker.icons.emoji-list"),
12+
spaces = 1,
13+
},
14+
["nerd_font"] = {
15+
icons = require("icon-picker.icons.nf-icon-list"),
16+
spaces = 2,
17+
},
18+
["symbols"] = {
19+
icons = require("icon-picker.icons.symbol-list"),
20+
spaces = 2,
21+
},
22+
["html_colors"] = {
23+
icons = require("icon-picker.icons.html-colors"),
24+
spaces = 1,
25+
},
2026
} --}}}
2127

2228
local list_types = { --{{{
23-
["PickAltFont"] = {
24-
icon_types = { "alt_font" },
25-
desc = "Pick an Alt Font Character",
26-
},
27-
["PickAltFontAndSymbols"] = {
28-
icon_types = { "symbols", "alt_font" },
29-
desc = "Pick Alt Font Characters & Symbols",
30-
},
31-
["PickEmoji"] = {
32-
icon_types = { "emoji" },
33-
desc = "Pick an emoji",
34-
},
35-
["PickIcons"] = {
36-
icon_types = { "emoji", "nerd_font" },
37-
desc = "Pick an Emoji & Nerd Font Icons",
38-
},
39-
["PickEverything"] = {
40-
icon_types = { "emoji", "nerd_font", "alt_font", "symbols" },
41-
desc = "Pick an icon",
42-
},
43-
["PickNerd"] = {
44-
icon_types = { "nerd_font" },
45-
desc = "Pick a Nerd Font",
46-
},
47-
["PickSymbols"] = {
48-
icon_types = { "symbols" },
49-
desc = "Pick a Symbol",
50-
},
29+
["PickAltFont"] = {
30+
icon_types = { "alt_font" },
31+
desc = "Pick an Alt Font Character",
32+
},
33+
["PickAltFontAndSymbols"] = {
34+
icon_types = { "alt_font", "symbols" },
35+
desc = "Pick Alt Font Characters & Symbols",
36+
},
37+
["PickEmoji"] = {
38+
icon_types = { "emoji" },
39+
desc = "Pick an emoji",
40+
},
41+
["PickIcons"] = {
42+
icon_types = { "emoji", "nerd_font" },
43+
desc = "Pick an Emoji & Nerd Font Icons",
44+
},
45+
["PickEverything"] = {
46+
icon_types = ICON_TYPE,
47+
desc = "Pick an icon",
48+
},
49+
["PickNerd"] = {
50+
icon_types = { "nerd_font" },
51+
desc = "Pick a Nerd Font",
52+
},
53+
["PickSymbols"] = {
54+
icon_types = { "symbols" },
55+
desc = "Pick a Symbol",
56+
},
57+
["PickHTMLColors"] = {
58+
icon_types = { "html_colors" },
59+
desc = "Pick an hexa HTML Color",
60+
},
5161
} --}}}
5262

5363
-- vim.ui.select functionality {{{
5464
local function insert_user_choice_normal(choice)
55-
if choice then
56-
local split = vim.split(choice, " ")
65+
if choice then
66+
local split = vim.split(choice, " ")
5767

58-
vim.api.nvim_put({ split[1] }, "", false, true)
59-
end
68+
vim.api.nvim_put({ split[1] }, "", false, true)
69+
end
6070
end
6171

6272
local function insert_user_choice_insert(choice)
63-
if choice then
64-
local split = vim.split(choice, " ")
73+
if choice then
74+
local split = vim.split(choice, " ")
6575

66-
local current_line = vim.api.nvim_get_current_line()
67-
local cursor_col = vim.api.nvim_win_get_cursor(0)[2]
76+
local current_line = vim.api.nvim_get_current_line()
77+
local cursor_col = vim.api.nvim_win_get_cursor(0)[2]
6878

69-
if cursor_col + 1 >= #current_line then
70-
vim.api.nvim_put({ split[1] }, "", true, true)
71-
else
72-
vim.api.nvim_put({ split[1] }, "", false, false)
73-
end
79+
if cursor_col + 1 >= #current_line then
80+
vim.api.nvim_put({ split[1] }, "", true, true)
81+
else
82+
vim.api.nvim_put({ split[1] }, "", false, false)
83+
end
7484

75-
vim.api.nvim_feedkeys("a", "t", false)
76-
end
85+
vim.api.nvim_feedkeys("a", "t", false)
86+
end
7787
end
7888

7989
local function yank_user_choice_normal(choice)
80-
if choice then
81-
local split = vim.split(choice, " ")
90+
if choice then
91+
local split = vim.split(choice, " ")
8292

83-
vim.schedule(function()
84-
vim.cmd("let @+='" .. split[1] .. "'")
85-
end)
86-
end
93+
vim.schedule(function()
94+
vim.cmd("let @+='" .. split[1] .. "'")
95+
end)
96+
end
8797
end
8898

8999
local function custom_ui_select(items, prompt, callback)
90-
vim.ui.select(items, {
91-
prompt = prompt,
92-
}, callback)
93-
end
94-
95-
--}}}
100+
vim.ui.select(items, {
101+
prompt = prompt,
102+
}, callback)
103+
end --}}}
96104

97105
-- list functionality {{{
98106
--- insert a key val pair into a list with an arbitrary amount of spaces
99107
-- @param map: hash map of pairs to insert into list
100108
-- @param num_spaces: number of spaces to insert between key and val
101109
local function push_map(map, num_spaces, cur_list)
102-
local spaces = string.rep(" ", num_spaces)
110+
local spaces = string.rep(" ", num_spaces)
103111

104-
for key, val in pairs(map) do
105-
table.insert(cur_list, table.concat({ val, key }, spaces))
106-
end
112+
for key, val in pairs(map) do
113+
table.insert(cur_list, table.concat({ val, key }, spaces))
114+
end
107115

108-
return cur_list
109-
end
116+
return cur_list
117+
end -- }}}
110118

111-
--- create list of icons and descriptions for UI and call UI with list
119+
--- create list of icons and descriptions for UI and call UI with list {{{
112120
-- @param keys: array table of icon list key names
113121
-- @param desc: description of picker functionality, e.g. "Pick an emoji"
114122
-- @param callback: user input callback function
115123
local function generate_list(keys, desc, callback)
116-
local cur_tbl
117-
local item_list = {}
124+
local cur_tbl
125+
local item_list = {}
118126

119-
for _, type_key in ipairs(keys) do
120-
cur_tbl = icon_type_data[type_key]
121-
item_list = push_map(cur_tbl["icons"], cur_tbl["spaces"], item_list)
122-
end
127+
for _, type_key in ipairs(keys) do
128+
cur_tbl = icon_type_data[type_key]
129+
item_list = push_map(cur_tbl["icons"], cur_tbl["spaces"], item_list)
130+
end
123131

124-
custom_ui_select(item_list, desc, callback)
132+
custom_ui_select(item_list, desc, callback)
125133
end -- }}}
126134

127135
-- New API --{{{
128136
function Split(s, delimiter) -- from code grep
129-
local result = {}
130-
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
131-
if #match > 0 then
132-
table.insert(result, match)
133-
end
134-
end
135-
return result
137+
local result = {}
138+
for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
139+
if #match > 0 then
140+
table.insert(result, match)
141+
end
142+
end
143+
return result
136144
end
137145

138-
local new_API_table = { -- need better variable name
139-
["Normal"] = insert_user_choice_normal,
140-
["Insert"] = insert_user_choice_insert,
141-
["Yank"] = yank_user_choice_normal,
146+
local new_API_table = {
147+
-- need better variable name
148+
["Normal"] = insert_user_choice_normal,
149+
["Insert"] = insert_user_choice_insert,
150+
["Yank"] = yank_user_choice_normal,
142151
}
143152

144153
-- loops through the table & create user commands
145154
for command, callback in pairs(new_API_table) do
146-
vim.api.nvim_create_user_command("IconPicker" .. command, function(opts)
147-
local args = Split(opts.args, " ") -- split command arguments
148-
local desc = "Pick"
149-
150-
if #args == 0 then
151-
args = { "emoji", "nerd_font", "alt_font", "symbols" }
152-
end
153-
154-
local item_list = {}
155-
for _, argument in ipairs(args) do
156-
local cur_tbl = icon_type_data[argument]
157-
if cur_tbl == nil then
158-
return
159-
end
160-
161-
-- push icon results into item_list
162-
item_list = push_map(cur_tbl["icons"], cur_tbl["spaces"], item_list)
163-
desc = desc .. " " .. argument
164-
end
165-
166-
custom_ui_select(item_list, desc, callback)
167-
end, {
168-
nargs = "?",
169-
complete = function()
170-
return {
171-
"alt_font",
172-
"nerd_font",
173-
"emoji",
174-
"symbols",
175-
}
176-
end,
177-
})
178-
end
179-
--}}}
155+
vim.api.nvim_create_user_command("IconPicker" .. command, function(opts)
156+
local args = Split(opts.args, " ") -- split command arguments
157+
local desc = "Pick"
158+
159+
if #args == 0 then
160+
args = ICON_TYPE
161+
end
162+
163+
local item_list = {}
164+
for _, argument in ipairs(args) do
165+
local cur_tbl = icon_type_data[argument]
166+
if cur_tbl == nil then
167+
return
168+
end
169+
170+
-- push icon results into item_list
171+
item_list = push_map(cur_tbl["icons"], cur_tbl["spaces"], item_list)
172+
desc = desc .. " " .. argument
173+
end
174+
175+
custom_ui_select(item_list, desc, callback)
176+
end, {
177+
nargs = "?",
178+
complete = function()
179+
return ICON_TYPE
180+
end,
181+
})
182+
end --}}}
180183

181184
-- init all commands (legacy)
182185
for type, data in pairs(list_types) do
183-
vim.api.nvim_create_user_command(type, function()
184-
generate_list(data["icon_types"], data["desc"], insert_user_choice_normal)
185-
end, {})
186+
vim.api.nvim_create_user_command(type, function()
187+
generate_list(data["icon_types"], data["desc"], insert_user_choice_normal)
188+
end, {})
186189

187-
vim.api.nvim_create_user_command(type .. "Yank", function()
188-
generate_list(data["icon_types"], data["desc"], yank_user_choice_normal)
189-
end, {})
190+
vim.api.nvim_create_user_command(type .. "Yank", function()
191+
generate_list(data["icon_types"], data["desc"], yank_user_choice_normal)
192+
end, {})
190193

191-
vim.api.nvim_create_user_command(type .. "Insert", function()
192-
generate_list(data["icon_types"], data["desc"], insert_user_choice_insert)
193-
end, {})
194+
vim.api.nvim_create_user_command(type .. "Insert", function()
195+
generate_list(data["icon_types"], data["desc"], insert_user_choice_insert)
196+
end, {})
194197
end
195198

196199
M.setup = function(opts) --{{{
197-
if opts.disable_legacy_commands then
198-
for type, data in pairs(list_types) do
199-
local status_ok, _ = pcall(vim.api.nvim_del_user_command, type)
200-
if not status_ok then
201-
return
202-
end
203-
204-
vim.api.nvim_del_user_command(type .. "Insert")
205-
vim.api.nvim_del_user_command(type .. "Yank")
206-
end
207-
end
200+
if opts.disable_legacy_commands then
201+
for type, data in pairs(list_types) do
202+
local status_ok, _ = pcall(vim.api.nvim_del_user_command, type)
203+
if not status_ok then
204+
return
205+
end
206+
207+
vim.api.nvim_del_user_command(type .. "Insert")
208+
vim.api.nvim_del_user_command(type .. "Yank")
209+
end
210+
end
208211
end --}}}
209212

210213
return M

0 commit comments

Comments
 (0)