--!name Multi Timer
--!icon clock
--!category Tools
local ab={"Tea","Coffee","Eggs","Pasta","Rice","Pizza","Oven","Bread","Laundry","Dryer","Game turn","Workout","Focus","Break","Nap","Parking"}local aA={3,4,8,10,18,12,30,40,60,50,2,30,25,5,20,120}local J,ao,P=8,16,24*60 local f={}local S={}local T,U=0,0 local j,B,O,K,x="main",1,1,1,1 local z,i,ac,n local function M(C,bc,a7)return math.max(bc,math.min(a7,C))end local function _(a,b)return a.e and math.max(0,a.e-b)or a.l end local function X(a,b)return a.f or(a.e~=nil and a.e<=b)end local function aK(c)return(c+59)//60 end local function aG(c)local h=aK(c);return h//60 ..":"..string.format("%02d",h%60)end local function aH(h)return h//60 .." h"..(h%60>0 and" "..h%60 .." min"or"")end local function E(c)local h=aK(c)return h<60 and h.." min"or aH(h)end local function aq(a,b)local h=math.max(0,b-a.e)//60 if h<1 then return"just now"end if h<1440 then return(h<60 and h.." min"or aH(h)).." ago"end return h//1440 ..(h<2880 and" day ago"or" days ago")end local function Q(b)return b>=1700000000 end local function u(c)return os.date("%H:%M",c)end local aV={["0"]="####.##.##.####",["1"]=".#.##..#..#.###",["2"]="###..#####..###",["3"]="###..####..####",["4"]="#.##.####..#..#",["5"]="####..###..####",["6"]="####..####.####",["7"]="###..#..#..#..#",["8"]="####.#####.####",["9"]="####.####..####",}local V=19 local aZ=("#"):rep(V)..("/"..("."):rep(V)):rep(3).."/"..("#"):rep(V)local function at(c)local w={}for l=1,5 do local p={}for e=1,#c do local d=c:sub(e,e)p[e]=d==":"and(".#.#."):sub(l,l)or aV[d]:sub(l*3-2,l*3)end w[l]=table.concat(p,".")end local N=math.max(2,(V-#w[1])//2)for l=1,5 do w[l]=("."):rep(N)..w[l]..("."):rep(math.max(2,V-#w[l]-N))end return table.concat(w,"/")end local function H()local aw={"M1|"..T.."|"..table.concat(S,",")}for D,a in ipairs(f)do aw[#aw+1]=a.n.."|"..a.d.."|"..(a.f and"f"..a.e or a.e and"e"..a.e or"p"..a.l)end snail.save(table.concat(aw,"\n"))end local function bh()for e,h in ipairs(aA)do S[e]=h end local c=snail.load()if type(c)~="string"or c:sub(1,3)~="M1|"then return end local A,N=c:match("^M1|(%d+)|([%d,]*)")T=tonumber(A)or 0 local e=0 for C in(N or""):gmatch("%d+")do e=e+1 if aA[e]then S[e]=M(tonumber(C),1,P)end end for v,m,h,C in c:gmatch("\n([^|\n]+)|(%d+)|([efp])(%d+)")do if#f<J then U=U+1 local a={n=v:sub(1,ao),d=M(tonumber(m),60,P*60),k=U}if h=="p"then a.l=M(tonumber(C),1,P*60)else a.e,a.f=tonumber(C),h=="f"or nil end f[#f+1]=a end end end local function bf(s,y)local ak,al=s.f and 0 or s.e and 1 or 2,y.f and 0 or y.e and 1 or 2 if ak~=al then return ak<al end local aI=ak==0 and-s.e or ak==1 and s.e or s.l local aJ=al==0 and-y.e or al==1 and y.e or y.l if aI~=aJ then return aI<aJ end return s.k<y.k end local function L(av)av=av or f[B]table.sort(f,bf)for e,a in ipairs(f)do if a==av then B=e end end end local function ay()local b=os.time()local m=b-T local au=false if T>0 and(m<0 or m>30*86400 or(not Q(T)and Q(b)))then for D,a in ipairs(f)do if a.e then a.e=a.e+m end end au=true end T=b for D,a in ipairs(f)do if a.e and not a.f and a.e<=b then a.f,au,n=true,true,nil end end if au then L()H()end end local function ar()local b,W=os.time(),nil for D,a in ipairs(f)do local c if a.f then if b-a.e<86400 then c=60-(b-a.e)%60 end elseif a.e then c=(a.e-b)%60 if c==0 then c=60 end end if c and(not W or c<W)then W=c end end snail.tick(W and math.max(1,W)*1000+50 or 0)end local function aR(ag,bi)local function aP(c)for D,a in ipairs(f)do if a~=bi and a.n==c then return true end end end if not aP(ag)then return ag end local a1=ag:gsub(" %d+$","")for g=2,J+1 do local c=a1:sub(1,ao-#tostring(g)-1).." "..g if not aP(c)then return c end end return ag end local function aO()for D,a in ipairs(f)do if a.e and not a.f then return true end end return false end local function a2()if#f>=J then n,j,i=J.." timers is the most","main",nil return end U=U+1 local b=os.time()local a={n=aR(i.n),d=i.m*60,e=b+i.m*60,k=U}if i.p then S[i.p]=i.m end f[#f+1]=a L(a)j,i="main",nil n=a.n..": "..E(a.d)..(Q(b)and", ready at "..u(a.e)or"")H()end local function aL(a,aa)local b=os.time()if aa and a.e and not a.f then a.l,a.e=_(a,b),nil elseif not aa and a.l then a.e,a.l=b+a.l,nil end end local function ap(a,c)local b=os.time()if a.f then if c<0 then return end a.e,a.f=b+c,nil else local p=_(a,b)p=M(p+c,math.min(p,60),P*60)if a.e then a.e=b+p else a.l=p end end L()H()end local function bg(a)for e,l in ipairs(f)do if l==a then table.remove(f,e);break end end H()end local aT="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function bj(e)return aT:sub(e+1,e+1)end local aW={"1234567890<","QWERTYUIOP'","ASDFGHJKL-+","^ZXCVBNM,./","()[_____]?>"}local aX={"1234567890<",":         >"}local aY="'-+,./()?"local k local o,t,I=1,2,1 local function af()return k.num and aX or aW end local function F(a,l)return af()[l]:sub(a,a)end local function ae(q,c)local aj=q=="m"k={f=q,s=c,at=#c,num=aj,max=aj and 5 or ao,back=j}I=(c==""and not aj)and 1 or 0 if aj then o,t=1,1 elseif t==1 or t>5 then o,t=1,2 end j="kb"end local function aE()local q,as=k.f,k.back local c=(k.s:gsub("^%s+",""):gsub("%s+$",""))j,k=as,nil if q=="m"then local A,h=c:match("^(%d+):(%d+)$")local C=A and tonumber(A)*60+tonumber(h)or tonumber(c:match("^%d+$")or"")if C and C>=1 and C<=P then i.m=C elseif c~=""then n="Type minutes like 90, or 1:30"end elseif as=="timer"then if c~=""then z.n=aR(c,z)H()end elseif as=="set"then if c~=""then i.n=c end elseif c~=""then i.n,j,x=c,"set",1 end end local function aM()local d=F(o,t)local c,u=k.s,k.at if d=="<"then if u>0 then k.s,k.at=c:sub(1,u-1)..c:sub(u+1),u-1 end elseif d=="^"then I=(I+1)%3 elseif d=="["then k.at=math.max(0,u-1)elseif d=="]"then k.at=math.min(#c,u+1)elseif d==">"then aE()elseif#c>=k.max then n="That's as long as it can be"else if d=="_"then d=" "elseif I==0 then d=d:lower()end k.s,k.at=c:sub(1,u)..d..c:sub(u+1),u+1 if I==1 and d:match("%a")then I=0 end end end local function a8(g)if g=="top"then aE()elseif g=="ok"then aM()elseif g=="left"or g=="right"then local m,d=g=="left"and-1 or 1,F(o,t)local v repeat o=(o-1+m)%11+1 v=F(o,t)until v~=" "and(d~="_"or v~="_")elseif g=="up"or g=="down"then t=(t-1+(g=="up"and-1 or 1))%#af()+1 if F(o,t)==" "then for R=1,10 do if o-R>=1 and F(o-R,t)~=" "then o=o-R;break end if o+R<=11 and F(o+R,t)~=" "then o=o+R;break end end end end end function touch(ba,s,y)if ba~="board"or j~="kb"or s<1 or s>11 or y<1 or y>#af()or F(s,y)==" "then return end n=nil o,t=s,y aM()end local function a9()local az=af()local w,be={},F(o,t)=="_"for l=1,#az do local b={}for a=1,11 do local d=az[l]:sub(a,a)local Z=l==t and(a==o or(be and d=="_"))local A,N,e=Z and 1 or 0,aY:find(d,1,true)if d==" "then e=nil;d="."elseif d:match("%u")then e=Z and d:byte()-65 elseif d:match("%d")then e=Z and 26+d:byte()-48 elseif N then e=(Z and 35+N)or(d=="/"and 45)elseif d==":"then e=Z and 66 elseif d=="<"then e=46+A elseif d=="^"then e=48+I*2+A elseif d=="["then e=54+A elseif d=="]"then e=56+A elseif d=="_"then e=58+A*3+(F(a-1,l)~="_"and 0 or F(a+1,l)~="_"and 2 or 1)else e=64+A end b[a]=e and bj(e)or d=="."and"."or("{"..d.."}")end w[l]=table.concat(b)end return table.concat(w,"/")end local a0={"pause","add","restart","rename","remove"}local aU={"restart","add","rename","remove"}local function aD(a)return a.f and aU or a0 end local function aC()local b,aN,Y={"new"},false,false for D,a in ipairs(f)do if a.f then Y=true else aN=true end end if aN then b[#b+1]="all"end if Y then b[#b+1]="clear"end return b end local function bd(g,m,G)local aB=aC()local v=#f+#aB B=M(B,1,v)local a=f[B]if m then B=(B-1+m)%v+1 elseif a then if g=="ok"then j,z,O="timer",a,1 elseif G then ap(a,G*60)end elseif g=="ok"then local r=aB[B-#f]if r=="new"then if#f>=J then n=J.." timers is the most"else j,K="pick",1 end elseif r=="all"then local aa=aO()for D,l in ipairs(f)do aL(l,aa)end n=aa and"All paused"or"All running again"L()H()elseif r=="clear"then local d=0 for e=#f,1,-1 do if f[e].f then table.remove(f,e);d=d+1 end end n=d==1 and"Cleared 1 finished timer"or"Cleared "..d.." finished timers"H()end end end local function bk(g,m,G,s)local a=z local an=aD(a)O=M(O,1,#an)if m then O=(O-1+m)%#an+1 elseif g=="top"then j,z="main",nil L(a)elseif G then ap(a,G*60)elseif g=="ok"then local r=an[O]if r=="pause"then aL(a,a.e~=nil)n=a.e and"Running again"or"Paused"L()H()elseif r=="add"then ap(a,300)n="Added 5 minutes"elseif r=="restart"then a.e,a.l,a.f=os.time()+a.d,nil,nil n="Restarted: "..E(a.d)L()H()elseif r=="rename"then ae("n",a.n)elseif a.f or s=="remove"then bg(a)n,j,z=a.n.." removed","main",nil else ac="remove"end end end function key(g)ay()if j=="kb"then n=nil a8(g)elseif not(j=="main"and g=="top")then n=nil local s=ac ac=nil local m=(g=="up"and-1)or(g=="down"and 1)or nil local G=(g=="left"and-1)or(g=="right"and 1)or nil if j=="main"then bd(g,m,G)elseif j=="pick"then local v=#ab+1 if m then K=(K-1+m)%v+1 elseif g=="top"then j="main"elseif g=="ok"then if ab[K]then i,j,x={n=ab[K],m=S[K],p=K},"set",1 else i={n="",m=10}ae("n","")end end elseif j=="set"then if m then x=(x-1+m)%4+1 elseif g=="top"then j,i="pick",nil elseif G then i.m=M(i.m+G*(x==2 and 60 or 1),1,P)elseif g=="ok"then if x<=2 then a2()elseif x==3 then ae("m","")else ae("n",i.n)end end else bk(g,m,G,s)end end ar()end function tick()ay()ar()end function start()bh()ay()L()snail.ink("fast")ar()end local function w(b,z,ax)local aQ=math.max(1,math.min(z-ax//2,#b-ax+1))for e=aQ,math.min(#b,aQ+ax-1)do snail.row(b[e],e==z)end end local function am(b)local q,d=nil,0 for D,a in ipairs(f)do if X(a,b)then d=d+1 if not q or a.e>q.e then q=a end end end if not q then return nil end if d>1 then return q.n.." and "..(d-1).." more are ready"end return q.n.." is ready - "..aq(q,b)end local function ad(c)snail.center(true)snail.small(c)snail.center(false)end local function aF(a,b)if X(a,b)then snail.board(aZ,nil,a.n.." is ready")ad("Finished "..aq(a,b)..(Q(b)and", at "..u(a.e)or""))else snail.board(at(aG(_(a,b))))ad(a.l and a.n.." - paused, "..E(a.l).." left"or a.n.." - "..(Q(b)and"ready at "..u(a.e)or E(_(a,b)).." left"))end end local function bb(a,b)if X(a,b)then return a.n.."   READY - "..aq(a,b)end if a.l then return a.n.."   paused, "..E(a.l)end return a.n.."   "..E(_(a,b))end local aS={new="+ New timer",clear="Clear finished timers"}local function a3()local q=k.f snail.title(q=="m"and"Time for "..i.n or k.back=="timer"and"Rename "..z.n or"Name the timer")snail.status(n or(k.num and""or I==2 and"CAPS"or I==1 and"Shift"or""))snail.text(k.s:sub(1,k.at).."|"..k.s:sub(k.at+1))snail.small(q=="m"and"Minutes like 45 or 90, or hours:minutes like 1:30. Up to 24:00."or"Up to 16 letters, like Sourdough or Sam's turn.")snail.board(a9())snail.hint("arrows move  BACK done  OK press")end local function a4(b)snail.title("New timer")snail.status(n or am(b)or(#f.." of "..J.." timers"))local p={}for e,v in ipairs(ab)do p[e]=v.."   "..E(S[e]*60)end p[#p+1]="+ Type a name"w(p,K,14)snail.hint("BACK timers  OK choose  UP/DN move")end local a_={"OK start  L -1 min  R +1 min","OK start  L -1 h  R +1 h","OK type  L -1 min  R +1 min","OK type  L -1 min  R +1 min"}local function a5(b)local c=i.m*60 snail.title(i.n)snail.status(n or am(b)or"How long?")snail.board(at(aG(c)))ad(E(c)..(Q(b)and", ready at "..u(b+c)or""))snail.row("Start "..i.n.." - "..E(c),x==1)snail.row("Hours: "..i.m//60,x==2)snail.row("Type the time, like 90 or 1:30",x==3)snail.row("Name: "..i.n,x==4)snail.hint("BACK list  "..a_[x].."  UP/DN move")end local function a6(b)local a=z local Y=X(a,b)snail.title(a.n)snail.status(n or(not Y and am(b))or(Y and"Ready"or a.e and"Running"or"Paused"))aF(a,b)for e,r in ipairs(aD(a))do local c if r=="pause"then c=a.e and"Pause"or"Resume"elseif r=="add"then c=a.f and"Snooze 5 minutes"or"Add 5 minutes"elseif r=="restart"then c="Restart ("..E(a.d)..")"elseif r=="rename"then c="Rename"elseif a.f then c="Clear it"else c=ac and"Press OK again to remove it"or"Remove"end snail.row(c,e==O)end snail.hint(a.f and"BACK timers  OK choose  R +1 min  UP/DN move"or"BACK timers  OK choose  L -1 min  R +1 min  UP/DN move")end function draw()local b=os.time()if j=="kb"then return a3()end if j=="pick"then return a4(b)end if j=="set"then return a5(b)end if j=="timer"then return a6(b)end snail.title("Multi Timer")local ai,ah=0,0 for D,a in ipairs(f)do if a.l then ah=ah+1 elseif not X(a,b)then ai=ai+1 end end snail.status(n or am(b)or(#f==0 and"No timers yet")or(ai>0 and ai.." running"..(ah>0 and", "..ah.." paused"or"")or"All paused"))local q=f[B]or f[1]if q then aF(q,b)else snail.board(at("0:00"))ad("Tea, eggs, pasta, laundry: up to "..J.." at once.")end local p={}for e,a in ipairs(f)do p[e]=bb(a,b)end for D,r in ipairs(aC())do p[#p+1]=r=="all"and(aO()and"Pause all"or"Resume all")or aS[r]end w(p,B,10)if#p<=8 then snail.small("Timers keep counting while the app is closed.")end local a=f[B]snail.hint(not a and"BACK leave  OK choose  UP/DN move"or a.f and"BACK leave  OK open  R +1 min  UP/DN move"or"BACK leave  OK open  L -1 min  R +1 min  UP/DN move")end
