-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfortune.lua
46 lines (39 loc) · 1.2 KB
/
fortune.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
---@module fortune
---@author Jzavrk
local M = {
fortune_list = {},
printable_list = "",
}
--- List possible categories
function M.init()
local categories = assert(io.popen("fortune -f 2>&1 1> /dev/null"), "fortune is missing")
-- Discard first line (path to fortune_list)
local _ = categories:read("l")
for it in categories:lines("l") do
local category = string.match(it, "[-_%a]+")
table.insert(M.fortune_list, category)
M.fortune_list[category] = true
M.printable_list = M.printable_list .. category .. "\n"
end
io.close(categories)
end
--- Get fortune by category
---@param category string
---@return string|nil,string|nil category name and fortune body
function M.get_fortune(category)
local handler = assert(io.popen("fortune -a " .. category), "fortune is missing")
local fortune = handler:read("a")
io.close(handler)
return category, fortune
end
--- Get random fortune
---@return string,string category and fortune body
function M.get_random_fortune()
local handler = assert(io.popen("fortune -ac"), "fortune is missing")
local category = handler:read("l"):match("[-_%a]+")
local _ = handler:read("l")
local fortune = handler:read("a")
io.close(handler)
return category, fortune
end
return M