-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_helper.lua
396 lines (313 loc) · 12.7 KB
/
file_helper.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
local expect = require "cc.expect".expect --[[@as fun(arg_n: number, value: any, ...: string)]]
---@class file_helper
local file = {
working_directory = fs.getDir(shell.getRunningProgram())
}
--- Return a table of lines from a file.
---@param filename string The file to be read.
---@param default string[]? The value returned when the file does not exist.
---@return string[] lines
function file:get_lines(filename, default)
if type(self) ~= "table" then -- shift arguments, not instanced.
default = filename --[[@as string[]?]]
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string")
expect(2, default, "table", "nil")
local lines = {}
if not fs.exists(fs.combine(self.working_directory, filename)) then
return default or {n = 0}
end
for line in io.lines(fs.combine(self.working_directory, filename)) do
table.insert(lines, line)
end
lines.n = #lines
return lines
end
--- Return a string containing the entirety of the file read.
---@param filename string The file to be read.
---@param default string? The value returned when the file does not exist.
---@return string data
function file:get_all(filename, default)
if type(self) ~= "table" then -- shift arguments, not instanced.
default = filename --[[@as string?]]
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string")
expect(2, default, "string", "nil")
local h = io.open(fs.combine(self.working_directory, filename), 'r')
if not h then
return default or ""
end
local data = h:read "*a" ---@diagnostic disable-line guh
h:close()
return data
end
--- Write data to a file
---@param filename string The file to write to.
---@param data string The data to write.
function file:write(filename, data)
if type(self) ~= "table" then -- shift arguments, not instanced.
data = filename --[[@as string]]
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string")
expect(2, data, "string")
local h, err = io.open(fs.combine(self.working_directory, filename), 'w')
if not h then
error(("Failed to open '%s' for writing: %s"):format(fs.combine(self.working_directory, filename), err), 2)
end
h:write(data):close()
end
--- Append data to a file
---@param filename string The file to write to.
---@param data string The data to write.
function file:append(filename, data)
if type(self) ~= "table" then -- shift arguments, not instanced.
data = filename --[[@as string]]
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string")
expect(2, data, "string")
local h, err = io.open(fs.combine(self.working_directory, filename), 'a')
if not h then
error(("Failed to open '%s' for writing: %s"):format(fs.combine(self.working_directory, filename), err), 2)
end
h:write(data):close()
end
--- Create an empty file (or empty the contents of an existing file).
---@param filename string? The file to write to.
function file:empty(filename)
if type(self) ~= "table" then -- shift arguments, not instanced.
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string", "nil")
filename = filename or ""
fs.delete(fs.combine(self.working_directory, filename))
local h, err = io.open(fs.combine(self.working_directory, filename), 'w')
if not h then
error(("Failed to open '%s' for writing: %s"):format(fs.combine(self.working_directory, filename), err), 2)
end
h:close()
end
--- Return the unserialized contents of the file read.
---@param filename string The file to be read.
---@param default any The value returned when the file does not exist.
---@return any data
function file:unserialize(filename, default)
if type(self) ~= "table" then -- shift arguments, not instanced.
default = filename --[[@as any]]
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string")
-- No expect for default, any type allowed.
local h = io.open(fs.combine(self.working_directory, filename), 'r')
if not h then
return default
end
local data = textutils.unserialise(h:read "*a") ---@diagnostic disable-line guh
h:close()
return data
end
--- Write data to a file
---@param filename string The file to write to.
---@param data any The data to write, this will be serialized.
---@param minify boolean? Whether or not to minify the serialized data.
function file:serialize(filename, data, minify)
if type(self) ~= "table" then -- shift arguments, not instanced.
minify = data --[[@as boolean?]]
data = filename --[[@as any]]
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string")
-- No expect for data, any type allowed.
expect(3, minify, "boolean", "nil")
local h, err = io.open(fs.combine(self.working_directory, filename), 'w')
if not h then
error(("Failed to open '%s' for writing: %s"):format(fs.combine(self.working_directory, filename), err), 2)
end
---@diagnostic disable-next-line ITS FINE
h:write(textutils.serialize(data, {compact = minify and true or false})):close()
end
--- Create an instance of the file helper with a different working directory.
---@param working_directory string? The working directory to use.
---@return file_helper file
function file:instanced(working_directory)
if type(self) ~= "table" then -- shift arguments, not instanced.
working_directory = self --[[@as string?]]
self = file --[[@as file_helper]]
end
local new_helper = {
working_directory = fs.combine(self.working_directory, working_directory)
}
return setmetatable(new_helper, {__index = file})
end
-- ####################################################################
-- # The following functions are shorthands for fs library functions. #
-- ####################################################################
--- Check if a file exists in the working directory. Shorthand for fs.exists(fs.combine(self.working_directory, filename)).
---@param filename string? The file to check.
---@return boolean exists
function file:exists(filename)
if type(self) ~= "table" then -- shift arguments, not instanced.
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string", "nil")
filename = filename or ""
return fs.exists(fs.combine(self.working_directory, filename))
end
--- Delete a file in the working directory. This is a shorthand for fs.delete(fs.combine(self.working_directory, filename)).
---@param filename string? The file to delete.
function file:delete(filename)
if type(self) ~= "table" then -- shift arguments, not instanced.
filename = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string", "nil")
filename = filename or ""
fs.delete(fs.combine(self.working_directory, filename))
end
--- List the contents of a directory. This is a shorthand for fs.list(fs.combine(self.working_directory, directory)).
---@param directory string? The directory to list.
---@return string[] files
function file:list(directory)
if type(self) ~= "table" then -- shift arguments, not instanced.
directory = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, directory, "string", "nil")
directory = directory or ""
return fs.list(fs.combine(self.working_directory, directory))
end
--- Check if the path given is a directory. This is a shorthand for fs.isDir(fs.combine(self.working_directory, path)).
---@param path string? The path to check.
---@return boolean is_directory
function file:is_directory(path)
if type(self) ~= "table" then -- shift arguments, not instanced.
path = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, path, "string", "nil")
path = path or ""
return fs.isDir(fs.combine(self.working_directory, path))
end
--- Open a file normally, alias to `fs.open(fs.combine(self.working_directory, filename), mode)`.
---@param filename string The file to open.
---@param mode string The mode to open the file in.
---@return ReadHandle|WriteHandle|BinaryReadHandle|BinaryWriteHandle|nil handle The file handle, or nil if it could not be opened.
function file:open(filename, mode)
if type(self) ~= "table" then -- shift arguments, not instanced.
filename = self --[[@as string]]
mode = filename --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, filename, "string")
expect(2, mode, "string")
return fs.open(fs.combine(self.working_directory, filename), mode)
end
--- Check if a file is read-only. This is a shorthand for fs.isReadOnly(fs.combine(self.working_directory, filename)).
---@param path string? The file path to check.
---@return boolean is_read_only
function file:is_read_only(path)
if type(self) ~= "table" then -- shift arguments, not instanced.
path = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, path, "string", "nil")
path = path or ""
return fs.isReadOnly(fs.combine(self.working_directory, path))
end
--- Get the directory a file is stored in. This is a shorthand for fs.getDir(fs.combine(self.working_directory, filename)).
---@param path string? The file path to check.
---@return string directory
function file:get_dir(path)
if type(self) ~= "table" then -- shift arguments, not instanced.
path = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, path, "string", "nil")
path = path or ""
return fs.getDir(fs.combine(self.working_directory, path))
end
--- Get the name of a file. This is a shorthand for fs.getName(fs.combine(self.working_directory, filename)).
---@param path string? The file path to check.
---@return string name
function file:get_name(path)
if type(self) ~= "table" then -- shift arguments, not instanced.
path = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, path, "string", "nil")
path = path or ""
return fs.getName(fs.combine(self.working_directory, path))
end
--- Get the size of a file. This is a shorthand for fs.getSize(fs.combine(self.working_directory, filename)).
---@param path string? The file path to check.
---@return integer size The size of the file, in bytes.
function file:get_size(path)
if type(self) ~= "table" then -- shift arguments, not instanced.
path = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, path, "string", "nil")
path = path or ""
return fs.getSize(fs.combine(self.working_directory, path))
end
--- Get the free space in the given directory. This is a shorthand for fs.getFreeSpace(fs.combine(self.working_directory, path)).
---@param path string? The directory to check.
---@return integer|"unlimited" free_space The free space in the directory, in bytes.
function file:get_free_space(path)
if type(self) ~= "table" then -- shift arguments, not instanced.
path = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, path, "string", "nil")
path = path or ""
return fs.getFreeSpace(fs.combine(self.working_directory, path))
end
--- Make a directory in the working directory. This is a shorthand for fs.makeDir(fs.combine(self.working_directory, path)).
---@param path string? The directory to create.
function file:make_dir(path)
if type(self) ~= "table" then -- shift arguments, not instanced.
path = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, path, "string", "nil")
path = path or ""
fs.makeDir(fs.combine(self.working_directory, path))
end
--- Move a file in the working directory. This is a shorthand for fs.move(fs.combine(self.working_directory, from), fs.combine(self.working_directory, to)).
---@param from string The file to move.
---@param to string The destination of the file.
function file:move(from, to)
if type(self) ~= "table" then -- shift arguments, not instanced.
to = from --[[@as string]]
from = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, from, "string")
expect(2, to, "string")
fs.move(fs.combine(self.working_directory, from), fs.combine(self.working_directory, to))
end
--- Copy a file in the working directory. This is a shorthand for fs.copy(fs.combine(self.working_directory, from), fs.combine(self.working_directory, to)).
---@param from string The file to copy.
---@param to string The destination of the file.
function file:copy(from, to)
if type(self) ~= "table" then -- shift arguments, not instanced.
to = from --[[@as string]]
from = self --[[@as string]]
self = file --[[@as file_helper]]
end
expect(1, from, "string")
expect(2, to, "string")
fs.copy(fs.combine(self.working_directory, from), fs.combine(self.working_directory, to))
end
return file