Advertisement
Guest User

osutils.lua

a guest
Mar 3rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 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. oslog("Crashed! Reason: "..reason)
  13. sleep(0.5)
  14. os.reboot()
  15. end
  16.  
  17. local oslog = function(s)
  18. local a = fs.open("/bootlog","r")
  19. local tmp = nil
  20. if a ~= nil then
  21. tmp = a.readAll()
  22. a.close()
  23. end
  24. local b = fs.open("/bootlog","w")
  25. if tmp == nil then
  26. tmp = ""
  27. end
  28. b.write(tmp.."\n"..s)
  29. b.close()
  30. end
  31.  
  32. runCons = function()
  33. shell.run("fg /sys/apps/console")
  34. end
  35.  
  36. editWall = function()
  37. shell.run("fg edit /data/wallpaper")
  38. end
  39.  
  40. local writeCentered = function(s,y)
  41. term.setCursorPos(math.floor((w/2)-(string.len(s)/2)),y)
  42. write(s)
  43. end
  44.  
  45. local logonBasic = function()
  46. write("Username: ")
  47. local usr = read()
  48. write("Password: ")
  49. local pas = read("*")
  50. return usr,pas
  51. end
  52.  
  53. logon = function()
  54. local logged = false
  55. while not logged do
  56. local user,pass = logonBasic()
  57. term.setBackgroundColor(colors.blue)
  58. term.setTextColor(colors.white)
  59. term.clear()
  60. term.setCursorPos(1, 1)
  61. paintutils.drawLine(1,1,w,1,colors.white)
  62. paintutils.drawLine(1,h,w,h,colors.white)
  63. term.setBackgroundColor(colors.blue)
  64. term.setTextColor(colors.white)
  65. term.setCursorPos(1,2)
  66. oslog("Logging data:")
  67. oslog(" Username: "..user)
  68. oslog(" Password: "..pass)
  69. oslog("Logging...")
  70. if not fs.isDir("/usr/"..user) then
  71. printError("Invaild Username!")
  72. oslog("Invaild Username!")
  73. else
  74. local a = fs.open("/usr/"..user.."/pass","r")
  75. local uPass = a.readLine()
  76. a.close()
  77. if pass ~= uPass then
  78. printError("Invaild Password!")
  79. oslog("Invaild Password!")
  80. else
  81. local b = fs.open("/usr/"..user.."/pimg","r")
  82. if b ~= nil then -- if file exists
  83. local tLines = {}
  84. repeat
  85. local line = b.readLine()
  86. table.insert(tLines,line)
  87. until not line
  88. b.close()
  89. local tLengths = {}
  90. for i=1,#tLines do
  91. slen = string.len(tLines[i])
  92. table.insert(tLengths,slen)
  93. table.sort(tLengths)
  94. end
  95. local xCenter = math.floor((w/2) - (tLengths[#tLengths]/2))
  96. local yCenter = math.floor((h/2) - (#tLengths/2))
  97. term.setCursorPos(xCenter,yCenter)
  98. local _img = paintutils.loadImage("/usr/"..user.."/pimg")
  99. paintutils.drawImage(_img,xCenter,yCenter)
  100. term.setBackgroundColor(colors.blue)
  101. logged = true
  102. writeCentered("Welcome!",h-2)
  103. sleep(5)
  104. else -- else
  105. logged = true
  106. writeCentered("Welcome!",math.floor(h/2))
  107. sleep(5)
  108. end
  109. end
  110. end
  111. end
  112. end
  113.  
  114. formatSize = function(size)
  115. if type(size) ~= "number" then
  116. error("Attempt to format size in "..type(size))
  117. end
  118. local format = nil
  119. local sizes = {
  120. [1] = {symbol="KB",num=1},
  121. [2] = {symbol="MB",num=2},
  122. [3] = {symbol="GB",num=3},
  123. [4] = {symbol="TB",num=4}
  124. }
  125. for i=1,#sizes do
  126. if i==1 then
  127. if size >= 1000^sizes[i].num then
  128. format = math.floor(size / (1000^sizes[i].num)) .. " " .. sizes[i].symbol
  129. end
  130. else
  131. if size >= 1000^sizes[i].num and size < 1000^sizes[i-1].num then
  132. format = math.floor(size / (1000^sizes[i].num)) .. " " .. sizes[i].symbol
  133. end
  134. end
  135. end
  136. if format == nil then
  137. format = size.." B"
  138. end
  139. return format
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement