forked from ghillb/cybu.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lua
94 lines (75 loc) · 2.32 KB
/
utils.lua
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
local c = require("cybu.config")
local has_plenary, strings = pcall(require, "plenary.strings")
---@class CybuUtils
local utils = {}
utils.strlen = function(str)
return #str:gsub("[\128-\191]", "")
end
-- got this from the awesome telescope/utils
local load_once = function(f)
local resolved = nil
return function(...)
if resolved == nil then
resolved = f()
end
---@diagnostic disable-next-line: need-check-nil
return resolved(...)
end
end
local function adjust_absolute_path_head_for_windows(path)
return path:sub(1, 1) .. ":" .. path:sub(2, -1)
end
local function has_windows()
return vim.fn.has("win32") == 1
end
utils.get_icon_or_separator = load_once(function()
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
if has_devicons then
if not devicons.has_loaded() then
devicons.setup()
end
return function(filename, devicons_enabled)
if not devicons_enabled or not filename then
return { text = c.opts.style.separator }
end
local icon, highlight = devicons.get_icon(filename, string.match(filename, "%a+$"), { default = true })
-- truncate some ambiwidth icons when plenary is installed
if has_plenary and c.opts.style.devicons.truncate and (strings.strdisplaywidth(icon) > 1) then
icon = strings.truncate(icon, 1)
end
if c.opts.style.devicons.colored then
return { text = icon, highlight = highlight }
else
return { text = icon }
end
end
else
return function(_, _)
return { text = c.opts.style.separator }
end
end
end)
function utils.shorten_path(path)
local Path = require("plenary.path")
path = Path:new(path)
local shortened_path = path:shorten(1)
if has_windows() and path:is_absolute() then
return adjust_absolute_path_head_for_windows(shortened_path)
else
return shortened_path
end
end
function utils.get_relative_path(path, cwd_path)
if has_windows() then
cwd_path = cwd_path:gsub("\\", "/")
path = path:gsub("\\", "/")
end
local Path = require("plenary.path")
return Path:new(path):make_relative(cwd_path)
end
function utils.is_filter_active()
return vim.tbl_contains(c.opts.exclude, vim.bo.filetype)
or vim.tbl_contains({ "nofile" }, vim.bo.buftype)
or (not vim.bo.buflisted and c.opts.filter.unlisted)
end
return utils