| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
---------------------- |
| 4 |
-- HELPER_FUNCTIONS -- |
| 5 |
---------------------- |
| 6 |
local function check_installed(prog) |
| 7 |
local r = os.execute("which " .. prog .. "&> /dev/null") |
| 8 |
return r |
| 9 |
end |
| 10 |
|
| 11 |
local function help() |
| 12 |
print("Displays a dmenu prompt with the last, current and next events and copies location/url [+password] to the clipboard") |
| 13 |
print() |
| 14 |
print("USAGE:") |
| 15 |
print(string.format("\t%s dataFile [ignoreType ...]", arg[0])) |
| 16 |
print() |
| 17 |
print("OPTIONS:") |
| 18 |
print() |
| 19 |
print("ARGS:") |
| 20 |
print(string.format("\t%-12s\t%s", "<dataFile>", "path to the input csv file with 'start', 'end', 'title', 'location', 'password', 'type' headers (not written in the file)")) |
| 21 |
print(string.format("\t%-12s\t%s", "", "location may contain '\\href{location}{url}' in which casethe two fields are extracted")) |
| 22 |
print(string.format("\t%-12s\t%s", "<ignoreType>", "entries with that type are ignored")) |
| 23 |
end |
| 24 |
|
| 25 |
local function dur2str(dur) |
| 26 |
return string.format("%02d:%02d", dur//60, dur%60) |
| 27 |
end |
| 28 |
|
| 29 |
local function contains(t, ele) |
| 30 |
for _,v in pairs(t) do |
| 31 |
if v == ele then return true end |
| 32 |
end |
| 33 |
return false |
| 34 |
end |
| 35 |
|
| 36 |
------------------- |
| 37 |
-- PREREQUISITES -- |
| 38 |
------------------- |
| 39 |
if not check_installed("dmenu") then |
| 40 |
print("Error: dmenu has to be installed (options -l, -i and -p required, but these are in the default anyway)") |
| 41 |
os.exit(-1) |
| 42 |
end |
| 43 |
if not check_installed("xsel") then |
| 44 |
print("Error: xsel has to be installed") |
| 45 |
os.exit(-1) |
| 46 |
end |
| 47 |
|
| 48 |
if #arg < 1 then |
| 49 |
print("Error: At least one argument has to be given") |
| 50 |
print() |
| 51 |
help() |
| 52 |
os.exit(-1) |
| 53 |
end |
| 54 |
if arg[1] == "-h" or arg[1] == "--help" then |
| 55 |
help() |
| 56 |
os.exit(0) |
| 57 |
end |
| 58 |
|
| 59 |
local type_exclude = {} |
| 60 |
for i=1,#arg do |
| 61 |
table.insert(type_exclude, arg[i]) |
| 62 |
end |
| 63 |
|
| 64 |
----------- |
| 65 |
-- MAIN -- |
| 66 |
---------- |
| 67 |
|
| 68 |
local maxes = {title=0, location=0, type=0} |
| 69 |
|
| 70 |
local before = {diff=25*60*7} |
| 71 |
local after = {diff=25*60*7} |
| 72 |
|
| 73 |
local c = {} |
| 74 |
|
| 75 |
local current = os.date("*t") |
| 76 |
local today = tonumber(os.date("%u")) - 1 |
| 77 |
current.minutes = current.min + (today*24 + current.hour)*60 |
| 78 |
|
| 79 |
local inputs = loadfile(arg[1], "t", {})() -- easy loading comes with the disadvantage that arbitrary code which is in that file gets executed |
| 80 |
for _,ele in ipairs(inputs) do |
| 81 |
if not contains(type_exclude, ele["type"]) then |
| 82 |
ele.start, ele["end"] = tonumber(ele.start), tonumber(ele["end"]) |
| 83 |
ele.url = ele.location:match([[\href ?{(.*)}{.*}]]) |
| 84 |
ele.location = ele.location:match([[\href ?{.*}{(.*)}]]) or ele.location |
| 85 |
|
| 86 |
if maxes.title < #ele.title then maxes.title = #ele.title end |
| 87 |
if maxes.location < #ele.location then maxes.location = #ele.location end |
| 88 |
if maxes.type < #ele.type then maxes.type = #ele.type end |
| 89 |
|
| 90 |
if current.minutes > ele.start and current.minutes < ele["end"] then |
| 91 |
ele.diff = current.minutes-ele["start"] |
| 92 |
table.insert(c, ele) |
| 93 |
elseif current.minutes < ele.start then |
| 94 |
if ele.start - current.minutes < before.diff then |
| 95 |
before = {diff=ele.start-current.minutes} |
| 96 |
end |
| 97 |
if ele.start - current.minutes <= before.diff then |
| 98 |
ele.diff = before.diff |
| 99 |
table.insert(before, ele) |
| 100 |
end |
| 101 |
elseif current.minutes > ele["end"] then |
| 102 |
if current.minutes - ele["end"] < after.diff then |
| 103 |
after = {diff=current.minutes-ele["end"]} |
| 104 |
end |
| 105 |
if current.minutes - ele["end"] <= after.diff then |
| 106 |
ele.diff = after.diff |
| 107 |
table.insert(after, ele) |
| 108 |
end |
| 109 |
end |
| 110 |
end |
| 111 |
end |
| 112 |
|
| 113 |
maxes = {title=maxes.title < 100 and maxes.title or 99, location=maxes.location < 100 and maxes.location or 99, type=maxes.type < 100 and maxes.type or 99} |
| 114 |
|
| 115 |
local dmenu_in,map = {},{} |
| 116 |
for _,x in ipairs(after) do |
| 117 |
local s = string.format("%-".. maxes.title+1+maxes.type .."s %-".. maxes.location+2 .."s # ended %s ago", x.title.."-"..x.type, "("..x.location..")", dur2str(x.diff)) |
| 118 |
table.insert(dmenu_in, s) |
| 119 |
map[s] = x |
| 120 |
end |
| 121 |
|
| 122 |
for _,x in ipairs(c) do |
| 123 |
local s = string.format("%-".. maxes.title+1+maxes.type .."s %-".. maxes.location+2 .."s # began %s ago", x.title.."-"..x.type, "("..x.location..")", dur2str(x.diff)) |
| 124 |
table.insert(dmenu_in, s) |
| 125 |
map[s] = x |
| 126 |
end |
| 127 |
|
| 128 |
for _,x in ipairs(before) do |
| 129 |
local s = string.format("%-".. maxes.title+1+maxes.type .."s %-".. maxes.location+2 .."s # begins in %s", x.title.."-"..x.type, "("..x.location..")", dur2str(x.diff)) |
| 130 |
table.insert(dmenu_in, s) |
| 131 |
map[s] = x |
| 132 |
end |
| 133 |
|
| 134 |
local dmenu_ins = table.concat(dmenu_in, "\n") |
| 135 |
|
| 136 |
print(dmenu_ins) |
| 137 |
-- TODO check for forbidden strings in dmenu_in |
| 138 |
local proc = io.popen(string.format("echo '%s' | dmenu -i -l %d -p 'Select entry'", dmenu_ins, #dmenu_in), "r") |
| 139 |
local dmenu_out = proc:read("l") |
| 140 |
if not dmenu_out then |
| 141 |
os.execute("notify-send 'Uni' 'Nothing selected'") |
| 142 |
os.exit() |
| 143 |
end |
| 144 |
|
| 145 |
local sel = map[dmenu_out] |
| 146 |
assert(sel, "Error building/reading the lookup table") |
| 147 |
if sel.url and sel.url ~= "" then |
| 148 |
os.execute(string.format("echo -n '%s' | xsel -b && notify-send -h string:x-canonical-private-synchronous:uni 'Uni' 'Copied URL to clipboard'", sel.url)) |
| 149 |
elseif sel.location and sel.location ~= "" then |
| 150 |
os.execute(string.format("echo -n '%s' | xsel -b && notify-send -h -- string:x-canonical-private-synchronous:uni 'Uni' 'Copied URL to clipboard'", sel.location)) |
| 151 |
os.execute(string.format("echo -n 'https://portal.mytum.de/campus/roomfinder/search_room_results?searchstring=%s&building=Alle&search=Suche+starten' | xsel -b && notify-send -h string:x-canonical-private-synchronous:uni 'Uni' 'Copied Roomfinder-url to clipboard'", sel.location)) |
| 152 |
end |
| 153 |
|
| 154 |
if sel.password and sel.password ~= "" then |
| 155 |
os.execute(string.format("sleep 3 && echo -n '%s' | xsel -b && notify-send -h string:x-canonical-private-synchronous:uni 'Uni' 'Copied password to clipboard'", sel.password)) |
| 156 |
end |