-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.lua
More file actions
1 lines (1 loc) · 3.53 KB
/
console.lua
File metadata and controls
1 lines (1 loc) · 3.53 KB
1
local a=require('io')local b={}function b.string(c,d,e,f,g,h)d=d or c;e=e or 0;if g==nil then g=true end;if h==nil then h=true end;local function i()local j={}if e==f then j[#j+1]='len='..tostring(e)else if e>0 and f~=nil then j[#j+1]=tostring(e)..'<=len<='..tostring(f)elseif e>0 then j[#j+1]='len>='..tostring(e)elseif f~=nil then j[#j+1]='len<='..tostring(f)end end;if not g then j[#j+1]='1 word'end;if h then j[#j+1]='trimmed'end;return j end;local function k()local l=i()if#l==0 then return end;a.write('[')for m,n in ipairs(l)do if m~=1 then a.write(', ')end;a.write(n)end;a.write(']')end;local o=false;a.write(c..': ')while true do local p=a.read()if h then p=p:match("^%s*(.-)%s*$")end;if not g and p:match('%s+')then elseif#p<e or f~=nil and#p>f then else return p end;if not o then a.write('Validation failed, reqs: ')k()a.write('\n')o=true end;a.write(d..': ')end end;function b.stringverify(c,d,q,e,f,g,h)q=q or'Did you make any typos?'local r=b.string(c,d,e,f,g,h)local s=not b.yesno(q)while not s do r=b.string(c,d,e,f,g,h)s=not b.yesno(q)end;return r end;function b.numeric(c,d,t,u,v)c=c or'Please enter a number'd=d or c;a.write(c..' (numeric')if t~=nil then if u==nil then a.write(' >= '..tostring(t))else a.write(' in ('..tostring(t)..', '..tostring(u)..')')end elseif u~=nil then a.write(' <= '..tostring(u))end;if v then a.write(' integer')end;a.write('): ')while true do local w=a.read()if#w~=0 then local x=tonumber(w)if x~=nil and(t==nil or x>=t)and(u==nil or x<=u)and(not v or x==math.floor(x))then return x end end;a.write(d)end end;function b.yesno(c,d)c=c or''d=d or c;a.write(c..' (Y/N): ')while true do local w=a.read()if#w==1 then if w=='y'or w=='Y'then return true end;if w=='n'or w=='N'then return false end end;a.write(d..': ')end end;function b.yesnocancel(c,d)c=c or''d=d or''a.write(c..' (Y/N/C): ')while true do local w=a.read()if#w==1 then if w=='y'or w=='Y'then return true end;if w=='n'or w=='N'then return false end;if w=='c'or w=='C'then return nil end end;a.write(d..': ')end end;local function y(z)if type(z)=='table'or type(z)=='userdata'and val.prompt then return tostring(z.prompt)else return tostring(z)end end;function b.select(c,A,B)local function C()for D,val in ipairs(A)do local E=y(val)a.write(' '..tostring(D)..'. '..E..'\n')end;if B then a.write('\n c. Cancel\n')end end;if type(c)=='table'then B=A;A=c;c=nil end;c=c or'Choose one'a.write(c..'\n')C()local F=0;while true do local G=a.read()if G=='c'or G=='C'then if B then return nil,nil end else local x=tonumber(G)if x~=nil and x>=1 and x<=#A then return A[x],x end end;F=F+1;if F>10 then a.write(c..'\n')C()F=0 end end end;function b.multiselect(c,A,H,I,B)if type(c)=='table'then B=I;I=H;H=A;A=c;c=nil end;c=c or'Choose'H=H or 0;I=I or#A;local J=0;local K={}local function C()for D,val in ipairs(A)do a.write(' ')if K[D]then a.write('[SEL] ')end;a.write(tostring(D)..'. '..y(val)..'\n')end;if J>=H and J<=I then a.write(' f. Finish\n')end;if B then a.write('\n c. Cancel\n')end end;local function L()a.write(c)if H==I then a.write(' (Select '..tostring(H)..')\n')elseif I>=#A then a.write(' (Select '..tostring(H)..' or more)\n')else a.write(' (Select '..tostring(H)..' to '..tostring(I)..')\n')end;C()end;local M=0;L()while true do local p=a.read()if B and(p=='c'or p=='C')then return nil,nil end;if(p=='f'or p=='F')and(J>=H and J<=I)then local N={}local O={}for D,P in pairs(K)do N[#N+1]=A[D]O[#O+1]=D end;return N,O end;local Q=tonumber(p)if Q~=nil and Q==math.floor(Q)and Q>0 and Q<=#A then if K[Q]then J=J-1;K[Q]=nil else J=J+1;K[Q]=true end;L()else M=M+1;if M>10 then L()M=0 end end end end;return b