1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 07:20:04 +08:00

feat(cpicker): add tools#cpicker layer

This commit is contained in:
Eric Wong 2024-07-12 00:31:36 +08:00
parent 0d84d91b4e
commit ff20c0284b
14 changed files with 606 additions and 68 deletions

View File

@ -1771,6 +1771,11 @@ let g:_spacevim_mappings_space.d = {'name' : '+Debug'}
let g:_spacevim_mappings_space.e = {'name' : '+Errors/Encoding'}
let g:_spacevim_mappings_space.B = {'name' : '+Global buffers'}
let g:_spacevim_mappings_space.f.v = {'name' : '+Vim/SpaceVim'}
let g:_spacevim_mappings_space.i = {'name' : '+Insertion'}
let g:_spacevim_mappings_space.i = {'name' : '+Insertion'}
let g:_spacevim_mappings_space.i.l = {'name' : '+Lorem-ipsum'}
let g:_spacevim_mappings_space.i.p = {'name' : '+Passwords/Picker'}
let g:_spacevim_mappings_space.i.U = {'name' : '+UUID'}
function! SpaceVim#end() abort
let &tabline = ' '

View File

@ -35,7 +35,6 @@ let s:filename = expand('<sfile>:~')
let s:lnum = expand('<slnum>') + 2
function! SpaceVim#layers#denite#config() abort
let g:_spacevim_mappings_space.i = {'name' : '+Insertion'}
call SpaceVim#mapping#space#def('nnoremap', ['i', 'u'], 'Denite unicode', 'search-and-insert-unicode', 1)
if g:spacevim_snippet_engine ==# 'neosnippet'
call SpaceVim#mapping#space#def('nnoremap', ['i', 's'], 'Denite neosnippet', 'insert snippets', 1)

View File

@ -321,10 +321,6 @@ function! SpaceVim#layers#edit#config() abort
call SpaceVim#mapping#space#def('nmap', ['x', 'g', 'n'], '<Plug>(grammarous-move-to-next-error)', 'move-cursor-to-next-error', 0, 1)
call SpaceVim#mapping#space#def('nmap', ['x', 'g', 'p'], '<Plug>(grammarous-move-to-previous-error)', 'move-cursor-to-previous-error', 0, 1)
let g:_spacevim_mappings_space.i = {'name' : '+Insertion'}
let g:_spacevim_mappings_space.i.l = {'name' : '+Lorem-ipsum'}
let g:_spacevim_mappings_space.i.p = {'name' : '+Passwords'}
let g:_spacevim_mappings_space.i.U = {'name' : '+UUID'}
call SpaceVim#mapping#space#def('nnoremap', ['i', 'p', 1], 'call call('
\ . string(s:_function('s:insert_simple_password')) . ', [])',
\ 'insert-simple-password', 1)

View File

@ -243,7 +243,6 @@ function! SpaceVim#layers#leaderf#config() abort
\ 'after_enter' : string(s:_function('s:init_leaderf_win', 1))[10:-3]
\ }
let g:_spacevim_mappings_space.i = {'name' : '+Insertion'}
call SpaceVim#mapping#space#def('nnoremap', ['i', 'u'], 'Leaderf unicode', 'search-and-insert-unicode', 1)
if g:spacevim_snippet_engine ==# 'neosnippet'
call SpaceVim#mapping#space#def('nnoremap', ['i', 's'], 'Leaderf neosnippet', 'insert snippets', 1)

View File

@ -0,0 +1,50 @@
"=============================================================================
" cpicker.vim
" Copyright (c) 2016-2019 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
""
" @section tools#cpicker, layers-tools-cpicker
" @parentsection layers
" The `tools#cpicker` layer provides a color picker.
" this layer is disabled by default, to enable this layer, add following
" snippet to your SpaceVim configuration file.
" >
" [[layers]]
" name = 'tools#cpicker'
" <
"
" @subsection Key bindings
" >
" Key Function
" ---------------------------------------------
" SPC i p c open color picker
" <
function! SpaceVim#layers#tools#cpicker#plugins() abort
return [
\ [g:_spacevim_root_dir . 'bundle/cpicker.nvim', {'merged' : 0, 'loadconf' : 1, 'on_cmd' : 'Cpicker'}],
\ ]
endfunction
function! SpaceVim#layers#tools#cpicker#config() abort
call SpaceVim#mapping#space#def('nnoremap', ['i', 'p', 'c'], 'Cpicker all',
\ 'insert-color-with-picker', 1)
endfunction
function! SpaceVim#layers#tools#cpicker#set_variable(var) abort
let g:cpicker_default_format = get(a:var, 'default_format', 'hex')
endfunction
function! SpaceVim#layers#tools#cpicker#loadeable() abort
return has('nvim-0.10.0')
endfunction

View File

@ -82,7 +82,6 @@ function! SpaceVim#layers#unite#config() abort
call SpaceVim#mapping#space#def('nnoremap', ['i', 'u'], 'Unite unicode', 'search-and-insert-unicode', 1)
call SpaceVim#mapping#space#def('nnoremap', ['f', 'r'], 'Unite file_mru', 'open-recent-file', 1)
call SpaceVim#mapping#space#def('nnoremap', ['r', 'l'], 'Unite resume', 'resume unite buffer', 1)
let g:_spacevim_mappings_space.i = {'name' : '+Insertion'}
call SpaceVim#mapping#space#def('nnoremap', ['i', 'u'], 'Unite unicode', 'search-and-insert-unicode', 1)
if g:spacevim_snippet_engine ==# 'neosnippet'
call SpaceVim#mapping#space#def('nnoremap', ['i', 's'], 'Unite neosnippet', 'insert snippets', 1)

View File

@ -0,0 +1,289 @@
--=============================================================================
-- 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
local hi = require('spacevim.api.vim.highlight')
local color = require('spacevim.api.color')
local red = 0 -- [0, 255]
local green = 0 -- [0, 255]
local blue = 0 -- [0, 255]
local hue = 0 -- [0, 360]
local saturation = 0 -- [0, 100%]
local lightness = 0 -- [0, 100%]
local enabled_formats = {}
local increase_keys = {}
local reduce_keys = {}
local function decimalToHex(decimal)
local hex = ''
local hexChars = '0123456789ABCDEF'
while decimal > 0 do
local mod = decimal % 16
hex = string.sub(hexChars, mod + 1, mod + 1) .. hex
decimal = math.floor(decimal / 16)
end
if hex == '' then
return '00'
elseif #hex == 1 then
return '0' .. hex
else
return hex
end
end
local function generate_bar(n, char, m)
return string.rep(char, math.floor(24 * n / (m or 255)))
end
local function increase_rgb_red()
if red < 255 then
red = red + 1
hue, saturation, lightness = color.rgb2hsl(red, green, blue)
end
end
local function reduce_rgb_red()
if red > 0 then
red = red - 1
hue, saturation, lightness = color.rgb2hsl(red, green, blue)
end
end
local function increase_rgb_green()
if green < 255 then
green = green + 1
hue, saturation, lightness = color.rgb2hsl(red, green, blue)
end
end
local function reduce_rgb_green()
if green > 0 then
green = green - 1
hue, saturation, lightness = color.rgb2hsl(red, green, blue)
end
end
local function increase_rgb_blue()
if blue < 255 then
blue = blue + 1
hue, saturation, lightness = color.rgb2hsl(red, green, blue)
end
end
local function reduce_rgb_blue()
if blue > 0 then
blue = blue - 1
hue, saturation, lightness = color.rgb2hsl(red, green, blue)
end
end
local function increase_hsl_h()
if hue < 360 then
hue = hue + 1
red, green, blue = color.hsl2rgb(hue, saturation, lightness)
end
end
local function reduce_hsl_h()
if hue >= 1 then
hue = hue - 1
elseif hue > 0 then
hue = 0
end
red, green, blue = color.hsl2rgb(hue, saturation, lightness)
end
local function increase_hsl_s()
if saturation <= 0.99 then
saturation = saturation + 0.01
elseif saturation < 1 then
saturation = 1
end
red, green, blue = color.hsl2rgb(hue, saturation, lightness)
end
local function reduce_hsl_s()
if saturation >= 0.01 then
saturation = saturation - 0.01
elseif saturation > 0 and saturation < 0.01 then
saturation = 0
end
red, green, blue = color.hsl2rgb(hue, saturation, lightness)
end
local function increase_hsl_l()
if lightness <= 0.99 then
lightness = lightness + 0.01
elseif lightness < 1 then
lightness = 1
end
red, green, blue = color.hsl2rgb(hue, saturation, lightness)
end
local function reduce_hsl_l()
if lightness >= 0.01 then
lightness = lightness - 0.01
elseif lightness > 0 and lightness < 0.01 then
lightness = 0
end
red, green, blue = color.hsl2rgb(hue, saturation, lightness)
end
local function buf_text()
local rst = {}
local r_bar = generate_bar(red, '+')
local g_bar = generate_bar(green, '+')
local b_bar = generate_bar(blue, '+')
local h_bar = generate_bar(hue, '+', 360)
local s_bar = generate_bar(saturation, '+', 1)
local l_bar = generate_bar(lightness, '+', 1)
if enabled_formats.all or enabled_formats.rgb then
table.insert(rst, 'R: ' .. string.format('%4s', red) .. ' ' .. r_bar)
increase_keys[#rst] = increase_rgb_red
reduce_keys[#rst] = reduce_rgb_red
table.insert(rst, 'G: ' .. string.format('%4s', green) .. ' ' .. g_bar)
increase_keys[#rst] = increase_rgb_green
reduce_keys[#rst] = reduce_rgb_green
table.insert(rst, 'B: ' .. string.format('%4s', blue) .. ' ' .. b_bar)
increase_keys[#rst] = increase_rgb_blue
reduce_keys[#rst] = reduce_rgb_blue
end
if enabled_formats.all or enabled_formats.hsl then
table.insert(rst, 'H: ' .. string.format('%4s', math.floor(hue + 0.5)) .. ' ' .. h_bar)
increase_keys[#rst] = increase_hsl_h
reduce_keys[#rst] = reduce_hsl_h
table.insert(
rst,
'S: ' .. string.format('%3s', math.floor(saturation * 100 + 0.5)) .. '% ' .. s_bar
)
increase_keys[#rst] = increase_hsl_s
reduce_keys[#rst] = reduce_hsl_s
table.insert(
rst,
'L: ' .. string.format('%3s', math.floor(lightness * 100 + 0.5)) .. '% ' .. l_bar
)
increase_keys[#rst] = increase_hsl_l
reduce_keys[#rst] = reduce_hsl_l
end
table.insert(rst, '')
local r = decimalToHex(red)
local g = decimalToHex(green)
local b = decimalToHex(blue)
if not vim.g.cpicker_default_format or vim.g.cpicker_default_format == 'hex' then
table.insert(rst, ' =========' .. ' #' .. r .. g .. b)
elseif vim.g.cpicker_default_format == 'rgb' then
table.insert(rst, ' =========' .. string.format(' rgb(%s, %s, %s)', red, green, blue))
elseif vim.g.cpicker_default_format == 'hsl' then
table.insert(
rst,
' ========='
.. string.format(
' hsl(%s, %s%%, %s%%)',
math.floor(hue + 0.5),
math.floor(saturation * 100 + 0.5),
math.floor(lightness * 100 + 0.5)
)
)
elseif vim.g.cpicker_default_format == 'all' then
table.insert(rst, ' =========' .. ' #' .. r .. g .. b)
table.insert(rst, ' =========' .. string.format(' rgb(%s, %s, %s)', red, green, blue))
table.insert(
rst,
' ========='
.. string.format(
' hsl(%s, %s%%, %s%%)',
math.floor(hue + 0.5),
math.floor(saturation * 100 + 0.5),
math.floor(lightness * 100 + 0.5)
)
)
end
local color_hi = '#' .. r .. g .. b
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,
})
return rst
end
local function increase()
if increase_keys[vim.fn.line('.')] then
increase_keys[vim.fn.line('.')]()
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, buf_text())
end
local function reduce()
if reduce_keys[vim.fn.line('.')] then
reduce_keys[vim.fn.line('.')]()
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, buf_text())
end
M.picker = function(formats)
enabled_formats = {}
for _, v in ipairs(formats) do
enabled_formats[v] = true
end
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,
})
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_buf_set_lines(bufnr, 0, -1, false, buf_text())
vim.api.nvim_set_option_value('number', false, {
win = winid,
})
vim.api.nvim_set_option_value('winhighlight', 'NormalFloat:Normal,FloatBorder:WinSeparator', {
win = winid,
})
end
return M

View File

@ -0,0 +1,17 @@
--=============================================================================
-- cpicker.lua
-- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
if vim.api.nvim_create_user_command then
local function complete(...)
return {'all', 'rgb', 'hsl'}
end
vim.api.nvim_create_user_command('Cpicker', function(opt)
require('cpicker').picker(opt.fargs)
end, { nargs = '*', complete = complete })
end

View File

@ -0,0 +1,13 @@
if exists('b:current_syntax') && b:current_syntax ==# 'spacevim_cpicker'
finish
endif
let b:current_syntax = 'spacevim_cpicker'
syntax case ignore
syn match ProcessBar /[?=+]\+/
syn match SpaceVimPickerCode /#[0123456789ABCDEF]\+\|rgb(\d\+,\s\d\+,\s\d\+)\|hsl(\d\+,\s\d\+%,\s\d\+%)/
syn match SpaceVimPickerBackground /=\+/
highlight ProcessBar ctermfg=Gray ctermbg=Gray guifg=Gray guibg=Gray

View File

@ -243,14 +243,15 @@ CONTENTS *SpaceVim-contents*
139. telescope................................ |SpaceVim-layers-telescope|
140. test.......................................... |SpaceVim-layers-test|
141. tmux.......................................... |SpaceVim-layers-tmux|
142. tools#dash.............................. |SpaceVim-layers-tools-dash|
143. tools#mpv................................ |SpaceVim-layers-tools-mpv|
144. tools#screensaver................ |SpaceVim-layers-tools-screensaver|
145. tools#zeal.............................. |SpaceVim-layers-tools-zeal|
146. treesitter.............................. |SpaceVim-layers-treesitter|
147. ui.............................................. |SpaceVim-layers-ui|
148. unite........................................ |SpaceVim-layers-unite|
149. xmake........................................ |SpaceVim-layers-xmake|
142. tools#cpicker........................ |SpaceVim-layers-tools-cpicker|
143. tools#dash.............................. |SpaceVim-layers-tools-dash|
144. tools#mpv................................ |SpaceVim-layers-tools-mpv|
145. tools#screensaver................ |SpaceVim-layers-tools-screensaver|
146. tools#zeal.............................. |SpaceVim-layers-tools-zeal|
147. treesitter.............................. |SpaceVim-layers-treesitter|
148. ui.............................................. |SpaceVim-layers-ui|
149. unite........................................ |SpaceVim-layers-unite|
150. xmake........................................ |SpaceVim-layers-xmake|
7. Usage..................................................... |SpaceVim-usage|
1. Editing....................................... |SpaceVim-usage-editing|
2. Error handling......................... |SpaceVim-usage-error-handling|
@ -6290,6 +6291,25 @@ MAPPINGS
<C-l> normal Switch to vim/tmux pane in right direction
<
==============================================================================
TOOLS#CPICKER *SpaceVim-layers-tools-cpicker*
The `tools#cpicker` layer provides a color picker. this layer is disabled by
default, to enable this layer, add following snippet to your SpaceVim
configuration file.
>
[[layers]]
name = 'tools#cpicker'
<
KEY BINDINGS
>
Key Function
---------------------------------------------
SPC i p c open color picker
<
==============================================================================
TOOLS#DASH *SpaceVim-layers-tools-dash*

View File

@ -0,0 +1,34 @@
---
title: "SpaceVim tools#cpicker 模块"
description: "这一模块为 SpaceVim 提供了调色板功能,可以快速调配颜色。"
lang: zh
---
# [可用模块](../../) >> tools#cpicker
<!-- vim-markdown-toc GFM -->
- [模块描述](#模块描述)
- [启用模块](#启用模块)
- [快捷键](#快捷键)
<!-- vim-markdown-toc -->
## 模块描述
该模块为 SpaceVim 提供了调色板功能, 需要 Neovim 0.10+。
## 启用模块
tools#mpv 模块默认并未启用,如果需要启用该模块,需要在配置文件里面加入:
```toml
[[layers]]
name = "tools#cpicker"
```
## 快捷键
| 快捷键 | 功能描述 |
| ----------- | ---------- |
| `SPC i p c` | 打开调色板 |

View File

@ -1,53 +1,53 @@
---
title: "SpaceVim tools#mpv 模块"
description: "这一模块为 SpaceVim 提供了mpv支持可快速查找光标位置的单词。"
lang: zh
---
# [可用模块](../) >> tools#mpv
<!-- vim-markdown-toc GFM -->
- [模块描述](#模块描述)
- [启用模块](#启用模块)
- [模块选项](#模块选项)
- [快捷键](#快捷键)
<!-- vim-markdown-toc -->
## 模块描述
该模块为 SpaceVim 提供了 mpv 集成。
## 启用模块
tools#mpv 模块默认并未启用,如果需要启用该模块,需要在配置文件里面加入:
```toml
[[layers]]
name = "tools#mpv"
```
## 模块选项
- `musics_directory`: 设置音乐存放文件夹,默认的文件夹是`~/Music`。
- `mpv_interpreter`: mpv 可执行命令路径
- `loop_mode`: 循环模式,默认是 `random`
example:
```toml
[[layers]]
name = 'tools#mpv'
mpv_interpreter = 'D:\Program Files\mpv\mpv.exe'
musics_directory = 'F:\other\musics'
```
## 快捷键
| 快捷键 | 功能描述 |
| ----------- | ---------------- |
| `SPC m m l` | 模糊搜索音乐列表 |
| `SPC m m n` | 下一首 |
| `SPC m m s` | 停止播放 |
---
title: "SpaceVim tools#mpv 模块"
description: "这一模块为 SpaceVim 提供了mpv支持可快速查找光标位置的单词。"
lang: zh
---
# [可用模块](../../) >> tools#mpv
<!-- vim-markdown-toc GFM -->
- [模块描述](#模块描述)
- [启用模块](#启用模块)
- [模块选项](#模块选项)
- [快捷键](#快捷键)
<!-- vim-markdown-toc -->
## 模块描述
该模块为 SpaceVim 提供了 mpv 集成。
## 启用模块
tools#mpv 模块默认并未启用,如果需要启用该模块,需要在配置文件里面加入:
```toml
[[layers]]
name = "tools#mpv"
```
## 模块选项
- `musics_directory`: 设置音乐存放文件夹,默认的文件夹是`~/Music`。
- `mpv_interpreter`: mpv 可执行命令路径
- `loop_mode`: 循环模式,默认是 `random`
example:
```toml
[[layers]]
name = 'tools#mpv'
mpv_interpreter = 'D:\Program Files\mpv\mpv.exe'
musics_directory = 'F:\other\musics'
```
## 快捷键
| 快捷键 | 功能描述 |
| ----------- | ---------------- |
| `SPC m m l` | 模糊搜索音乐列表 |
| `SPC m m n` | 下一首 |
| `SPC m m s` | 停止播放 |

View File

@ -0,0 +1,33 @@
---
title: "SpaceVim cpicker layer"
description: "This layer provides color picker for SpaceVim"
---
# [Available Layers](../../) >> tools#cpicker
<!-- vim-markdown-toc GFM -->
- [Description](#description)
- [Install](#install)
- [Key bindings](#key-bindings)
<!-- vim-markdown-toc -->
## Description
This layer provides color picker for SpaceVim. It requires neovim 0.10+.
## Install
To use this configuration layer, add it to your `~/.SpaceVim.d/init.toml`.
```vim
[[layers]]
name = "tools#cpicker"
```
## Key bindings
| Key Binding | Description |
| ----------- | ----------------- |
| `SPC i p c` | open color picker |

View File

@ -0,0 +1,84 @@
--=============================================================================
-- color.lua ---
-- Copyright (c) 2019-2024 Wang Shidong & Contributors
-- Author: Wang Shidong < wsdjeg@outlook.com >
-- URL: https://spacevim.org
-- License: GPLv3
--=============================================================================
local color = {}
-- 参考: https://blog.csdn.net/Sunshine_in_Moon/article/details/45131285
color.rgb2hsl = function(r, g, b)
r = r / 255
g = g / 255
b = b / 255
local max = math.max(r, g, b)
local min = math.min(r, g, b)
local h, s, l
if max == min then
h = 0
elseif max == r and g >= b then
h = 60 * (g - b) / (max - min) + 0
elseif max == r and g < b then
h = 60 * (g - b) / (max - min) + 360
elseif max == g then
h = 60 * (b - r) / (max - min) + 120
elseif max == b then
h = 60 * (r - g) / (max - min) + 240
end
l = 1 / 2 * (max + min)
if l == 0 or max == min then
s = 0
elseif l > 0 and l <= 0.5 then
s = (max - min) / (2 * l)
elseif l > 0.5 then
s = (max - min) / (2 - 2 * l)
end
return math.floor(h), s, l
end
-- https://stackoverflow.com/questions/68317097/how-to-properly-convert-hsl-colors-to-rgb-colors-in-lua
color.hsl2rgb = function(h, s, l)
h = h / 360
local r, g, b
if s == 0 then
r, g, b = l, l, l
else
local function hue2rgb(p, q, t)
if t < 0 then
t = t + 1
end
if t > 1 then
t = t - 1
end
if t < 1 / 6 then
return p + (q - p) * 6 * t
end
if t < 1 / 2 then
return q
end
if t < 2 / 3 then
return p + (q - p) * (2 / 3 - t) * 6
end
return p
end
local q = l < 0.5 and l * (1 + s) or l + s - l * s
local p = 2 * l - q
r = hue2rgb(p, q, h + 1 / 3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h - 1 / 3)
end
return math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)
end
return color