blob: fa3fc2142331b45d90f82439391a5ba4f8813795 (
plain)
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
|
local gdebug = require "gears.debug"
local GFile = require("lgi").Gio.File
---@class QuarrelFs
local M = {}
--- Read a file with the specified format (or "a") and close the file
---@param path string
---@param format? openmode
---@return any
function M.read(path, format)
local f = assert(io.open(path, "r"))
local c = f:read(format or "a")
f:close()
return c
end
--- Write a file with the specified format (or "a") and close the file
---@param path string
---@param format? openmode
---@return any
function M.write(content, path, format)
local f = assert(io.open(path, format or "w+"))
f:write(content)
f:close()
end
--- List files in a directory
---@param path string
---@param absolute boolean?
---@return table
function M.ls_files(path, absolute)
local files = GFile.new_for_path(path):enumerate_children("standard::*", 0)
local files_filtered = {}
if not files then
return {}
end
for file in
function()
return files:next_file()
end
do
if file:get_file_type() == "REGULAR" then
local file_name = file:get_display_name()
file_name = absolute and (path:gsub("[/]*$", "") .. "/" .. file_name) or file_name
table.insert(files_filtered, file_name)
end
end
return files_filtered
end
return M
|