Advertisement
Guest User

osutils.lua

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. local w,h = term.getSize()
  2. crash = function(reason)
  3.   if type(reason) ~= "string" then
  4.     error("String excepted, got "..type(reason))
  5.   end
  6.   term.setBackgroundColor(colors.blue)
  7.   term.setTextColor(colors.white)
  8.   term.clear()
  9.   term.setCursorPos(1,1)
  10.   print("Something goes wrong!")
  11.   print("Reason: "..reason)
  12.   sleep(0.5)
  13.   os.reboot()
  14. end
  15.  
  16. runCons = function()
  17.   shell.run("fg /sys/apps/console")
  18. end
  19.  
  20. editWall = function()
  21.   shell.run("fg edit /data/wallpaper")
  22. end
  23.  
  24. local writeCentered = function(s,y)
  25.   term.setCursorPos(math.floor((w/2)-(string.len(s)/2)),y)
  26.   write(s)
  27. end
  28.  
  29. local logonBasic = function()
  30.   write("Username: ")
  31.   local usr = read()
  32.   write("Password: ")
  33.   local pas = read("*")
  34.   return usr,pas
  35. end
  36.  
  37. logon = function()
  38.   local logged = false
  39.   while not logged do
  40.     local user,pass = logonBasic()
  41.     term.setBackgroundColor(colors.blue)
  42.     term.setTextColor(colors.white)
  43.     term.clear()
  44.     term.setCursorPos(1, 1)
  45.     paintutils.drawLine(1,1,w,1,colors.white)
  46.     paintutils.drawLine(1,h,w,h,colors.white)
  47.     term.setBackgroundColor(colors.blue)
  48.     term.setTextColor(colors.white)
  49.     term.setCursorPos(1,2)
  50.     if not fs.isDir("/usr/"..user) then
  51.       printError("Invaild Username!")
  52.     else
  53.       local a = fs.open("/usr/"..user.."/pass","r")
  54.       local uPass = a.readLine()
  55.       a.close()
  56.       if pass ~= uPass then
  57.         printError("Invaild Password!")
  58.       else
  59.         logged = true
  60.         writeCentered("Welcome!",math.floor(h/2))
  61.         sleep(5)
  62.       end
  63.     end
  64.   end
  65. end
  66.  
  67. formatSize = function(size)
  68.   if type(size) ~= "number" then
  69.     error("Attempt to format size in "..type(size))
  70.   end
  71.   local format = nil
  72.   local sizes = {
  73.     [1] = {symbol="KB",num=1},
  74.     [2] = {symbol="MB",num=2},
  75.     [3] = {symbol="GB",num=3}
  76.   }
  77.   for i=1,#sizes do
  78.     if i==1 then
  79.       if size >= 1000^sizes[i].num then
  80.         format = math.floor(size / (1000^sizes[i].num)) .. " " .. sizes[i].symbol
  81.       end
  82.     else
  83.       if size >= 1000^sizes[i].num and size < 1000^sizes[i-1].num then
  84.         format = math.floor(size / (1000^sizes[i].num)) .. " " .. sizes[i].symbol
  85.       end
  86.     end
  87.   end
  88.   if format == nil then
  89.     format = size.." B"
  90.   end
  91.   return format
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement