1 line
7.7 KiB
Lua
1 line
7.7 KiB
Lua
local a=import("serialize")local b=import("unicode")local c=import("event")local d=import("component")local e=import("computer")local d=d.gpu;_G.termlib={}termlib.cursorPosX=1;termlib.cursorPosY=1;termlib.readHistory={}local f,g=d.getResolution()local h={["dark"]={[0]=0,[1]=8388608,[2]=32768,[3]=8421376,[4]=128,[5]=8388736,[6]=32896,[7]=12632256},["bright"]={[0]=8421504,[1]=16711680,[2]=65280,[3]=16776960,[4]=255,[5]=16711935,[6]=65535,[7]=16777215}}defaultForegroundColor=h["bright"][7]defaultBackgroundColor=h["dark"][0]d.setForeground(defaultForegroundColor)d.setBackground(defaultBackgroundColor)local function i()local a,b=d.getResolution()if d.copy(1,1,a,b,0,-1)then local c=d.getForeground()local e=d.getBackground()d.setForeground(defaultForegroundColor)d.setBackground(defaultBackgroundColor)d.fill(1,b,a,1," ")d.setForeground(c)d.setBackground(e)termlib.cursorPosY=b end end;local function j()termlib.cursorPosX=1;termlib.cursorPosY=termlib.cursorPosY+1;if termlib.cursorPosY>g then i()end end;local function k(a)local b={}for a in a:sub(3,-2):gmatch("[^;]+")do table.insert(b,tonumber(a))end;return b end;local function l(a)a=math.floor(a)&255;if a<16 then return 4473924*((a>>3)&1)+(12255232*((a>>2)&1)|47872*((a>>1)&1)|187*(a&1))end;if a>=232 then return 65793*(8+(a-232)*10)end;a=a-16;local b={0,95,135,175,215,255}return(b[(a//36)%6+1]<<16)|(b[(a//6)%6+1]<<8)|b[a%6+1]end;local function m(a,b,c)a,b,c=math.floor(a)&255,math.floor(b)&255,math.floor(c)&255;return(a<<16)|(b<<8)|c end;local function n(a,b)local function c(a,b,c)return a>=b and a<=c end;b=b+2;while b<=#a and not c(a:byte(b),64,127)do b=b+1 end;return b end;function termlib.write(a,c)local f,g=d.getResolution()if c==nil then c=true end;if not a or not tostring(a)then return end;if a:find("\a")then e.beep()end;a=tostring(a)a="\27[0m"..a:gsub("\t"," ")local e=0;local g=""local function i()if#g==0 then return end;while true do d.set(termlib.cursorPosX,termlib.cursorPosY,g)if b.wlen(g)>f-termlib.cursorPosX+1 and c then g=g:sub(f-termlib.cursorPosX+2)j()else termlib.cursorPosX=termlib.cursorPosX+b.wlen(g)break end end;g=""end;for b=1,#a do if e>0 then e=e-1;goto continue end;if string.byte(a,b)==10 then i()j()elseif string.byte(a,b)==13 then i()termlib.cursorPosX=1 elseif string.byte(a,b)==27 and b<=#a-2 then i()local c=string.sub(a,b+1,b+1)if c=="["then local c=n(a,b)local a=string.sub(a,b,c)e=e+#a-1;local b=k(a)local a=a:sub(-1)if a=="m"then if b[1]>=30 and b[1]<=37 then d.setForeground(h["dark"][b[1]%10])end;if b[1]==38 and b[2]==5 then d.setForeground(l(b[3]))end;if b[1]==38 and b[2]==2 then d.setForeground(m(b[3],b[4],b[5]))end;if b[1]==39 or b[1]==0 then d.setForeground(defaultForegroundColor)end;if b[1]>=40 and b[1]<=47 then d.setBackground(h["dark"][b[1]%10])end;if b[1]==48 and b[2]==5 then d.setBackground(l(b[3]))end;if b[1]==48 and b[2]==2 then d.setBackground(m(b[3],b[4],b[5]))end;if b[1]==49 or b[1]==0 then d.setBackground(defaultBackgroundColor)end;if b[1]>=90 and b[1]<=97 then d.setForeground(h["bright"][b[1]%10])end;if b[1]>=100 and b[1]<=107 then d.setBackground(h["bright"][b[1]%10])end end end else g=g..string.sub(a,b,b)end::continue::end;i()end;function _G.print(...)local b={...}local c={}for b,b in pairs(b)do if type(b)=="table"then table.insert(c,a.table(b,true))elseif tostring(b)then table.insert(c,tostring(b))end end;termlib.write(table.concat(c," ").."\n")end;function _G.clear()f,g=d.getResolution()d.setForeground(defaultForegroundColor)d.setBackground(defaultBackgroundColor)d.fill(1,1,f,g," ")termlib.cursorPosX,termlib.cursorPosY=1,1 end;function _G.read(a,e,h,j)checkArg(1,a,"string","nil")checkArg(2,e,"string","nil")checkArg(3,h,"string","nil")checkArg(4,j,"number","nil")j=j or math.huge;local h=h or""local k;if a then if not termlib.readHistory[a]then termlib.readHistory[a]={h}elseif termlib.readHistory[a][#termlib.readHistory[a]]~=""then table.insert(termlib.readHistory[a],h)end;k=#termlib.readHistory[a]end;local function l()if not a then return end;termlib.readHistory[a][k]=h end;local m=b.len(h)+1;if e then termlib.write(e)end;local e,n=termlib.cursorPosX,termlib.cursorPosY;local o,p=d.getForeground(),d.getBackground()local q=true;local function r(a)a=e+a-1;return d.get(a%f,n+(a//f))end;local function r(a)for a=1,a-g do i()n=n-1 end;return math.min(a,g)end;local function s(a,c,g)if c==nil or c==""then return end;if g then d.setForeground(p)d.setBackground(o)else d.setForeground(o)d.setBackground(p)end;a=e+a-1;local a,e=(a-1)%f+1,n+((a-1)//f+1)-1;e=r(e)d.set(a,e,b.sub(c,1,f-a+1))for g=1,math.ceil((#c+a-1)/f)+1 do d.set(1,e+g,b.sub(c,2-a+g*f,f+g*f-a))e=r(e)end end;local function d(a,b)if#a==0 then return b end;return a end;local function n(a)return b.wlen(b.sub(h,1,a-1))+1 end;local function o(a)if type(a)~="string"or#a==0 then return end;if b.len(h)>=j then return end;if j<math.huge then a=b.sub(a,1,j-b.len(h))end;h=b.sub(h,1,m-1)..a..b.sub(h,m)s(n(m),a,false)m=math.min(m+b.len(a),j+1)s(n(m),d(b.sub(h,m,m)," "),true)q=true;s(n(m+1),b.sub(h,m+1),false)end;local function j(a)s(n(m),d(b.sub(h,m,m)," "),false)m=math.max(math.min(m+a,b.len(h)+1),1)s(n(m),d(b.sub(h,m,m)," "),true)q=true end;local function p(a)return not string.find("\x09 :@-./_~?&=%+#",a,1,true)end;local function r(a,c,d)if d==nil then d=m end;local a=math.max(math.min(d+a,b.len(h)+1),1)if c then return b.sub(h,a,a)end;return a end;local function t(a)local b=m;while r(a,false,b)~=b and p(r(a,true,b))==(a==1)do b=r(a,false,b)end;while r(a,false,b)~=b and p(r(a,true,b))==(a==-1)do b=r(a,false,b)end;return b end;local function p(a)if r(a)==m then return end;s(n(m),d(b.sub(h,m,m)," "),false)m=t(a)s(n(m),d(b.sub(h,m,m)," "),true)q=true end;local function r(a)local c=t(a)local e=b.wlen(h)if a==1 then h=b.sub(h,1,m-1)..b.sub(h,c)s(n(m+1),b.sub(h,m+1)..string.rep(" ",e-b.wlen(h)+1),false)s(n(m),d(b.sub(h,m,m)," "),true)else h=b.sub(h,1,c-1)..b.sub(h,m)m=c;s(n(m+1),b.sub(h,m+1)..string.rep(" ",e-b.wlen(h)+1),false)s(n(m),d(b.sub(h,m,m)," "),true)end;l()q=true end;local function t(a)return a=="\n"or a=="\r"end;s(1,h,false)s(n(m)," ",true)local function u(a)s(1,a..string.rep(" ",b.wlen(h)-b.wlen(a)+1),false)m=b.len(a)+1;h=a;s(n(m)," ",true)end;while true do local c={c.pull("key_down","clipboard",0.5)}if c and c[1]=="key_down"and c[4]then local e=keyboard.keys[c[4]]if e=="up"and a then k=math.max(k-1,1)u(termlib.readHistory[a][k])elseif e=="down"and a then k=math.min(k+1,#termlib.readHistory[a])u(termlib.readHistory[a][k])elseif e=="left"and keyboard.ctrlDown then p(-1)elseif e=="right"and keyboard.ctrlDown then p(1)elseif e=="left"then j(-1)elseif e=="right"then j(1)elseif e=="home"then j(-math.huge)elseif e=="end"then j(math.huge)elseif e=="back"and keyboard.ctrlDown then r(-1)elseif e=="delete"and keyboard.ctrlDown then r(1)elseif e=="back"and m>1 then h=b.sub(h,1,m-2)..b.sub(h,m)m=m-1;s(n(m),d(b.sub(h,m,m)," "),true)q=true;s(n(m)+1,b.sub(h,m+1).." ",false)l()elseif e=="delete"then h=b.sub(h,1,m-1)..b.sub(h,m+1)s(n(m),d(b.sub(h,m,m)," "),true)q=true;if m<=b.len(h)then s(n(m+1),b.sub(h,m+1).." ",false)end;l()elseif e=="enter"then s(n(m),d(b.sub(h,m,m)," "),false)break elseif not(c[3]<32 or(c[3]>127 and c[3]<=159))then o(b.char(c[3])or" ")l()end elseif c and c[1]=="clipboard"then local a=c[3]if not c[3]then goto continue end;while t(b.sub(a,1,1))do a=b.sub(a,2)end;while t(b.sub(a,-1))do a=b.sub(a,1,-2)end;o(a)l()else q=not q;s(n(m),d(b.sub(h,m,m)," "),q)end::continue::end;if a then if termlib.readHistory[a][#termlib.readHistory[a]]==""then table.remove(termlib.readHistory[a],#termlib.readHistory[a])end;if k<#termlib.readHistory[a]then table.remove(termlib.readHistory[a],k)table.insert(termlib.readHistory[a],h)end;while#termlib.readHistory[a]>50 do table.remove(termlib.readHistory[a],1)end end;termlib.cursorPosX=1;termlib.cursorPosY=termlib.cursorPosY+math.ceil((b.wlen(h)+e-1)/f)if termlib.cursorPosY>g then i()end;return h end |