Debian Codegraph @

examples/cosmicink.lua

raw
1 -- CosmicInk config for lualine
2 -- Author: Yeeloman
3 -- MIT license, see LICENSE for more details.
4
5 -- Main configuration for setting up lualine.nvim statusline plugin
6
7 -- Default Theme Colors: Define a set of base colors for your theme
8 local colors = {
9 BG = '#16181b', -- Dark background
10 FG = '#c5c4c4', -- Light foreground for contrast
11 YELLOW = '#e8b75f', -- Vibrant yellow
12 CYAN = '#00bcd4', -- Soft cyan
13 DARKBLUE = '#2b3e50', -- Deep blue
14 GREEN = '#00e676', -- Bright green
15 ORANGE = '#ff7733', -- Warm orange
16 VIOLET = '#7a3ba8', -- Strong violet
17 MAGENTA = '#d360aa', -- Deep magenta
18 BLUE = '#4f9cff', -- Light-medium blue
19 RED = '#ff3344', -- Strong red
20 }
21
22 -- Function to get the color associated with the current mode in Vim
23 local function get_mode_color()
24 -- Define a table mapping modes to their associated colors
25 local mode_color = {
26 n = colors.DARKBLUE,
27 i = colors.VIOLET,
28 v = colors.RED,
29 [''] = colors.BLUE,
30 V = colors.RED,
31 c = colors.MAGENTA,
32 no = colors.RED,
33 s = colors.ORANGE,
34 S = colors.ORANGE,
35 [''] = colors.ORANGE,
36 ic = colors.YELLOW,
37 R = colors.ORANGE,
38 Rv = colors.ORANGE,
39 cv = colors.RED,
40 ce = colors.RED,
41 r = colors.CYAN,
42 rm = colors.CYAN,
43 ['r?'] = colors.CYAN,
44 ['!'] = colors.RED,
45 t = colors.RED,
46 }
47 -- Return the opposite color, or fallback to foreground color
48 return mode_color[vim.fn.mode()]
49 end
50
51 -- Function to get the opposite color of a given mode color
52 local function get_opposite_color(mode_color)
53 -- Define a table mapping colors to their opposite color
54 local opposite_colors = {
55 [colors.RED] = colors.CYAN,
56 [colors.BLUE] = colors.ORANGE,
57 [colors.GREEN] = colors.MAGENTA,
58 [colors.MAGENTA] = colors.DARKBLUE,
59 [colors.ORANGE] = colors.BLUE,
60 [colors.CYAN] = colors.YELLOW,
61 [colors.VIOLET] = colors.GREEN,
62 [colors.YELLOW] = colors.RED,
63 [colors.DARKBLUE] = colors.VIOLET,
64 }
65 -- Return the opposite color, or fallback to foreground color
66 return opposite_colors[mode_color] or colors.FG
67 end
68
69 -- Function to get an animated color (randomly chosen from available colors)
70 local function get_animated_color(mode_color)
71 -- Define a list of all available colors
72 local all_colors = {
73 colors.RED,
74 colors.BLUE,
75 colors.GREEN,
76 colors.MAGENTA,
77 colors.ORANGE,
78 colors.CYAN,
79 colors.VIOLET,
80 colors.YELLOW,
81 colors.DARKBLUE,
82 }
83 -- Create a list of possible opposite colors (excluding the current mode color)
84 local possible_opposites = {}
85 for _, color in ipairs(all_colors) do
86 if color ~= mode_color then
87 table.insert(possible_opposites, color)
88 end
89 end
90 -- Randomly select an opposite color
91 if #possible_opposites > 0 then
92 local random_index = math.random(1, #possible_opposites)
93 return possible_opposites[random_index]
94 else
95 return colors.FG -- Default to foreground color if no opposite found
96 end
97 end
98
99 -- Function to interpolate between two colors for a smooth transition
100 local function interpolate_color(color1, color2, step)
101 -- Blend two colors based on the given step factor (0.0 -> color1, 1.0 -> color2)
102 local blend = function(c1, c2, stp)
103 return math.floor(c1 + (c2 - c1) * stp)
104 end
105 -- Extract the RGB values of both colors (in hex)
106 local r1, g1, b1 = tonumber(color1:sub(2, 3), 16), tonumber(color1:sub(4, 5), 16), tonumber(color1:sub(6, 7), 16)
107 local r2, g2, b2 = tonumber(color2:sub(2, 3), 16), tonumber(color2:sub(4, 5), 16), tonumber(color2:sub(6, 7), 16)
108
109 -- Calculate the new RGB values for the blended color
110 local r = blend(r1, r2, step)
111 local g = blend(g1, g2, step)
112 local b = blend(b1, b2, step)
113
114 -- Return the blended color in hex format
115 return string.format('#%02X%02X%02X', r, g, b)
116 end
117
118 -- Function to get a middle color by interpolating between mode color and its opposite
119 local function get_middle_color(color_step)
120 -- Set default value for color_step if not provided
121 color_step = color_step or 0.5 -- If color_step is nil, default to 0.5
122
123 local color1 = get_mode_color() -- Get the current mode color
124 local color2 = get_opposite_color(color1) -- Get the opposite color
125
126 -- Return an interpolated color between the two (based on the color_step value)
127 return interpolate_color(color1, color2, color_step)
128 end
129
130 -- Condition: Check if the buffer is not empty
131 -- This checks whether the current file's name is non-empty.
132 -- If the file is open (i.e., has a name), it returns true, meaning the buffer is not empty.
133 -- local function buffer_not_empty()
134 -- return vim.fn.empty(vim.fn.expand('%:t')) ~= 1 -- 'expand('%:t')' gets the file name
135 -- end
136
137 -- Condition: Hide in width (only show the statusline when the window width is greater than 80)
138 -- This ensures that the statusline will only appear if the current window width exceeds 80 characters.
139 local function hide_in_width()
140 return vim.fn.winwidth(0) > 80 -- 'winwidth(0)' returns the current window width
141 end
142
143 -- Condition: Check if the current workspace is inside a Git repository
144 -- This function checks if the current file is inside a Git repository by looking for a `.git` directory
145 -- in the current file's path. Returns true if the file is in a Git workspace.
146 -- local function check_git_workspace()
147 -- local filepath = vim.fn.expand('%:p:h') -- Get the current file's directory
148 -- local gitdir = vim.fn.finddir('.git', filepath .. ';') -- Search for a `.git` directory in the file path
149 -- return gitdir and #gitdir > 0 and #gitdir < #filepath -- Returns true if a `.git` directory is found
150 -- end
151
152 -- -- Set random seed based on current time for randomness
153 math.randomseed(os.time())
154 -- Icon sets for random selection
155 local icon_sets = {
156 stars = { '★', '☆', '✧', '✦', '✶', '✷', '✸', '✹' }, -- Set of star-like icons
157 runes = {
158 '✠',
159 '⛧',
160 '𖤐',
161 'ᛟ',
162 'ᚨ',
163 'ᚱ',
164 'ᚷ',
165 'ᚠ',
166 'ᛉ',
167 'ᛊ',
168 'ᛏ',
169 '☠',
170 '☾',
171 '♰',
172 '✟',
173 '☽',
174 '⚚',
175 '🜏',
176 }, -- Set of rune-like symbols
177 hearts = { '❤', '♥', '♡', '❦', '❧' }, -- Set of heart-shaped icons
178 waves = { '≈', '∿', '≋', '≀', '⌀', '≣', '⌇' }, -- Set of wave-like symbols
179 crosses = { '☨', '✟', '♰', '♱', '⛨', '' }, -- Set of cross-like symbols
180 }
181
182 -- Function to select a random icon from a given set
183 local function get_random_icon(icons)
184 return icons[math.random(#icons)] -- Returns a random icon from the set
185 end
186
187 -- Function to shuffle the elements in a table
188 local function shuffle_table(tbl)
189 local n = #tbl
190 while n > 1 do
191 local k = math.random(n)
192 tbl[n], tbl[k] = tbl[k], tbl[n] -- Swap elements
193 n = n - 1 -- Decrease the size of the unsorted portion
194 end
195 end
196
197 -- Create a list of all icon sets to allow for random selection from any set
198 local icon_sets_list = {}
199 for _, icons in pairs(icon_sets) do
200 table.insert(icon_sets_list, icons) -- Add each icon set to the list
201 end
202 shuffle_table(icon_sets_list) -- Shuffle the icon sets list
203
204 -- Function to reverse the order of elements in a table
205 local function reverse_table(tbl)
206 local reversed = {}
207 for i = #tbl, 1, -1 do
208 table.insert(reversed, tbl[i]) -- Insert elements in reverse order
209 end
210 return reversed
211 end
212
213 -- Create a reversed list of icon sets
214 local reversed_icon_sets = reverse_table(icon_sets_list)
215
216 -- Function to create a separator component based on side (left/right) and optional mode color
217 local function create_separator(side, use_mode_color)
218 return {
219 function()
220 return side == 'left' and '' or '' -- Choose separator symbol based on side
221 end,
222 color = function()
223 -- Set color based on mode or opposite color
224 local color = use_mode_color and get_mode_color() or get_opposite_color(get_mode_color())
225 return {
226 fg = color,
227 }
228 end,
229 padding = {
230 left = 0,
231 },
232 }
233 end
234
235 -- Function to create a mode-based component (e.g., statusline)
236 -- with optional content, icon, and colors
237 local function create_mode_based_component(content, icon, color_fg, color_bg)
238 return {
239 content,
240 icon = icon,
241 color = function()
242 local mode_color = get_mode_color()
243 local opposite_color = get_opposite_color(mode_color)
244 return {
245 fg = color_fg or colors.FG,
246 bg = color_bg or opposite_color,
247 gui = 'bold',
248 }
249 end,
250 }
251 end
252
253 -- -- Function to get the current mode indicator as a single character
254 local function mode()
255 -- Map of modes to their respective shorthand indicators
256 local mode_map = {
257 n = 'N', -- Normal mode
258 i = 'I', -- Insert mode
259 v = 'V', -- Visual mode
260 [''] = 'V', -- Visual block mode
261 V = 'V', -- Visual line mode
262 c = 'C', -- Command-line mode
263 no = 'N', -- NInsert mode
264 s = 'S', -- Select mode
265 S = 'S', -- Select line mode
266 ic = 'I', -- Insert mode (completion)
267 R = 'R', -- Replace mode
268 Rv = 'R', -- Virtual Replace mode
269 cv = 'C', -- Command-line mode
270 ce = 'C', -- Ex mode
271 r = 'R', -- Prompt mode
272 rm = 'M', -- More mode
273 ['r?'] = '?', -- Confirm mode
274 ['!'] = '!', -- Shell mode
275 t = 'T', -- Terminal mode
276 }
277 -- Return the mode shorthand or [UNKNOWN] if no match
278 return mode_map[vim.fn.mode()] or '[UNKNOWN]'
279 end
280
281 -- Config
282 local config = {
283 options = {
284 component_separators = '',
285 section_separators = '',
286 theme = {
287 normal = {
288 c = {
289 fg = colors.FG,
290 bg = colors.BG,
291 },
292 },
293 inactive = {
294 c = {
295 fg = colors.FG,
296 bg = colors.BG,
297 },
298 }, -- Simplified inactive theme
299 },
300 disabled_filetypes = {
301 'neo-tree',
302 'undotree',
303 'sagaoutline',
304 'diff',
305 },
306 },
307 sections = {
308 lualine_a = {},
309 lualine_b = {},
310 lualine_c = {},
311 lualine_x = {},
312 lualine_y = {},
313 lualine_z = {},
314 },
315 inactive_sections = {
316 lualine_a = {},
317 lualine_b = {},
318 lualine_c = {
319 {
320
321 'location',
322 color = function()
323 return {
324 fg = colors.FG,
325 gui = 'bold',
326 }
327 end,
328 },
329 },
330 lualine_x = {
331 {
332 'filename',
333 color = function()
334 return {
335 fg = colors.FG,
336 gui = 'bold,italic',
337 }
338 end,
339 },
340 },
341 lualine_y = {},
342 lualine_z = {},
343 },
344 }
345
346 -- Helper functions
347 local function ins_left(component)
348 table.insert(config.sections.lualine_c, component)
349 end
350
351 local function ins_right(component)
352 table.insert(config.sections.lualine_x, component)
353 end
354
355 -- LEFT
356 ins_left {
357 mode,
358 color = function()
359 local mode_color = get_mode_color()
360 return {
361 fg = colors.BG,
362 bg = mode_color,
363 gui = 'bold',
364 }
365 end,
366 padding = { left = 1, right = 1 },
367 }
368
369 ins_left(create_separator('left', true))
370
371 ins_left {
372 function()
373 return vim.fn.fnamemodify(vim.fn.getcwd(), ':t')
374 end,
375 icon = ' ',
376 color = function()
377 local virtual_env = vim.env.VIRTUAL_ENV
378 if virtual_env then
379 return {
380 fg = get_mode_color(),
381 gui = 'bold,strikethrough',
382 }
383 else
384 return {
385 fg = get_mode_color(),
386 gui = 'bold',
387 }
388 end
389 end,
390 }
391
392 ins_left(create_separator('right'))
393
394 ins_left(create_mode_based_component('filename', nil, colors.BG))
395
396 ins_left(create_separator('left'))
397
398 ins_left {
399 function()
400 return ''
401 end,
402 color = function()
403 return {
404 fg = get_middle_color(),
405 }
406 end,
407 cond = hide_in_width,
408 }
409
410 ins_left {
411 function()
412 local git_status = vim.b.gitsigns_status_dict
413 if git_status then
414 return string.format('+%d ~%d -%d', git_status.added or 0, git_status.changed or 0, git_status.removed or 0)
415 end
416 return ''
417 end,
418 -- icon = '󰊢 ',
419 color = {
420 fg = colors.YELLOW,
421 gui = 'bold',
422 },
423 cond = hide_in_width,
424 }
425
426 for _, icons in pairs(icon_sets_list) do
427 ins_left {
428 function()
429 return get_random_icon(icons)
430 end,
431 color = function()
432 return {
433 fg = get_animated_color(),
434 }
435 end,
436 cond = hide_in_width,
437 }
438 end
439
440 ins_left {
441 'searchcount',
442 color = {
443 fg = colors.GREEN,
444 gui = 'bold',
445 },
446 }
447
448 -- RIGHT
449 ins_right {
450 function()
451 local reg = vim.fn.reg_recording()
452 return reg ~= '' and '[' .. reg .. ']' or ''
453 end,
454 color = {
455 fg = '#ff3344',
456 gui = 'bold',
457 },
458 cond = function()
459 return vim.fn.reg_recording() ~= ''
460 end,
461 }
462
463 ins_right {
464 'selectioncount',
465 color = {
466 fg = colors.GREEN,
467 gui = 'bold',
468 },
469 }
470
471 for _, icons in ipairs(reversed_icon_sets) do
472 ins_right {
473 function()
474 return get_random_icon(icons)
475 end,
476 color = function()
477 return {
478 fg = get_animated_color(),
479 }
480 end,
481 cond = hide_in_width,
482 }
483 end
484
485 ins_right {
486 function()
487 local msg = 'No Active Lsp'
488 local buf_ft = vim.api.nvim_buf_get_option(0, 'filetype')
489 local clients = vim.lsp.get_clients()
490 if next(clients) == nil then
491 return msg
492 end
493 local lsp_short_names = {
494 pyright = 'py',
495 tsserver = 'ts',
496 rust_analyzer = 'rs',
497 lua_ls = 'lua',
498 clangd = 'c++',
499 bashls = 'sh',
500 jsonls = 'json',
501 html = 'html',
502 cssls = 'css',
503 tailwindcss = 'tw',
504 dockerls = 'docker',
505 sqlls = 'sql',
506 yamlls = 'yml',
507 }
508 for _, client in ipairs(clients) do
509 local filetypes = client.config.filetypes
510 if filetypes and vim.fn.index(filetypes, buf_ft) ~= -1 then
511 return lsp_short_names[client.name] or client.name:sub(1, 2)
512 end
513 end
514 return msg
515 end,
516 icon = ' ',
517 color = {
518 fg = colors.YELLOW,
519 gui = 'bold',
520 },
521 }
522
523 ins_right {
524 function()
525 return ''
526 end,
527 color = function()
528 return { fg = get_middle_color() }
529 end,
530 cond = hide_in_width,
531 }
532
533 ins_right(create_separator('right'))
534
535 ins_right(create_mode_based_component('location', nil, colors.BG))
536
537 ins_right(create_separator('left'))
538
539 ins_right {
540 'branch',
541 icon = ' ',
542 --[[ Truncates and formats Git branch names for display in lualine:
543 First segment: Uppercase, truncated to 1 character.
544 Middle segments: Lowercase, truncated to 1 character.
545 Last segment: Unchanged.
546 Separator: › between truncated segments and the last segment.
547
548 Example Input/Output:
549 Branch Name Output
550 backend/setup/tailwind Bs›tailwind
551 feature/add-ui Fa›add-ui
552 main main
553 ]]
554 fmt = function(branch)
555 if branch == '' or branch == nil then
556 return 'No Repo'
557 end
558
559 -- Function to truncate a segment to a specified length
560 local function truncate_segment(segment, max_length)
561 if #segment > max_length then
562 return segment:sub(1, max_length)
563 end
564 return segment
565 end
566
567 -- Split the branch name by '/'
568 local segments = {}
569 for segment in branch:gmatch('[^/]+') do
570 table.insert(segments, segment)
571 end
572
573 -- Truncate all segments except the last one
574 for i = 1, #segments - 1 do
575 segments[i] = truncate_segment(segments[i], 1) -- Truncate to 1 character
576 end
577
578 -- If there's only one segment (no '/'), return it as-is
579 if #segments == 1 then
580 return segments[1]
581 end
582
583 -- Capitalize the first segment and lowercase the rest (except the last one)
584 segments[1] = segments[1]:upper() -- First segment uppercase
585 for i = 2, #segments - 1 do
586 segments[i] = segments[i]:lower() -- Other segments lowercase
587 end
588
589 -- Combine the first segments with no separator and add '›' before the last segment
590 local truncated_branch = table.concat(segments, '', 1, #segments - 1) .. '›' .. segments[#segments]
591
592 -- Ensure the final result doesn't exceed a maximum length
593 local max_total_length = 15
594 if #truncated_branch > max_total_length then
595 truncated_branch = truncated_branch:sub(1, max_total_length) .. '…'
596 end
597
598 return truncated_branch
599 end,
600 color = function()
601 local mode_color = get_mode_color()
602 return {
603 fg = mode_color,
604 gui = 'bold',
605 }
606 end,
607 }
608
609 ins_right(create_separator('right'))
610
611 ins_right(create_mode_based_component('progress', nil, colors.BG))
612
613 require('lualine').setup(config)