Skip to content

Commit

Permalink
feat: enable to preview themes (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyaowong committed Jul 16, 2021
1 parent 1866265 commit 7473962
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ builtin.colorscheme({opts}) *builtin.colorscheme()*
Parameters: ~
{opts} (table) options to pass to the picker

Fields: ~
{enable_preview} (boolean) if true, will preview the selected color


builtin.marks({opts}) *builtin.marks()*
Lists vim marks and their value, jumps to the mark on `<cr>`
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ builtin.buffers = require('telescope.builtin.internal').buffers

--- Lists available colorschemes and applies them on `<cr>`
---@param opts table: options to pass to the picker
---@field enable_preview boolean: if true, will preview the selected color
builtin.colorscheme = require('telescope.builtin.internal').colorscheme

--- Lists vim marks and their value, jumps to the mark on `<cr>`
Expand Down
91 changes: 83 additions & 8 deletions lua/telescope/builtin/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -635,26 +635,101 @@ internal.buffers = function(opts)
end

internal.colorscheme = function(opts)
local colors = vim.list_extend(opts.colors or {}, vim.fn.getcompletion('', 'color'))
local before_color = vim.api.nvim_exec('colorscheme', true)
local need_restore = true

pickers.new(opts,{
prompt = 'Change Colorscheme',
local colors = opts.colors or { before_color }
if not vim.tbl_contains(colors, before_color) then
table.insert(colors, 1, before_color)
end

colors = vim.list_extend(
colors,
vim.tbl_filter(function(color)
return color ~= before_color
end, vim.fn.getcompletion(
'',
'color'
))
)

local previewer
if opts.enable_preview then
-- define previewer
local bufnr = vim.api.nvim_get_current_buf()
local p = vim.api.nvim_buf_get_name(bufnr)

-- don't need previewer
if vim.fn.buflisted(bufnr) ~= 1 then
local deleted = false
local function del_win(win_id)
if win_id and vim.api.nvim_win_is_valid(win_id) then
utils.buf_delete(vim.api.nvim_win_get_buf(win_id))
pcall(vim.api.nvim_win_close, win_id, true)
end
end

previewer = previewers.new {
preview_fn = function(_, entry, status)
if not deleted then
deleted = true
del_win(status.preview_win)
del_win(status.preview_border_win)
end
vim.cmd('colorscheme ' .. entry.value)
end,
}
else
-- show current buffer content in previewer
previewer = previewers.new_buffer_previewer {
get_buffer_by_name = function()
return p
end,
define_preview = function(self, entry)
if vim.loop.fs_stat(p) then
conf.buffer_previewer_maker(p, self.state.bufnr, { bufname = self.state.bufname })
else
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)
end
vim.cmd('colorscheme ' .. entry.value)
end,
}
end
end

local picker = pickers.new(opts, {
prompt_title = 'Change Colorscheme',
finder = finders.new_table {
results = colors
results = colors,
},
-- TODO: better preview?
sorter = conf.generic_sorter(opts),
previewer = previewer,
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()

actions.close(prompt_bufnr)
vim.cmd("colorscheme " .. selection.value)

need_restore = false
vim.cmd('colorscheme ' .. selection.value)
end)

return true
end,
})

if opts.enable_preview then
-- rewrite picker.close_windows. restore color if needed
local close_windows = picker.close_windows
picker.close_windows = function(status)
close_windows(status)
if need_restore then
vim.cmd('colorscheme ' .. before_color)
end
end
}):find()
end

picker:find()
end

internal.marks = function(opts)
Expand Down

0 comments on commit 7473962

Please sign in to comment.