|
| 1 | +local telescope = require("telescope") |
| 2 | +local pickers = require("telescope.pickers") |
| 3 | +local finders = require("telescope.finders") |
| 4 | +local conf = require("telescope.config").values |
| 5 | +local actions = require("telescope.actions") |
| 6 | +local action_state = require("telescope.actions.state") |
| 7 | + |
| 8 | +-- Function to update line number colors |
1 | 9 | local updateLineNumberColors = function() |
2 | 10 | vim.api.nvim_set_hl(0, "LineNr", { fg = "#ffffff" }) |
3 | 11 | vim.api.nvim_set_hl(0, "CursorLineNr", { fg = "#ffffff", bold = true }) |
4 | 12 | end |
5 | 13 |
|
6 | | -vim.api.nvim_create_user_command("LineColor", updateLineNumberColors, {}) |
7 | | -vim.keymap.set("n", "<leader>clc", updateLineNumberColors) |
8 | | - |
9 | | - |
10 | | -function SetColorscheme(scheme) |
| 14 | +-- Function to set colorscheme and update highlights |
| 15 | +local function SetColorscheme(scheme) |
11 | 16 | vim.cmd("colorscheme " .. scheme) |
12 | 17 | updateLineNumberColors() |
13 | 18 | end |
14 | 19 |
|
15 | | -function ListColorschemes() |
16 | | - local themes = {} |
17 | | - for _, theme in ipairs(vim.fn.getcompletion('', 'color')) do |
18 | | - table.insert(themes, theme) |
19 | | - end |
20 | | - return themes |
| 20 | +-- Function to list all available colorschemes |
| 21 | +local function ListColorschemes() |
| 22 | + return vim.fn.getcompletion('', 'color') |
21 | 23 | end |
22 | 24 |
|
23 | | --- Create SetColorscheme command |
24 | | -vim.api.nvim_create_user_command('SetColorscheme', function(opts) |
25 | | - SetColorscheme(opts.args) |
26 | | -end, { |
27 | | - nargs = 1, |
28 | | - complete = function(lead) |
29 | | - return vim.fn.getcompletion(lead, 'color') |
30 | | - end, |
31 | | -}) |
32 | | - |
33 | | --- Create ListColorschemes command |
34 | | -vim.api.nvim_create_user_command('ListColorschemes', function() |
35 | | - local available_themes = ListColorschemes() |
36 | | - print("Available colorschemes:") |
37 | | - for _, theme in ipairs(available_themes) do |
38 | | - print(theme) |
39 | | - end |
40 | | -end, {}) |
| 25 | +-- Telescope picker for colorschemes with live preview |
| 26 | +local function PickColorscheme() |
| 27 | + local themes = ListColorschemes() |
| 28 | + pickers.new({ |
| 29 | + prompt_title = "Select a Colorscheme", |
| 30 | + finder = finders.new_table(themes), |
| 31 | + sorter = conf.generic_sorter({}), |
| 32 | + layout_strategy = "center", -- Centered floating window |
| 33 | + layout_config = { |
| 34 | + width = 0.4, -- 40% of screen width |
| 35 | + height = 0.4, -- 40% of screen height |
| 36 | + }, |
| 37 | + attach_mappings = function(prompt_bufnr, map) |
| 38 | + -- Apply the selected colorscheme when pressing Enter |
| 39 | + actions.select_default:replace(function() |
| 40 | + actions.close(prompt_bufnr) |
| 41 | + local selection = action_state.get_selected_entry() |
| 42 | + if selection then |
| 43 | + SetColorscheme(selection[1]) |
| 44 | + end |
| 45 | + end) |
| 46 | + |
| 47 | + -- Live preview: Change colorscheme as the user moves through options |
| 48 | + map("i", "<Down>", function() |
| 49 | + local selection = action_state.get_selected_entry() |
| 50 | + if selection then |
| 51 | + SetColorscheme(selection[1]) |
| 52 | + end |
| 53 | + actions.move_selection_next(prompt_bufnr) |
| 54 | + end) |
| 55 | + |
| 56 | + map("i", "<Up>", function() |
| 57 | + local selection = action_state.get_selected_entry() |
| 58 | + if selection then |
| 59 | + SetColorscheme(selection[1]) |
| 60 | + end |
| 61 | + actions.move_selection_previous(prompt_bufnr) |
| 62 | + end) |
| 63 | + |
| 64 | + return true |
| 65 | + end, |
| 66 | + }):find() |
| 67 | +end |
| 68 | + |
| 69 | +-- Create a command for opening the colorscheme picker |
| 70 | +vim.api.nvim_create_user_command("PickColorscheme", PickColorscheme, {}) |
| 71 | + |
| 72 | +-- Keymap for opening the colorscheme picker |
| 73 | +vim.keymap.set("n", "<leader>pcs", PickColorscheme) |
0 commit comments