Advertisement
1lann

barebones-bios

Mar 21st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. -- A very simple, as basic as you can get bios.lua for ComputerCraft
  2. -- Note: DOES NOT COME WITH FIRST PART PRE-BIOS
  3. -- Made by 1lann and GravityScore for testing our ComputerCraft emulator
  4.  
  5. local commandHistory = {}
  6.  
  7. local function newLine()
  8. local wid, hi = term.getSize()
  9. local x, y = term.getCursorPos()
  10. if y == hi then
  11. term.scroll(1)
  12. term.setCursorPos(1, y)
  13. else
  14. term.setCursorPos(1, y + 1)
  15. end
  16. end
  17.  
  18. local nativeShutdown = os.shutdown
  19. function os.shutdown()
  20. nativeShutdown()
  21. while true do
  22. coroutine.yield()
  23. end
  24. end
  25.  
  26. local nativeReboot = os.reboot
  27. function os.reboot()
  28. nativeReboot()
  29. while true do
  30. coroutine.yield()
  31. end
  32. end
  33.  
  34. function read(replaceCharacter, history)
  35. term.setCursorBlink(true)
  36.  
  37. local line = ""
  38. local historyPos = nil
  39. local pos = 0
  40. if replaceCharacter then
  41. replaceCharacter = string.sub(replaceCharacter, 1, 1)
  42. end
  43.  
  44. local w, h = term.getSize()
  45. local sx, sy = term.getCursorPos()
  46.  
  47. local function redraw(replChar)
  48. local scroll = 0
  49. if sx + pos >= w then
  50. scroll = (sx + pos) - w
  51. end
  52.  
  53. term.setCursorPos(sx, sy)
  54. local replace = replChar or replaceCharacter
  55. if replace then
  56. term.write(string.rep(replace, string.len(line) - scroll))
  57. else
  58. term.write(string.sub(line, scroll + 1))
  59. end
  60. term.setCursorPos(sx + pos - scroll, sy)
  61. end
  62.  
  63. while true do
  64. local sEvent, param = coroutine.yield()
  65. if sEvent == "char" then
  66. line = string.sub(line, 1, pos) .. param .. string.sub(line, pos + 1)
  67. pos = pos + 1
  68. redraw()
  69. elseif sEvent == "key" then
  70. if param == 28 then
  71. break
  72. elseif param == 203 then
  73. if pos > 0 then
  74. pos = pos - 1
  75. redraw()
  76. end
  77. elseif param == 205 then
  78. if pos < string.len(line) then
  79. redraw(" ")
  80. pos = pos + 1
  81. redraw()
  82. end
  83. elseif param == 200 or param == 208 then
  84. if history then
  85. redraw(" ")
  86. if param == 200 then
  87. if historyPos == nil then
  88. if #history > 0 then
  89. historyPos = #history
  90. end
  91. elseif historyPos > 1 then
  92. historyPos = historyPos - 1
  93. end
  94. else
  95. if historyPos == #history then
  96. historyPos = nil
  97. elseif historyPos ~= nil then
  98. historyPos = historyPos + 1
  99. end
  100. end
  101. if historyPos then
  102. line = history[historyPos]
  103. pos = string.len(line)
  104. else
  105. line = ""
  106. pos = 0
  107. end
  108. redraw()
  109. end
  110. elseif param == 14 then
  111. if pos > 0 then
  112. redraw(" ")
  113. line = string.sub(line, 1, pos - 1) .. string.sub(line, pos + 1)
  114. pos = pos - 1
  115. redraw()
  116. end
  117. elseif param == 199 then
  118. redraw(" ")
  119. pos = 0
  120. redraw()
  121. elseif param == 211 then
  122. if pos < string.len(line) then
  123. redraw(" ")
  124. line = string.sub(line, 1, pos) .. string.sub(line, pos + 2)
  125. redraw()
  126. end
  127. elseif param == 207 then
  128. redraw(" ")
  129. pos = string.len(line)
  130. redraw()
  131. end
  132. end
  133. end
  134.  
  135. term.setCursorBlink(false)
  136. term.setCursorPos(w + 1, sy)
  137. newLine()
  138.  
  139. return line
  140. end
  141.  
  142. while true do
  143. term.setTextColor(1)
  144. term.setBackgroundColor(32768)
  145. term.write("lua> ")
  146. local command = ("return "..read(nil, commandHistory))
  147. table.insert(commandHistory, command)
  148. local toRun, cError = loadstring(command, "error")
  149. if toRun then
  150. setfenv(toRun, getfenv(1))
  151. local results = {pcall(toRun)}
  152. term.setBackgroundColor(32768)
  153. if results[1] then
  154. table.remove(results,1)
  155. if #results <= 0 then
  156. term.write("nil")
  157. else
  158. for k,v in pairs(results) do
  159. if k > 1 then newLine() end
  160. term.write(tostring(v))
  161. end
  162. end
  163. elseif not results[2] then
  164. term.write("nil")
  165. else
  166. if term.isColor() then
  167. term.setTextColor(16384)
  168. end
  169. term.write(results[2])
  170. end
  171. else
  172. if term.isColor() then
  173. term.setTextColor(16384)
  174. end
  175. term.write(cError)
  176. end
  177. newLine()
  178. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement