summaryrefslogtreecommitdiffstats
path: root/lua/core/theme.lua
blob: f6598ac40723b669c05c8693884765dcc705aeb7 (plain) (blame)
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
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local previewers = require("telescope.previewers")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_set = require("telescope.actions.set")
local action_state = require("telescope.actions.state")
local misc = require('core.misc')

local M = {}

function M.switcher()
  local bufnr = vim.api.nvim_get_current_buf()

  -- show current buffer content in previewer
  local colors = vim.fn.getcompletion('', 'color')
  for k, v in pairs(colors) do
    if v.find(v, '.ext') then
      table.remove(colors, k)
      break
    end
  end

  -- our picker function: colors
  pickers.new({
    prompt_title = "Set Nvim Colorscheme",
    finder = finders.new_table { results = colors },
    sorter = conf.generic_sorter(),
    previewer = previewers.new_buffer_previewer {
      define_preview = function(self, entry)
        -- add content
        local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
        vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)

        -- add syntax highlighting in previewer
        local ft = (vim.filetype.match { buf = bufnr } or "diff"):match "%w+"
        require("telescope.previewers.utils").highlighter(self.state.bufnr, ft)
      end
    },

    attach_mappings = function(prompt_bufnr, map)
      -- reload theme while typing
      vim.schedule(function()
        vim.api.nvim_create_autocmd("TextChangedI", {
          buffer = prompt_bufnr,
          callback = function()
            if action_state.get_selected_entry() then
              misc.colorscheme(action_state.get_selected_entry()[1])
            end
          end,
        })
      end)

      -- reload theme on cycling
      actions.move_selection_previous:replace(function()
        action_set.shift_selection(prompt_bufnr, -1)
        misc.colorscheme(action_state.get_selected_entry()[1])
      end)
      actions.move_selection_next:replace(function()
        action_set.shift_selection(prompt_bufnr, 1)
        misc.colorscheme(action_state.get_selected_entry()[1])
      end)

      -- reload theme on selection
      actions.select_default:replace(function()
        if action_state.get_selected_entry() then
          actions.close(prompt_bufnr)
          misc.colorscheme(action_state.get_selected_entry()[1])

          -- make the colorscheme swap persistant
          misc.replaceword("misc.colorscheme%(.+%)",
            "misc.colorscheme('"..action_state.get_selected_entry()[1].."')")
        end
      end)
      return true
    end,
  }):find()
end

return M