Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.57 KB | None | 0 0
  1. ---
  2. --- Created by Petereo.
  3. --- DateTime: 19/11/2019 18:12
  4. ---
  5. _osname = 'Petereo OS'
  6. _osversion = "0.1"
  7.  
  8. function require(pkg)
  9.     if type(pkg) ~= 'string' then return nil end
  10.     return _ENV[pkg] or _G[pkg]
  11. end
  12. --- Component ---
  13. setmetatable(component,{__index = function(_,k) return component.getPrimary(k) end})
  14. function component.getPrimary(dev)
  15.     if dev == 'filesystem' then
  16.         return component.proxy(computer.getBootAddress())
  17.     end
  18.     for k,v in component.list() do
  19.         if v == dev then
  20.             return component.proxy(k)
  21.         end
  22.     end
  23. end
  24.  
  25. --- Graphics ---
  26.  
  27. g = {}
  28. function g.bind()
  29.     local screen = component.list('screen')()
  30.     return component.gpu.bind(screen)
  31. end
  32.  
  33. function g.available()
  34.     local gpu, screen = component.list('gpu')(),component.list('screen')()
  35.     return(gpu and screen)
  36. end
  37.  
  38. function g.copy(x,y,w,h,a,b)
  39.     component.gpu.copy(x,y,w,h,a,b)
  40. end
  41.  
  42. function g.getResolution()
  43.     return component.gpu.getResolution()
  44. end
  45.  
  46. function g.setResolution(w,h)
  47.     return component.gpu.setResolution(w,h)
  48. end
  49.  
  50. function g.getDepth()
  51.     return component.gpu.getDepth()
  52. end
  53.  
  54. function g.setBG(color)
  55.     return component.gpu.setBG(color)
  56. end
  57.  
  58. function g.setFG(color)
  59.     return component.gpu.setFG(color)
  60. end
  61.  
  62. function g.fill(x,y,w,h)
  63.     return component.gpu.fill(x,y,w,h,' ')
  64. end
  65.  
  66. function g.fillc(x,y,w,h,ch)
  67.     return component.gpu.fill(x,y,w,h,ch)
  68. end
  69.  
  70. function g.drawText(x,y,str)
  71.     return component.gpu.set(x,y,str)
  72. end
  73.  
  74. local w,h = g.getResolution()
  75. local cx,cy = (w/2),(h/2)
  76.  
  77.  
  78. --+-+-+-+-+ Filesystem +-+-+-+-+--
  79. f = {}
  80. f.addr = computer.getBootAddress()
  81. function f.setBootAddress(addr)
  82.     f.addr = addr
  83. end
  84. function f.open(file,mode)
  85.     return component.filesystem.open(file,mode)
  86. end
  87. function f.read(handle)
  88.     return component.filesystem.read(handle,math.huge)
  89. end
  90. function f.write(handle,str)
  91.     return component.filesystem.write(handle,str)
  92. end
  93. function f.close(handle)
  94.     return component.filesystem.close(handle)
  95. end
  96. function f.list(dir)
  97.     return component.filesystem.list(dir or '/')
  98. end
  99. function f.mkdir(dir)
  100.     return component.filesystem.makeDirectory(dir)
  101. end
  102. function f.rename(name1,name2)
  103.     return component.filesystem.rename(name1,name2)
  104. end
  105. function f.remove(dir)
  106.     return component.filesystem.remove(dir)
  107. end
  108. function f.loadfile(file)
  109.     local hdl,err = f.open(file,'r')
  110.     if not hdl then error(err) end
  111.     local buffer = ''
  112.     repeat
  113.         local data, err_read = f.read(hdl)
  114.         if not data and err_read then error(err_read) end
  115.         buffer = buffer .. (data or '')
  116.     until not data
  117.     f.close(hdl)
  118.     return load(buffer,'='..file)
  119. end
  120. function f.runfile(file,argc,args)
  121.     local prog,err = f.loadfile(file)
  122.     if prog then
  123.         local res = table.pack(xpcall(prog,function(...) return debug.traceback() end,argc,args))
  124.         if res[1] then
  125.             return table.unpack(res,2,res.n)
  126.         else
  127.             error(res[2])
  128.         end
  129.     else
  130.         error(err)
  131.     end
  132. end
  133. f.load = f.loadfile
  134. f.run = f.runfile
  135.  
  136. --- Error ---
  137.  
  138. std_error = error
  139. function error(msg)
  140.     g.setFG(0xFFFFFF)
  141.     g.setBG(0x000000)
  142.     g.fill(1,1,w,h)
  143.     -- write to screen
  144.     local line = 1
  145.     local prev = 1
  146.     for w in string.gmatch(msg,'()\n') do
  147.         g.drawText(1,line,msg:sub(prev,w-1))
  148.         prev = w+1
  149.         line = line+1
  150.     end
  151.     g.drawText(1,line,msg:sub(prev))
  152.     -- write to log
  153.     local hdl,errf = f.open('error_log.txt','w')
  154.     if hdl then
  155.         f.write(hdl,msg..'\n')
  156.     end
  157.     f.close(hdl)
  158.     repeat
  159.         local e,addr,char,code = computer.pullSignal()
  160.     until e == 'key_down' and code == 28
  161.     computer.shutdown(true)
  162. end
  163.  
  164. -- Shell --
  165.  
  166. g.fill(1,1,w,h)
  167.  
  168. function tabletostr(tabl)
  169.     local retval = ''
  170.     for i=1, #tabl do
  171.         retval = retval .. tabl[i]
  172.     end
  173.     return retval
  174. end
  175.  
  176. function strtotable(str)
  177.     local retval = {}
  178.     for i = 1, #str do
  179.         table.insert(retval,str:sub(i,i))
  180.     end
  181.     return retval
  182. end
  183.  
  184. function serialize (o, ind)
  185.     ind = ind or 1
  186.     local indn = 0
  187.     local indent = ''
  188.     while indn < ind do
  189.         indent = indent .. '  '
  190.         indn =  indn + 1
  191.     end
  192.     local retstr = ''
  193.     if type(o) == "number" then
  194.         retstr = retstr .. o .. ''
  195.     elseif type(o) == "boolean" then
  196.         retstr = retstr .. tostring(o)
  197.     elseif type(o) == "string" then
  198.         retstr = retstr .. string.format("%q", o) .. ''
  199.     elseif type(o) == "table" then
  200.         retstr = retstr .. '{\n'
  201.         for k,v in pairs(o) do
  202.             retstr = retstr .. indent .. '' .. k .. '='
  203.             retstr = retstr .. serialize(v,ind+1)
  204.             retstr = retstr .. ',\n'
  205.         end
  206.         retstr = retstr .. indent:sub(1,indent:len()-2) .. '}'
  207.     else
  208.         retstr = retstr .. '<'..type(o)..'>'
  209.     end
  210.     return retstr
  211. end
  212.  
  213. -- Console --
  214.  
  215. local console_header = '$ '
  216.  
  217. --- output buffer
  218.  
  219. local out = {} -- output system
  220. out.history = {} -- output lines history
  221. out.historysize = h*10 -- history size
  222. out.coutl = {} -- current output line
  223. out.line = 1 -- current line
  224. out.lptr = 1 -- line pointer to history table
  225. out.col = 1 -- current column
  226. out.topline = 0 -- last line in history
  227.  
  228. local function outl()
  229.     if out.topline ~= 0 then return end
  230.     g.fill(out.col,out.line,w,1)
  231.     local strout = tabletostr(out.coutl)
  232.     g.drawText(1,out.line,strout)
  233. end
  234. function cout(str)
  235.     str = tostring(str)
  236.     if #str > 1 then
  237.         local atab = strtotable(str)
  238.         for i=1, #atab do
  239.             table.insert(out.coutl,out.col,atab[i])
  240.             out.col = out.col + 1
  241.         end
  242.     else
  243.         table.insert(out.coutl,out.col,str)
  244.         out.col = out.col + 1
  245.     end
  246.     outl()
  247. end
  248. local function backspace()
  249.     if out.col <= #console_header+1 then
  250.         out.col = #console_header+1
  251.         return
  252.     end
  253.     table.remove(out.coutl,out.col-1)
  254.     out.col = out.col - 1
  255.     outl()
  256. end
  257. local function scrollUp()
  258.     if #out.history >= h then
  259.         if out.topline == 0 then outl() end
  260.         out.topline = out.topline + 1
  261.         if out.topline+(h-1) > #out.history then
  262.             out.topline = #out.history - h+1
  263.             return false
  264.         end
  265.         g.copy(1,1,w,h-1,0,1)
  266.         g.fill(1,1,w,1)
  267.         g.drawText(1,1,out.history[out.topline + (h-1)])
  268.         return true
  269.     end
  270. end
  271. local function scrollDown()
  272.     if #out.history >= h then
  273.         out.topline = out.topline - 1
  274.         if out.topline < 0 then
  275.             out.topline = 0
  276.             return false
  277.         end
  278.         if out.topline == 0 then
  279.             g.copy(1,2,w,h-1,0,-1)
  280.             g.fill(1,h,w,1)
  281.             outl()
  282.         else
  283.             g.copy(1,2,w,h-1,0,-1)
  284.             g.fill(1,h,w,1)
  285.             g.drawText(1,h,out.history[out.topline])
  286.         end
  287.         return true
  288.     end
  289. end
  290.  
  291. local function newline()
  292.     local atab = tabletostr(out.coutl)
  293.     table.insert(out.history,1,atab)
  294.     if #out.history > out.historysize then
  295.         table.remove(out.history)
  296.     end
  297.     out.line = out.line + 1
  298.     out.lptr = out.lptr + 1
  299.     if out.line > h then
  300.         out.line = h
  301.         if out.topline == 0 then
  302.             g.copy(1,2,w,h-1,0,-1)
  303.             g.fill(1,h,w,1)
  304.         end
  305.     end
  306.     out.col = 1
  307.     out.coutl = {}
  308. end
  309.  
  310. function print(str)
  311.     if type(str) == 'table' then
  312.         str = serialize(str)
  313.     else
  314.         str = tostring(str)
  315.     end
  316.     local prev = 1
  317.     for w in string.gmatch(str,'()\n') do
  318.         cout(str:sub(prev,w-1))
  319.         prev = w+1
  320.         newline()
  321.     end
  322.     cout(str:sub(prev))
  323.     newline()
  324. end
  325.  
  326. local function main()
  327.     local curloc = 1
  328.     local curstate = false
  329.  
  330.     local function blinkcursor()
  331.         if out.topline ~= 0 then return end
  332.         if curstate then
  333.             g.drawText(out.col,out.line,unicode.char(0x2588))
  334.         else
  335.             outl()
  336.         end
  337.         curstate = not curstate
  338.     end
  339.  
  340.     -- command history --
  341.  
  342.     local cmdhistory = {}
  343.     local cmdhlen = 20
  344.     local cmdhbrowse = 0
  345.  
  346.     --- todo ---
  347.  
  348.     print(_osname..' '.._osversion.. '(' ..math.floor(computer.totalMemory() / 1000)..'k RAM)\n\n')
  349.     cout(console_header)
  350.     while true do
  351.  
  352.         local evt = table.pack(computer.pullSignal(0.4))
  353.         if evt[1] == 'key_down' then
  354.             if evt[4] == 88 then computer.shutdown(true) end -- F12 to restart
  355.             if evt[4] == 68 then computer.shutdown() end -- F10 to shutdown
  356.             -- command
  357.             if evt[4] == 28 then -- enter key
  358.                 outl()
  359.                 -- convert to string
  360.                 local cinput = tabletostr(out.coutl)
  361.                 cinput = cinput:sub(#console_header+1)
  362.                 -- add to history
  363.                 if cinput ~= '' then
  364.                     table.insert(cmdhistory,1,cinput)
  365.                 end
  366.                 if #cmdhistory > cmdhlen then table.remove(cmdhistory) end
  367.                 -- parse command
  368.                 local luacmd = cinput
  369.                 --[[local args = {}
  370.                 local argc = 0
  371.                 local cmdget = false
  372.                 local command = ''
  373.                 cinput = string.gsub(cinput,'%s',unicode.char(0x2591))
  374.                 for p in string.gmatch(cinput,'(.-)'..unicode.char(0x2591)) do
  375.                   if not cmdget then
  376.                     command = p
  377.                     cmdget = true
  378.                   else
  379.                     table.insert(args,p)
  380.                     argc = argc + 1
  381.                   end
  382.                 end]]
  383.                 -- execute
  384.                 newline()
  385.                 cmdhbrowse = 0
  386.                 local loaded,lderr = load(luacmd,'=cinput')
  387.                 if loaded then
  388.                     local res,err = xpcall(loaded,function(msg) return msg ..'\n'.. debug.traceback() end)
  389.                     if not res and err then
  390.                         print(err)
  391.                     end
  392.                 elseif not loaded and lderr then
  393.                     print(lderr)
  394.                 else
  395.                     print('Unknown Command')
  396.                 end
  397.                 -- done
  398.                 cout(console_header)
  399.             elseif evt[4] == 14 then -- backspace
  400.                 curstate = true -- keep cursor displayed
  401.                 backspace()
  402.             elseif evt[4] == 200 then -- up key
  403.                 out.coutl = {}
  404.                 out.col = 1
  405.                 if cmdhbrowse >= #cmdhistory then cmdhbrowse = #cmdhistory else
  406.                     cmdhbrowse = cmdhbrowse + 1 end
  407.                 local hval = cmdhistory[cmdhbrowse]
  408.                 cout(console_header)
  409.                 cout(hval)
  410.             elseif evt[4] == 208 then -- down key
  411.                 out.coutl = {}
  412.                 out.col = 1
  413.                 if cmdhbrowse <= 1 then cmdhbrowse = 1 else
  414.                     cmdhbrowse = cmdhbrowse - 1 end
  415.                 local hval = cmdhistory[cmdhbrowse]
  416.                 cout(console_header)
  417.                 cout(hval)
  418.             elseif evt[4] == 203 then -- left key
  419.                 curstate = true -- keep cursor displayed
  420.                 out.col = out.col - 1
  421.                 if out.col < #console_header+1 then
  422.                     out.col = #console_header+1
  423.                 end
  424.                 outl()
  425.             elseif evt[4] == 205 then --  right key
  426.                 curstate = true -- keep cursor displayed
  427.                 out.col = out.col + 1
  428.                 if out.col > #out.coutl+1 then
  429.                     out.col = #out.coutl+1
  430.                 end
  431.                 outl()
  432.             elseif evt[4] == 199 then -- home
  433.                 curstate = true
  434.                 out.col = #console_header+1
  435.                 outl()
  436.             elseif evt[4] == 207 then -- end
  437.                 curstate = true
  438.                 out.col = #out.coutl+1
  439.                 outl()
  440.             elseif evt[3] ~= 0 then -- printable keys
  441.                 curstate = true -- keep cursor displayed
  442.                 local char = string.char(evt[3])
  443.                 cout(char)
  444.             end
  445.         elseif evt[1] == 'scroll' then
  446.             if evt[5] > 0 then -- scroll up
  447.                 scrollUp()
  448.             elseif evt[5] < 0 then -- scroll down
  449.                 scrollDown()
  450.             end
  451.         end
  452.         blinkcursor()
  453.     end
  454. end
  455.  
  456.     if component.filesystem.exists('autorun.lua') then
  457.         f.runfile('autorun.lua')
  458.     end
  459.  
  460. local res,err = xpcall(main,function(msg) return msg ..'\n'.. debug.traceback() end)
  461. if not res and err then
  462.     error(err)
  463. end
  464. computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement