-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathglob.d.lua
More file actions
191 lines (181 loc) · 5 KB
/
Copy pathglob.d.lua
File metadata and controls
191 lines (181 loc) · 5 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---@meta _
---Options for glob matching and directory traversal.
---@class mods.GlobOptions
---@field hidden? boolean Whether to include hidden files/directories.
---@field recursive? boolean Whether to traverse directories recursively.
---@field follow? boolean Whether to follow symbolic links.
---@field ignorecase? boolean Whether to perform case-insensitive matching.
---
---Glob-style matching and filesystem expansion helpers.
---
---## Usage
---
---```lua
---local mods = require "mods"
---local glob = mods.glob
---
---print(glob.match("src/mods/fs.lua", "**/*.lua")) --> true
---print(glob.match("DATA.TXT", "*.txt", true)) --> true
---print(glob.filter({ "a.lua", "b.txt" }, "*.lua")[1]) --> "a.lua"
---print(glob.glob("src", "*.lua")[1])
---```
---
---## Supported wildcards
---
---* `*`: match zero or more characters within one path segment.
---
--- ```lua
--- match("main.lua", "*.lua")
--- ```
---
---* `?`: match exactly one character within one path segment.
---
--- ```lua
--- match("a1.lua", "a?.lua")
--- ```
---
---* `[]`: match one character from a bracket class like `[a-z]`.
---
--- ```lua
--- match("file7.lua", "file[0-9].lua")
--- ```
---
---* `[!]`: negate a bracket class, like `[!0-9]`.
---
--- ```lua
--- match("filex.lua", "file[!0-9].lua")
--- ```
---
---* `{a,b}`: match one of several brace alternatives.
---
--- ```lua
--- match("init.lua", "init.{lua,luac}")
--- ```
---
---* `**`: match across path segments recursively.
---
--- ```lua
--- match("src/mods/fs.lua", "**/*.lua")
--- ```
---
---@class mods.glob
local M = {}
---
---Match a path against a glob pattern.
---
---```lua
---glob.match("src/mods/fs.lua", "**/*.lua") --> true
---```
---
---@section Glob Operations
---@param path string Input path.
---@param pattern string Input glob pattern.
---@param ignorecase? boolean Override platform-default case matching.
---@return boolean matches True when the path matches the pattern.
---@nodiscard
function M.match(path, pattern, ignorecase) end
---
---Translate one glob segment into an equivalent Lua pattern.
---
---```lua
---local s = "init.lua"
---local pattern = "*.lua"
---local matches = glob.match(s, pattern)
---local translated_matches = s:match(glob.translate(pattern)) ~= nil
---print(matches == translated_matches) --> true
---```
---
---> [!NOTE]
--->
---> * `*` and `?` stay within a single path segment.
--->
---> ```lua
---> local pattern = "*.txt"
---> print(glob.translate(pattern)) --> "^[^/]*%.txt$"
---> print(glob.match("foo/bar.txt", pattern)) --> false
---> ```
--->
---> * `**` and `{a,b}` need higher-level matching logic.
--->
---> ```lua
---> pattern = "src/{x,y}.lua"
---> print(("src/x.lua"):match(glob.translate(pattern))) --> nil
---> print(glob.match("src/x.lua", pattern)) --> true
---> ```
---
---@section Glob Operations
---@param pattern string Input glob segment.
---@return string lua_pattern Lua pattern string.
---@nodiscard
function M.translate(pattern) end
---Return whether a pattern contains glob metacharacters.
---
---```lua
---glob.has_magic("foo.txt") --> false
---glob.has_magic("*.txt") --> true
---```
---
---@section Glob Operations
---@param s string Input string.
---@return boolean has_magic True when the string contains glob syntax.
---@nodiscard
function M.has_magic(s) end
---
---Escape glob metacharacters in a literal string.
---
---```lua
---glob.escape("a*b") --> "a\\*b"
---```
---
---@section Glob Operations
---@param s string Input literal string.
---@return string pattern Escaped glob pattern.
---@nodiscard
function M.escape(s) end
---
---Return the values from `names` that match the glob pattern.
---
---```lua
---glob.filter({ "a.lua", "b.txt", "c.lua" }, "*.lua") --> { "a.lua", "c.lua" }
---```
---
---@section Glob Operations
---@param names string[] Input names.
---@param pattern string Input glob pattern.
---@param ignorecase? boolean Override platform-default case matching.
---@return mods.List<string> matches Matching values from `names`.
---@nodiscard
function M.filter(names, pattern, ignorecase) end
---
---Return glob matches under `path`.
---
---```lua
---print(glob.glob("src", "*.lua"))
---print(glob.glob("src", "*.lua", { recursive = true }))
---```
---
---@section Glob Operations
---@param path string Input path.
---@param pattern? string Optional pattern to match.
---@param opts? mods.GlobOptions Optional glob options.
---@return mods.List<string> paths Matching paths under `path`.
---@nodiscard
function M.glob(path, pattern, opts) end
---
---Iterator over glob matches under `path`.
---
---```lua
---for path in glob.iglob("src", "*.lua") do
--- print(path)
---end
---```
---
---@section Glob Operations
---@param path string Input path.
---@param pattern? string Optional pattern to match.
---@param opts? mods.GlobOptions Optional glob options.
---@return (fun(state:table, prev?:string): (path:string?)) iterator Iterator function.
---@return table state Iterator state table.
---@return nil initial Initial iterator value.
function M.iglob(path, pattern, opts) end
return M