-
Notifications
You must be signed in to change notification settings - Fork 147
/
tabby.lua
220 lines (194 loc) · 6.14 KB
/
tabby.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
-- Tabby
-- https://github.com/UserEast/nightfox.nvim/tree/main/mics/tabby.lua
--
-- This file is a complete example of creating the tabby configuration shown in the readme of
-- nightfox. This configuration generates its own highlight groups from the currently applied
-- colorscheme. These highlight groups are regenreated on colorscheme changes.
--
-- Required plugins:
-- - `nanozuki/tabby.nvim`
--
-- This file is required to be in your `lua` folder of your config. Your colorscheme should also
-- be applied before this file is sourced. This file cannot be located `lua/tabby.lua` as this
-- would clash with the actual plugin require path.
--
--
-- # Example:
--
-- ```lua
-- vim.cmd("colorscheme nightfox")
-- require('user.ui.tabby')
-- ```
--
-- This assumes that this file is located at `lua/user/ui/tabby.lua`
local fmt = string.format
----------------------------------------------------------------------------------------------------
-- Colors
---Convert color number to hex string
---@param n number
---@return string
local hex = function(n)
if n then
return fmt("#%06x", n)
end
end
---Parse `style` string into nvim_set_hl options
---@param style string
---@return table
local function parse_style(style)
if not style or style == "NONE" then
return {}
end
local result = {}
for token in string.gmatch(style, "([^,]+)") do
result[token] = true
end
return result
end
---Get highlight opts for a given highlight group name
---@param name string
---@return table
local function get_highlight(name)
local hl = vim.api.nvim_get_hl_by_name(name, true)
if hl.link then
return get_highlight(hl.link)
end
local result = parse_style(hl.style)
result.fg = hl.foreground and hex(hl.foreground)
result.bg = hl.background and hex(hl.background)
result.sp = hl.special and hex(hl.special)
return result
end
---Set highlight group from provided table
---@param groups table
local function set_highlights(groups)
for group, opts in pairs(groups) do
vim.api.nvim_set_hl(0, group, opts)
end
end
---Generate a color palette from the current applied colorscheme
---@return table
local function generate_pallet_from_colorscheme()
-- stylua: ignore
local color_map = {
black = { index = 0, default = "#393b44" },
red = { index = 1, default = "#c94f6d" },
green = { index = 2, default = "#81b29a" },
yellow = { index = 3, default = "#dbc074" },
blue = { index = 4, default = "#719cd6" },
magenta = { index = 5, default = "#9d79d6" },
cyan = { index = 6, default = "#63cdcf" },
white = { index = 7, default = "#dfdfe0" },
}
local pallet = {}
for name, value in pairs(color_map) do
local global_name = "terminal_color_" .. value.index
pallet[name] = vim.g[global_name] and vim.g[global_name] or value.default
end
pallet.sl = get_highlight("StatusLine")
pallet.tab = get_highlight("TabLine")
pallet.sel = get_highlight("TabLineSel")
pallet.fill = get_highlight("TabLineFill")
return pallet
end
---Generate user highlight groups based on the curent applied colorscheme
---
---NOTE: This is a global because I dont known where this file will be in your config
---and it is needed for the autocmd below
_G._genreate_user_tabline_highlights = function()
local pal = generate_pallet_from_colorscheme()
-- stylua: ignore
local sl_colors = {
Black = { fg = pal.black, bg = pal.white },
Red = { fg = pal.red, bg = pal.sl.bg },
Green = { fg = pal.green, bg = pal.sl.bg },
Yellow = { fg = pal.yellow, bg = pal.sl.bg },
Blue = { fg = pal.blue, bg = pal.sl.bg },
Magenta = { fg = pal.magenta, bg = pal.sl.bg },
Cyan = { fg = pal.cyan, bg = pal.sl.bg },
White = { fg = pal.white, bg = pal.black },
}
local colors = {}
for name, value in pairs(sl_colors) do
colors["User" .. name] = { fg = value.fg, bg = value.bg, bold = true }
colors["UserRv" .. name] = { fg = value.bg, bg = value.fg, bold = true }
end
local groups = {
-- tabline
UserTLHead = { fg = pal.fill.bg, bg = pal.cyan },
UserTLHeadSep = { fg = pal.cyan, bg = pal.fill.bg },
UserTLActive = { fg = pal.sel.fg, bg = pal.sel.bg, bold = true },
UserTLActiveSep = { fg = pal.sel.bg, bg = pal.fill.bg },
UserTLBoldLine = { fg = pal.tab.fg, bg = pal.tab.bg, bold = true },
UserTLLineSep = { fg = pal.tab.bg, bg = pal.fill.bg },
}
set_highlights(vim.tbl_extend("force", colors, groups))
end
_genreate_user_tabline_highlights()
vim.api.nvim_create_augroup("UserTablineHighlightGroups", { clear = true })
vim.api.nvim_create_autocmd({ "SessionLoadPost", "ColorScheme" }, {
callback = function()
_genreate_user_tabline_highlights()
end,
})
----------------------------------------------------------------------------------------------------
-- Feline
local filename = require("tabby.filename")
local cwd = function()
return " " .. vim.fn.fnamemodify(vim.fn.getcwd(), ":t") .. " "
end
local line = {
hl = "TabLineFill",
layout = "active_wins_at_tail",
head = {
{ cwd, hl = "UserTLHead" },
{ "", hl = "UserTLHeadSep" },
},
active_tab = {
label = function(tabid)
return {
" " .. tabid .. " ",
hl = "UserTLActive",
}
end,
left_sep = { "", hl = "UserTLActiveSep" },
right_sep = { "", hl = "UserTLActiveSep" },
},
inactive_tab = {
label = function(tabid)
return {
" " .. tabid .. " ",
hl = "UserTLBoldLine",
}
end,
left_sep = { "", hl = "UserTLLineSep" },
right_sep = { "", hl = "UserTLLineSep" },
},
top_win = {
label = function(winid)
return {
" " .. filename.unique(winid) .. " ",
hl = "TabLine",
}
end,
left_sep = { "", hl = "UserTLLineSep" },
right_sep = { "", hl = "UserTLLineSep" },
},
win = {
label = function(winid)
return {
" " .. filename.unique(winid) .. " ",
hl = "TabLine",
}
end,
left_sep = { "", hl = "UserTLLineSep" },
right_sep = { "", hl = "UserTLLineSep" },
},
tail = {
{ "", hl = "UserTLHeadSep" },
{ " ", hl = "UserTLHead" },
},
}
require("tabby").setup({
tabline = line,
})