Advertisement
xXm0dzXx

Multitasker v1.0 PR1

Oct 14th, 2012
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. local oldevent = os.pullEventRaw
  2. local x,y = term.getSize()
  3. tabbing = false;
  4. isMultitasking = false
  5. screenMemory = {}
  6. multiMemory = {
  7. "CraftOS vM.1",
  8. }
  9.  
  10. local cPrint = function(text)
  11. local x2,y2 = term.getCursorPos()
  12. term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  13.  
  14. print(text)
  15. end
  16.  
  17. function mRead( _sReplaceChar, _tHistory )
  18. term.setCursorBlink( true )
  19.  
  20. local sLine = ""
  21. local nHistoryPos = nil
  22. local nPos = 0
  23. if _sReplaceChar then
  24. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  25. end
  26.  
  27. local w, h = term.getSize()
  28. local sx, sy = term.getCursorPos()
  29. local function redraw()
  30. local nScroll = 0
  31. if sx + nPos >= w then
  32. nScroll = (sx + nPos) - w
  33. end
  34.  
  35. term.setCursorPos( sx, sy )
  36. mWrite( "> " ..string.rep(" ", 14) )
  37. term.setCursorPos( sx, sy )
  38. if _sReplaceChar then
  39. mWrite( "> " ..string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  40. else
  41. mWrite( "> " ..string.sub( sLine, nScroll + 1 ) )
  42. end
  43. term.setCursorPos( sx + nPos - nScroll, sy )
  44. end
  45.  
  46. while true do
  47. local sEvent, param = os.pullEvent()
  48. if sEvent == "char" then
  49. if nPos < 14 then
  50. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  51. nPos = nPos + 1
  52. end
  53. redraw()
  54.  
  55. elseif sEvent == "key" then
  56. if param == keys.enter then
  57. -- Enter
  58. break
  59.  
  60. elseif param == keys.left then
  61. -- Left
  62. if nPos > 0 then
  63. nPos = nPos - 1
  64. redraw()
  65. end
  66.  
  67. elseif param == keys.right then
  68. -- Right
  69. if nPos < string.len(sLine) then
  70. nPos = nPos + 1
  71. redraw()
  72. end
  73.  
  74. elseif param == keys.up or param == keys.down then
  75. -- Up or down
  76. if _tHistory then
  77. if param == keys.up then
  78. -- Up
  79. if nHistoryPos == nil then
  80. if #_tHistory > 0 then
  81. nHistoryPos = #_tHistory
  82. end
  83. elseif nHistoryPos > 1 then
  84. nHistoryPos = nHistoryPos - 1
  85. end
  86. else
  87. -- Down
  88. if nHistoryPos == #_tHistory then
  89. nHistoryPos = nil
  90. elseif nHistoryPos ~= nil then
  91. nHistoryPos = nHistoryPos + 1
  92. end
  93. end
  94.  
  95. if nHistoryPos then
  96. sLine = _tHistory[nHistoryPos]
  97. nPos = string.len( sLine )
  98. else
  99. sLine = ""
  100. nPos = 0
  101. end
  102. redraw()
  103. end
  104. elseif param == keys.backspace then
  105. -- Backspace
  106. if nPos > 0 then
  107. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  108. nPos = nPos - 1
  109. redraw()
  110. end
  111. end
  112. end
  113. end
  114.  
  115. term.setCursorBlink( false )
  116. term.setCursorPos( w + 1, sy )
  117. print()
  118.  
  119. return sLine
  120. end
  121.  
  122. mWrite = function(text)
  123. length1 = #("| |")
  124. length2 = math.ceil((x /2) - (length1 / 2))
  125. local x2,y2 = term.getCursorPos()
  126. term.setCursorPos(length2+2, y2)
  127.  
  128. write(text)
  129. end
  130.  
  131. local _write = function(text)
  132. multiMemory[#multiMemory+1] = text
  133. end
  134.  
  135. function newevent( _sFilter )
  136. local event, p1, p2, p3, p4, p5 = oldevent( _sFilter )
  137. if event == "key" then
  138. if p1 == 15 then
  139. if tabbing == false then
  140. tabbing = true
  141. thattimer = os.startTimer(0.7)
  142. else
  143. tabbing = false
  144. isMultitasking = true
  145. while isMultitasking do
  146. term.setCursorPos(1,3)
  147. cPrint("+------Simulated_Shell------+")
  148. cPrint("| |")
  149. cPrint("| |")
  150. cPrint("| |")
  151. cPrint("| |")
  152. cPrint("| |")
  153. cPrint("| |")
  154. cPrint("+---------------------------+")
  155. local testPos1 = 1
  156. if #multiMemory < 5 then
  157. for i=1,5 do
  158. if multiMemory[i] then
  159. term.setCursorPos(1, 3+i)
  160. mWrite(string.sub(multiMemory[i], 1, 26))
  161. testPos1 = testPos1+1
  162. end
  163. end
  164. else
  165. for i=#multiMemory-4,#multiMemory do
  166. if multiMemory[i] then
  167. term.setCursorPos(1, 3+testPos1)
  168. mWrite(string.sub(multiMemory[i], 1, 26))
  169. testPos1 = testPos1+1
  170. end
  171. end
  172. end
  173. term.setCursorPos(1, 3+testPos1)
  174. mWrite("> ")
  175. local request = mRead()
  176. _write("> " ..request)
  177. if request ~= "" then
  178. _write("Not yet implemented")
  179. end
  180. end
  181. end
  182. else
  183. tabbing = false
  184. end
  185. elseif event == "timer" and p1 == thattimer then
  186. tabbing = false
  187. end
  188. return event, p1, p2, p3, p4, p5
  189. end
  190.  
  191. print("Loading multitasker... ")
  192. textutils.slowPrint("Creating new events...")
  193. sleep(0)
  194. textutils.slowPrint("Generating event function...")
  195. sleep(0)
  196. textutils.slowPrint("Overwriting old events...")
  197. sleep(0)
  198. textutils.slowPrint("Done!")
  199. print("Multitasker loaded. Version: 1.0 PR1")
  200. print("Run a program and double press Tab to multitask.")
  201.  
  202. os.pullEventRaw = newevent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement