Skip to content

Commit f4ebff0

Browse files
committed
feat(hues): add get_palette()
Details: - With presence of `apply_palette()`, the actually applied palette might be different from the `MiniHues.make_palette(MiniHues.config)`. So there should be some way to get currently "active" palette.
1 parent 66d3cf7 commit f4ebff0

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ There are following change types:
4747

4848
- Make black (0 and 8) and white (7 and 15) colors for built-in terminal different from regular background and foreground. This improves color coverage and does not affect default uncolored text (it is highlighted as `Normal`).
4949

50+
### Expand
51+
52+
- Add `get_palette()` function.
53+
5054
## mini.jump
5155

5256
### Expand

doc/mini-hues.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ Return ~
309309
- <accent> and <accent_bg> represent accent colors with foreground and
310310
background lightness values.
311311

312+
See also ~
313+
|MiniHues.get_palette()|
314+
312315
------------------------------------------------------------------------------
313316
*MiniHues.apply_palette()*
314317
`MiniHues.apply_palette`({palette}, {plugins})
@@ -334,6 +337,18 @@ Usage ~
334337
palette.cyan_bg = '#004629'
335338
require('mini.hues').apply_palette(palette)
336339
<
340+
See also ~
341+
|MiniHues.get_palette()|
342+
343+
------------------------------------------------------------------------------
344+
*MiniHues.get_palette()*
345+
`MiniHues.get_palette`()
346+
Get latest applied palette
347+
348+
Return ~
349+
`(table)` Table with structure as |MiniHues.make_palette()| output that was
350+
the latest applied (via |MiniHues.apply_palette()|) palette.
351+
337352
------------------------------------------------------------------------------
338353
*MiniHues.gen_random_base_colors()*
339354
`MiniHues.gen_random_base_colors`({opts})

lua/mini/hues.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ MiniHues.config = {
314314
--- background.
315315
--- - <accent> and <accent_bg> represent accent colors with foreground and
316316
--- background lightness values.
317+
---
318+
---@seealso |MiniHues.get_palette()|
317319
MiniHues.make_palette = function(config)
318320
config = vim.tbl_deep_extend('force', MiniHues.config, config or {})
319321
local bg = H.validate_hex(config.background, 'background')
@@ -420,11 +422,14 @@ end
420422
--- palette.cyan_bg = '#004629'
421423
--- require('mini.hues').apply_palette(palette)
422424
--- <
425+
---@seealso |MiniHues.get_palette()|
423426
MiniHues.apply_palette = function(palette, plugins)
424427
if type(palette) ~= 'table' then H.error('`palette` should be table with palette colors.') end
425428
plugins = plugins or MiniHues.config.plugins
426429
if type(plugins) ~= 'table' then H.error('`plugins` should be table with plugin integrations data.') end
427430

431+
H.palette = vim.deepcopy(palette)
432+
428433
-- Prepare highlighting application. Notes:
429434
-- - Clear current highlight only if other theme was loaded previously.
430435
-- - No need to `syntax reset` because *all* syntax groups are defined later.
@@ -1506,6 +1511,12 @@ MiniHues.apply_palette = function(palette, plugins)
15061511
vim.g.terminal_color_15 = p[white .. '_edge2']
15071512
end
15081513

1514+
--- Get latest applied palette
1515+
---
1516+
---@return table Table with structure as |MiniHues.make_palette()| output that was
1517+
--- the latest applied (via |MiniHues.apply_palette()|) palette.
1518+
MiniHues.get_palette = function() return vim.deepcopy(H.palette) end
1519+
15091520
--- Generate random base colors
15101521
---
15111522
--- Compute background and foreground colors based on randomly generated hue
@@ -1622,6 +1633,9 @@ H.cusps = {
16221633
}
16231634
--stylua: ignore end
16241635

1636+
-- Latest applied palette
1637+
H.palette = nil
1638+
16251639
-- Helper functionality =======================================================
16261640
-- Settings -------------------------------------------------------------------
16271641
H.setup_config = function(config)

tests/test_hues.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,31 @@ T['apply_palette()']['validates input'] = function()
494494
expect.error(function() apply_palette(palette, 'a') end, '`plugins`.*table')
495495
end
496496

497+
T['get_palette()'] = new_set()
498+
499+
T['get_palette()']['works'] = function()
500+
local res = child.lua([[
501+
local palette = MiniHues.make_palette({ background = '#222222', foreground = '#dddddd' })
502+
palette.fg = '#aaaaaa'
503+
MiniHues.apply_palette(palette)
504+
505+
local res_palette = MiniHues.get_palette()
506+
local is_same = vim.deep_equal(palette, res_palette)
507+
508+
-- Should not be affected by change in applied palette
509+
palette.bg = '#000000'
510+
local is_same_1 = vim.deep_equal(palette, res_palette)
511+
512+
-- Should return copy
513+
res_palette.bg = '#000000'
514+
local is_same_2 = vim.deep_equal(MiniHues.get_palette(), res_palette)
515+
516+
return { is_same, is_same_1, is_same_2 }
517+
]])
518+
519+
eq(res, { true, false, false })
520+
end
521+
497522
T['gen_random_base_colors()'] = new_set()
498523

499524
T['gen_random_base_colors()']['works'] = function()

0 commit comments

Comments
 (0)