2024-07-12 00:31:36 +08:00
|
|
|
--=============================================================================
|
|
|
|
-- cpicker.lua
|
|
|
|
-- Copyright (c) 2019-2024 Wang Shidong & Contributors
|
|
|
|
-- Author: Wang Shidong < wsdjeg@outlook.com >
|
|
|
|
-- URL: https://spacevim.org
|
|
|
|
-- License: GPLv3
|
|
|
|
--=============================================================================
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
local winid
|
|
|
|
local bufnr
|
2024-07-13 18:52:07 +08:00
|
|
|
local color_hi = '#000000'
|
2024-07-12 00:31:36 +08:00
|
|
|
|
|
|
|
local hi = require('spacevim.api.vim.highlight')
|
2024-07-12 08:11:09 +08:00
|
|
|
local notify = require('spacevim.api.notify')
|
2024-07-13 18:52:07 +08:00
|
|
|
local log = require('spacevim.logger').derive('cpicker')
|
|
|
|
local util = require('cpicker.util')
|
2024-07-12 00:31:36 +08:00
|
|
|
|
|
|
|
local enabled_formats = {}
|
|
|
|
local increase_keys = {}
|
|
|
|
local reduce_keys = {}
|
|
|
|
|
2024-07-12 08:11:09 +08:00
|
|
|
local function update_buf_text()
|
2024-07-12 00:31:36 +08:00
|
|
|
local rst = {}
|
2024-07-13 18:52:07 +08:00
|
|
|
for _, format in ipairs(enabled_formats) do
|
|
|
|
local ok, f = pcall(require, 'cpicker.formats.' .. format)
|
|
|
|
if ok then
|
|
|
|
local funcs = f.increase_reduce_functions()
|
|
|
|
for i, text in ipairs(f.buf_text()) do
|
|
|
|
table.insert(rst, text)
|
|
|
|
increase_keys[#rst] = funcs[i][1]
|
|
|
|
reduce_keys[#rst] = funcs[i][2]
|
|
|
|
end
|
|
|
|
end
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
|
|
|
table.insert(rst, '')
|
2024-07-13 18:52:07 +08:00
|
|
|
for _, format in ipairs(enabled_formats) do
|
|
|
|
local ok, f = pcall(require, 'cpicker.formats.' .. format)
|
|
|
|
if ok then
|
|
|
|
table.insert(rst, f.color_code())
|
|
|
|
end
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
|
|
|
local normal_bg = hi.group2dict('Normal').guibg
|
|
|
|
hi.hi({
|
|
|
|
name = 'SpaceVimPickerCode',
|
|
|
|
guifg = color_hi,
|
|
|
|
guibg = normal_bg,
|
|
|
|
})
|
|
|
|
hi.hi({
|
|
|
|
name = 'SpaceVimPickerNoText',
|
|
|
|
guifg = normal_bg,
|
|
|
|
guibg = normal_bg,
|
|
|
|
})
|
|
|
|
hi.hi({
|
|
|
|
name = 'SpaceVimPickerBackground',
|
|
|
|
guibg = color_hi,
|
|
|
|
guifg = color_hi,
|
|
|
|
})
|
2024-07-12 08:11:09 +08:00
|
|
|
|
|
|
|
vim.api.nvim_set_option_value('modifiable', true, {
|
|
|
|
buf = bufnr,
|
|
|
|
})
|
|
|
|
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, rst)
|
|
|
|
vim.api.nvim_set_option_value('modifiable', false, {
|
|
|
|
buf = bufnr,
|
|
|
|
})
|
2024-07-13 18:52:07 +08:00
|
|
|
vim.api.nvim_win_set_config(winid, {
|
|
|
|
height = #rst + 1
|
|
|
|
})
|
2024-07-12 08:11:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- https://zenn.dev/kawarimidoll/articles/a8ac50a17477bd
|
|
|
|
|
|
|
|
local function copy_color()
|
2024-07-13 18:52:07 +08:00
|
|
|
local from, to = vim
|
|
|
|
.regex([[#[0123456789ABCDEF]\+\|rgb(\d\+,\s\d\+,\s\d\+)\|hsl(\d\+,\s\d\+%,\s\d\+%)\|hsv(\d\+,\s\d\+%,\s\d\+%)]])
|
2024-07-12 08:11:09 +08:00
|
|
|
:match_str(vim.fn.getline('.'))
|
|
|
|
if from then
|
|
|
|
vim.fn.setreg('+', string.sub(vim.fn.getline('.'), from, to + 1))
|
|
|
|
notify.notify('copyed:' .. string.sub(vim.fn.getline('.'), from, to + 1))
|
|
|
|
end
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local function increase()
|
|
|
|
if increase_keys[vim.fn.line('.')] then
|
2024-07-13 18:52:07 +08:00
|
|
|
local t, code = increase_keys[vim.fn.line('.')]()
|
|
|
|
color_hi = util.get_hex_code(t, code)
|
|
|
|
for _, format in ipairs(enabled_formats) do
|
|
|
|
local ok, f = pcall(require, 'cpicker.formats.' .. format)
|
|
|
|
if ok then
|
|
|
|
f.on_change(t, code)
|
|
|
|
end
|
|
|
|
end
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
2024-07-12 08:11:09 +08:00
|
|
|
update_buf_text()
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local function reduce()
|
|
|
|
if reduce_keys[vim.fn.line('.')] then
|
2024-07-13 18:52:07 +08:00
|
|
|
local t, code = reduce_keys[vim.fn.line('.')]()
|
|
|
|
color_hi = util.get_hex_code(t, code)
|
|
|
|
for _, format in ipairs(enabled_formats) do
|
|
|
|
local ok, f = pcall(require, 'cpicker.formats.' .. format)
|
|
|
|
if ok then
|
|
|
|
f.on_change(t, code)
|
|
|
|
end
|
|
|
|
end
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
2024-07-12 08:11:09 +08:00
|
|
|
update_buf_text()
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
M.picker = function(formats)
|
2024-07-13 18:52:07 +08:00
|
|
|
enabled_formats = formats
|
|
|
|
log.info(vim.inspect(enabled_formats))
|
2024-07-12 00:31:36 +08:00
|
|
|
if not bufnr or not vim.api.nvim_win_is_valid(bufnr) then
|
|
|
|
bufnr = vim.api.nvim_create_buf(false, false)
|
|
|
|
vim.api.nvim_set_option_value('buftype', 'nofile', {
|
|
|
|
buf = bufnr,
|
|
|
|
})
|
|
|
|
vim.api.nvim_set_option_value('filetype', 'spacevim_cpicker', {
|
|
|
|
buf = bufnr,
|
|
|
|
})
|
|
|
|
vim.api.nvim_set_option_value('bufhidden', 'wipe', {
|
|
|
|
buf = bufnr,
|
|
|
|
})
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'l', '', {
|
|
|
|
callback = increase,
|
|
|
|
})
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'h', '', {
|
|
|
|
callback = reduce,
|
|
|
|
})
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<Right>', '', {
|
|
|
|
callback = increase,
|
|
|
|
})
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<Left>', '', {
|
|
|
|
callback = reduce,
|
|
|
|
})
|
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, 'n', 'q', '', {
|
|
|
|
callback = function()
|
|
|
|
vim.api.nvim_win_close(winid, true)
|
|
|
|
end,
|
|
|
|
})
|
2024-07-12 08:11:09 +08:00
|
|
|
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<Cr>', '', {
|
|
|
|
callback = copy_color,
|
|
|
|
})
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
|
|
|
if not winid or not vim.api.nvim_win_is_valid(winid) then
|
|
|
|
winid = vim.api.nvim_open_win(bufnr, true, {
|
|
|
|
relative = 'cursor',
|
|
|
|
border = 'single',
|
|
|
|
width = 40,
|
|
|
|
height = 10,
|
|
|
|
row = 1,
|
|
|
|
col = 1,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
vim.api.nvim_set_option_value('number', false, {
|
|
|
|
win = winid,
|
|
|
|
})
|
|
|
|
vim.api.nvim_set_option_value('winhighlight', 'NormalFloat:Normal,FloatBorder:WinSeparator', {
|
|
|
|
win = winid,
|
|
|
|
})
|
2024-07-12 08:11:09 +08:00
|
|
|
vim.api.nvim_set_option_value('modifiable', false, {
|
|
|
|
buf = bufnr,
|
|
|
|
})
|
|
|
|
update_buf_text()
|
2024-07-12 00:31:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|