Skip to content

Commit

Permalink
path display options as enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
caojoshua committed May 16, 2021
1 parent 0048228 commit c885f9e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
8 changes: 5 additions & 3 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local types = require('telescope.types')

-- Keep the values around between reloads
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {}

Expand Down Expand Up @@ -113,9 +115,9 @@ function config.set_defaults(defaults)
set("border", {})
set("borderchars", { '', '', '', '', '', '', '', ''})

set("hide_filename", false)
set("shorten_path", false)
set("tail_path", false)
set(types.path_display_options.HIDE_PATH, false)
set(types.path_display_options.SHORTEN_PATH, false)
set(types.path_display_options.TAIL_PATH, false)

set("get_status_text", function(self)
local xx = (self.stats.processed or 0) - (self.stats.filtered or 0)
Expand Down
23 changes: 23 additions & 0 deletions lua/telescope/types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local types = {}

-- convert numbered index table into an enum
--
-- example:
-- enum { "foo", "bar" }
-- returns:
-- { foo = 1, bar = 2 }
local enum = function(table)
for i = 1, #table do
table[table[i]] = i
table[i] = nil
end
return table
end

types.path_display_options = enum {
"HIDE_PATH",
"SHORTEN_PATH",
"TAIL_PATH"
}

return types
8 changes: 5 additions & 3 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local has_devicons, devicons = pcall(require, 'nvim-web-devicons')

local pathlib = require('telescope.path')
local Job = require('plenary.job')
local types = require('telescope.types')

local utils = {}

Expand Down Expand Up @@ -149,20 +150,21 @@ end)()

utils.transform_filepath = function(opts, path)
local config = require('telescope.config').values
local types = types.path_display_options

if path == nil or utils.get_default(opts.hide_filename, config.hide_filename) then
if path == nil or utils.get_default(opts[types.HIDE_PATH], config[types.HIDE_PATH]) then
return ''
end

local transformed_path = path

if utils.get_default(opts.tail_path, config.tail_path) then
if utils.get_default(opts[types.TAIL_PATH], config[types.TAIL_PATH]) then
transformed_path = utils.path_tail(transformed_path)
else
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
transformed_path = pathlib.make_relative(transformed_path, cwd)

if utils.get_default(opts.shorten_path, config.shorten_path) then
if utils.get_default(opts[types.SHORTEN_PATH], config[types.SHORTEN_PATH]) then
transformed_path = pathlib.shorten(transformed_path)
end
end
Expand Down

0 comments on commit c885f9e

Please sign in to comment.