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
|
#!/usr/bin/env lua
--
-- A simple program to display which places are currently open
--
-- Usage: nowopen [-r|--random] [-p|--plain] [-w|--where] [-e|--edit] [-h|--help] [tag1 tag2...]
--
-- Establishments matching any of the specified tags (prefix matching is used)
-- and the amount of time till they close will be printed.
--
-- Business hours will be looked up in ~/.local/share/nowopen/businesshours or
-- ~/.businesshours and have the format:
--
-- Joe's Diner (restaurant, diner):
-- 6am-1am
-- fri-sat: 6am-3am
-- sun: closed
--
local XDG_DATA_HOME = os.getenv("XDG_DATA_HOME") or "~/.local/share"
if arg[1] == '-w' or arg[1] == '--where' then
print(XDG_DATA_HOME..'/nowopen/businesshours')
os.exit(0)
elseif arg[1] == '-e' or arg[1] == '--edit' then
local editor = os.getenv("EDITOR") or "vim"
os.execute(editor.." "..XDG_DATA_HOME..'/nowopen/businesshours')
os.exit(0)
end
local f = io.open(XDG_DATA_HOME..'/nowopen/businesshours') or io.open('~/.businesshours')
if not f then
print("Could not find config file.\n"
.."Please create a config file in either "
..XDG_DATA_HOME.."/nowopen/businesshours or ~/.businesshours\n"
.."For an example, see the 'example_businesshours' file included with nowopen.")
os.exit(1)
end
local place_text = f:read("*a")
f:close()
local raw_print = false
local randomize = false
for i=#arg,1,-1 do
if arg[i] == "-p" or arg[i] == "--plain" then
raw_print = true
table.remove(arg, i)
elseif arg[i] == "-r" or arg[i] == "--random" then
randomize = true
table.remove(arg, i)
elseif arg[i] == "-h" or arg[i] == "--help" then
print([[
nowopen: show which businesses are now open.
Usage: nowopen [-p|--plain] [-r|--random] [-w|--where] [-e|--edit] [-h|--help] [tags...]
-p: Print plain text to stdout
-r: Randomly pick one available option
-w: Print where the config file is and exit
-e: Edit the nowopen file and exit
--help: display this message
tags: if provided, only show businesses that match one of the given tags
]])
os.exit(0)
end
end
local today = os.date("*t")
local now = os.time(today)
local function hours(t)
return math.floor(t/3600)
end
local function mins(t)
return math.floor((t/60) % 60)
end
local function secs(t)
return math.floor(t % 60)
end
local re = require('re')
local dsl = re.compile([=[
file <- {| (place / ws? comment? %nl)* |} {.+}?
place <- {|
{:name: {word (ws word)*} :}
ws? ("(" ws? {:tags: {| {tag} (ws? "," ws? {tag})* ws? |} :} ")")? ws? ":"
({:times: {|
((ws? comment? %nl)+ (%tab / " "^+2) ws?
{| days ws? ("closed" / (time_range (ws* "," ws* time_range)*)) |})+
|} :})
|}
comment <- "#" [^%nl]*
days <-
({:from: {[a-zA-Z]+} :} ws? "-" ws? {:to: {[a-zA-Z]+} :} ws? ":")
/({:from: {[a-zA-Z]+} :} ws? ":")
/({:from:''->'sun':} {:to:''->'sat':})
time_range <- {| {:open: time :} ws? "-" ws? {:close: time :} |}
time <- {|
{:hour: {[0-9]+} :} (":" {:minute: {[0-9]+} :})? ws? {:ampm: { "am" / "pm"} :}
/ ("noon" {:hour: {~''->'12'~} :} {:ampm: {~''->'pm'~} :})
/ ("midnight" {:hour: {~''->'12'~} :} {:ampm: {~''->'am'~} :})
|}
tag <- word (ws word)*
word <- [^%nl%tab (),:#-]+
ws <- [ %tab]+
]=], {tab="\t"})
local places,err = dsl:match(place_text)
if err then
print("Failed to parse config file:")
print(place_text:sub(1,#place_text-#err).."\x1b[31;7m"..err.."\x1b[0m")
os.exit(1)
end
local weekdays = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"}
local function get_weekday(str)
for i,w in ipairs(weekdays) do
if w:sub(1,#str):lower() == str:lower() then return i end
end
end
local options = {}
for i,place in ipairs(places) do
if arg[1] then
for _,request_tag in ipairs(arg) do
for _,tag in ipairs(place.tags) do
if tag:sub(1,#request_tag) == request_tag then goto carry_on end
end
end
goto next_place
::carry_on::
end
local today_times = nil
for _,time in ipairs(place.times) do
local from, to = get_weekday(time.from), get_weekday(time.to or time.from)
if (from <= to and from <= today.wday and today.wday <= to) -- no wraparound (e.g. mon-wed)
or (from > to and (today.wday >= from or today.wday <= to)) then -- wraparound (e.g. fri-tues)
today_times = time
end
end
local function t(time)
local hour24 = tonumber(time.hour)
local minute = tonumber(time.minute or 0)
if hour24 == 12 then
if time.ampm == 'am' then hour24 = hour24 - 12 end
else
if time.ampm == 'pm' then hour24 = hour24 + 12 end
end
local t = os.time{hour=hour24, min=minute, year=today.year, month=today.month, day=today.day}
while t < now do t = t + 24*60*60 end
return t
end
if today_times and today_times[1] then
for _, window in ipairs(today_times) do
local until_open = t(window.open) - now
local until_close = t(window.close) - now
if until_close < until_open then
table.insert(options, {name=place.name, until_close=until_close})
end
end
end
::next_place::
end
local rows, cols = 0, 0
if not raw_print then
local p = io.popen("tput cols")
cols = tonumber(p:read("*a"))
p:close()
local p = io.popen("tput lines")
rows = tonumber(p:read("*a"))
p:close()
end
local output = raw_print and io.output() or io.popen("less -r", "w")
local function displaylen(str) return utf8.len((str:gsub("\x1b%[[0-9;]*m", ""))) end
local function center(str, n) return (" "):rep((n-displaylen(str))//2)..str..(" "):rep((n-displaylen(str))//2) end
local function lpad(str, n) return (" "):rep(n-displaylen(str))..str end
local lines = {}
local colors = setmetatable({[0]="\x1b[31;1m",[1]="\x1b[33;1m"}, {__index=function() return "\x1b[32;1m" end})
if #options == 0 and not raw_print then
table.insert(lines, center("\x1b[1mSorry, nothing's open\x1b[0m", cols))
table.insert(lines, "")
table.insert(lines, center("¯\\_(ツ)_/¯", cols))
else
if randomize then
math.randomseed(os.time())
options = {options[math.random(#options)]}
else
table.sort(options, function(o1, o2) return o1.until_close < o2.until_close end)
end
local max_line = 0
for _,option in ipairs(options) do
local line = ("%s: %2s:%02d left"):format(
option.name, hours(option.until_close), mins(option.until_close))
if not raw_print then line = " "..colors[hours(option.until_close)]..line.."\x1b[0m " end
if displaylen(line) > max_line then max_line = displaylen(line) end
table.insert(lines, line)
end
if not raw_print then
for i, line in ipairs(lines) do lines[i] = center(lpad(line, max_line), cols) end
if randomize then
table.insert(lines, 1, center("\x1b[7m"..center("Your Random Selection", max_line).."\x1b[0m", cols))
else
table.insert(lines, 1, center("\x1b[7m"..center("Open Now", max_line).."\x1b[0m", cols))
end
table.insert(lines, 2, "")
end
end
if not raw_print then output:write(("\n"):rep((rows-#lines)//2)) end
output:write(table.concat(lines, "\n"))
if not raw_print then output:write(("\n"):rep((rows-#lines)//2)) end
if raw_print then output:write("\n") end
output:close()
|