feat: Better border configuration.

Started adding `resolve` which will allow you to do some really nice
stuff easily with options to get the window options from a variety of
inputs.

You can see how we do it in pickers.lua for borders & borderchars
currently.
This commit is contained in:
TJ DeVries
2020-09-12 14:56:58 -04:00
parent 58ab225dca
commit f449c0499c
4 changed files with 127 additions and 8 deletions

View File

@@ -79,8 +79,12 @@ That's the next step to scrolling.
width = ... width = ...
} }
--]] --]]
local get_default = require('telescope.utils').get_default
local resolver = {} local resolver = {}
local percentage_resolver = function(selector, percentage) local percentage_resolver = function(selector, percentage)
@@ -100,4 +104,52 @@ resolver.resolve_percentage_width = function(percentage)
return percentage_resolver(2, percentage) return percentage_resolver(2, percentage)
end end
--- Win option always returns a table with preview, results, and prompt.
--- It handles many different ways. Some examples are as follows:
--
-- -- Disable
-- borderschars = false
--
-- -- All three windows share the same
-- borderchars = { '─', '│', '─', '│', '┌', '┐', '┘', '└'},
--
-- -- Each window gets it's own configuration
-- borderchars = {
-- preview = {...},
-- results = {...},
-- prompt = {...},
-- }
--
-- -- Default to [1] but override with specific items
-- borderchars = {
-- {...}
-- prompt = {...},
-- }
resolver.win_option = function(val, default)
if type(val) ~= 'table' or vim.tbl_islist(val) then
if val == nil then
val = default
end
return {
preview = val,
results = val,
prompt = val,
}
elseif type(val) == 'table' then
assert(not vim.tbl_islist(val))
local val_to_set = val[1]
if val_to_set == nil then
val_to_set = default
end
return {
preview = get_default(val.preview, val_to_set),
results = get_default(val.results, val_to_set),
prompt = get_default(val.prompt, val_to_set),
}
end
end
return resolver return resolver

View File

@@ -3,6 +3,7 @@ local popup = require('popup')
local actions = require('telescope.actions') local actions = require('telescope.actions')
local config = require('telescope.config') local config = require('telescope.config')
local resolve = require('telescope.config.resolve')
local layout_strategies = require('telescope.pickers.layout_strategies') local layout_strategies = require('telescope.pickers.layout_strategies')
local log = require('telescope.log') local log = require('telescope.log')
local mappings = require('telescope.mappings') local mappings = require('telescope.mappings')
@@ -134,28 +135,28 @@ function Picker:new(opts)
end end
function Picker:_get_initial_window_options(prompt_title) function Picker:_get_initial_window_options(prompt_title)
local popup_border = self.window.border local popup_border = resolve.win_option(self.window.border)
local popup_borderchars = self.window.borderchars local popup_borderchars = resolve.win_option(self.window.borderchars)
local preview = { local preview = {
title = 'Preview', title = 'Preview',
border = popup_border, border = popup_border.preview,
borderchars = popup_borderchars, borderchars = popup_borderchars.preview,
enter = false, enter = false,
highlight = false highlight = false
} }
local results = { local results = {
title = 'Results', title = 'Results',
border = popup_border, border = popup_border.results,
borderchars = popup_borderchars, borderchars = popup_borderchars.results,
enter = false, enter = false,
} }
local prompt = { local prompt = {
title = prompt_title, title = prompt_title,
border = popup_border, border = popup_border.prompt,
borderchars = popup_borderchars, borderchars = popup_borderchars.prompt,
enter = true enter = true
} }

View File

@@ -0,0 +1,58 @@
RELOAD('telescope')
local resolve = require('telescope.config.resolve')
local eq = function(a, b)
if a ~= b then
error(string.format(
"Expected a == b, got: %s and %s", vim.inspect(a), vim.inspect(b)
))
end
end
local opt = nil
local height_config = 0.8
opt = resolve.win_option(height_config)
eq(height_config, opt.preview)
eq(height_config, opt.prompt)
eq(height_config, opt.results)
opt = resolve.win_option(nil, height_config)
eq(height_config, opt.preview)
eq(height_config, opt.prompt)
eq(height_config, opt.results)
local table_val = {'a'}
opt = resolve.win_option(nil, table_val)
eq(table_val, opt.preview)
eq(table_val, opt.prompt)
eq(table_val, opt.results)
local prompt_override = {'a', prompt = 'b'}
opt = resolve.win_option(prompt_override)
eq('a', opt.preview)
eq('a', opt.results)
eq('b', opt.prompt)
local all_specified = {preview = 'a', prompt = 'b', results = 'c'}
opt = resolve.win_option(all_specified)
eq('a', opt.preview)
eq('b', opt.prompt)
eq('c', opt.results)
local some_specified = {prompt = 'b', results = 'c'}
opt = resolve.win_option(some_specified, 'a')
eq('a', opt.preview)
eq('b', opt.prompt)
eq('c', opt.results)
-- local true_table = {true}
-- opt = resolve.win_option(some_specified, 'a')
-- eq('a', opt.preview)
-- eq('b', opt.prompt)
-- eq('c', opt.results)
print("DONE!")

View File

@@ -5,6 +5,14 @@ highlight default link TelescopeSelection Visual
" "Normal" in the floating windows created by telescope. " "Normal" in the floating windows created by telescope.
highlight default link TelescopeNormal Normal highlight default link TelescopeNormal Normal
" Border highlight groups.
" Use TelescopeBorder to override the default.
" Otherwise set them specifically
highlight default link TelescopeBorder TelescopeNormal
highlight default link TelescopePromptBorder TelescopeBorder
highlight default link TelescopeResultsBorder TelescopeBorder
highlight default link TelescopePreviewBorder TelescopeBorder
" This is like "<C-R>" in your terminal. " This is like "<C-R>" in your terminal.
" To use it, do `cmap <C-R> <Plug>(TelescopeFuzzyCommandSearch) " To use it, do `cmap <C-R> <Plug>(TelescopeFuzzyCommandSearch)