forked from iGio90/filemanager-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
105 lines (90 loc) · 3.46 KB
/
utils.lua
File metadata and controls
105 lines (90 loc) · 3.46 KB
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
95
96
97
98
99
100
101
102
103
104
105
local config = import('micro/config')
local micro = import('micro')
local golib_ioutil = import('ioutil')
local shell = import('micro/shell')
local buffer = import('micro/buffer')
local filepath = import('path/filepath')
local icon = dofile(config.ConfigDir .. '/plug/filemanager/icon.lua')
function Icons()
return icon.Icons()
end
local function get_panes_quantity(tab)
return #tab.Panes
end
-- Returns a list of files (in the target dir) that are ignored by the VCS system (if exists)
-- aka this returns a list of gitignored files (but for whatever VCS is found)
local function get_ignored_files(tar_dir)
local icons = Icons()
-- True/false if the target dir returns a non-fatal error when checked with 'git status'
local function has_git()
local git_rp_results = shell.ExecCommand('git -C "' .. tar_dir .. '" rev-parse --is-inside-work-tree')
return git_rp_results:match('^true%s*$')
end
local readout_results = {}
-- TODO: Support more than just Git, such as Mercurial or SVN
if has_git() then
-- If the dir is a git dir, get all ignored in the dir
local git_ls_results =
shell.ExecCommand('git -C "' .. tar_dir .. '" ls-files . --ignored --exclude-standard --others --directory')
-- Cut off the newline that is at the end of each result
for split_results in string.gmatch(git_ls_results, '([^\r\n]' .. icons['dir'] .. ')') do
-- git ls-files adds a trailing slash if it's a dir, so we remove it (if it is one)
readout_results[#readout_results + 1] = (
string.sub(split_results, -1) == '/' and string.sub(split_results, 1, -2) or split_results
)
end
end
-- Make sure we return a table
return readout_results
end
-- Appends the elements of the second table to the first table
local function get_appended_tables(table1, table2)
for i = 1, #table2 do
table1[#table1 + 1] = table2[i]
end
return table1
end
-- Joins the target dir's leading path to the passed name
function dirname_and_join(path, join_name)
local leading_path = filepath.Dir(path)
return filepath.Join(leading_path, join_name)
end
-- Returns true/false if the file is a dotfile
local function is_dotfile(entry_name)
return string.sub(entry_name, 1, 1) == '.'
end
-- This function is designed to identify the position of the first character
-- in 'entry.content' that is neither a space nor an icon. This is necessary
-- because 'entry.content' can have an offset due to leading spaces or icons.
local function first_char_loc(str)
for i = 1, #str do
local char = str:sub(i, i)
if char ~= " " then
-- When this condition is true it means the icon was found
-- Adding 2 to the position accounts for the space between the icon and the
-- file name.
return i + 2
end
end
return nil
end
-- Returns the postition of the last dot of the given string not considerating the first
local function get_dot_location(str)
local position = string.find(str, "%.[^%.]*$")
return position
end
local function get_content(str)
-- Correct the starting position to account the icon which is a multi-byte character
local first_char_location = first_char_loc(str) + 3
return string.sub(str, first_char_location)
end
return {
get_ignored_files = get_ignored_files,
get_appended_tables = get_appended_tables,
dirname_and_join = dirname_and_join,
is_dotfile = is_dotfile,
get_panes_quantity = get_panes_quantity,
first_char_loc = first_char_loc,
get_dot_location = get_dot_location,
get_content = get_content
}