Advertisement
xXm0dzXx

OS Test

Nov 24th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. local windows = {}
  2. local clipboard = ""
  3. local oldSize = term.getSize
  4. local oldClear = term.clear
  5.  
  6. term.forceCursorPos = term.setCursorPos
  7. oldClearLine = term.clearLine
  8.  
  9. term.paste = function()
  10. return os.queueEvent( "char", clipboard )
  11. end
  12.  
  13. term.clear = function()
  14. local x,y = term.getCursorPos()
  15. oldClear()
  16. statusBar()
  17. term.setCursorPos(x,y)
  18. end
  19.  
  20. term.copy = function( text )
  21. clipboard = text
  22. end
  23.  
  24. term.getClipboard = function()
  25. return clipboard
  26. end
  27.  
  28. term.getWindows = function()
  29. return windows
  30. end
  31.  
  32. term.getSize = function()
  33. local x,y = oldSize()
  34. return x,y-2
  35. end
  36.  
  37. read = function( _sReplaceChar, _tHistory )
  38. term.setCursorBlink( true )
  39.  
  40. local cStart = 0
  41. local cEnd = 0
  42. local sLine = ""
  43. local nHistoryPos = nil
  44. local nPos = 0
  45. if _sReplaceChar then
  46. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  47. end
  48.  
  49. local w, h = term.getSize()
  50. local sx, sy = term.getCursorPos()
  51.  
  52. local function redraw( _sCustomReplaceChar )
  53. local nScroll = 0
  54. if sx + nPos >= w then
  55. nScroll = (sx + nPos) - w
  56. end
  57.  
  58. term.setCursorPos( sx, sy )
  59. local sReplace = _sCustomReplaceChar or _sReplaceChar
  60. if sReplace then
  61. sText = string.rep(sReplace, string.len(sLine) - nScroll)
  62. else
  63. sText = string.sub( sLine, nScroll + 1 )
  64. end
  65.  
  66. if cStart ~= 0 and cEnd ~= 0 then
  67. local sText1 = string.sub( sText, 1, cStart-1 )
  68. local sHigh = string.sub( sText, cStart, cEnd )
  69. local sText2 = string.sub( sText, cEnd+1, #sText )
  70. if sText1 then
  71. write( sText1 )
  72. end
  73. term.setBackgroundColour( colors.blue )
  74. if sHigh then
  75. write( sHigh )
  76. end
  77. term.setBackgroundColour( colors.black )
  78. if sText2 then
  79. write( sText2 )
  80. end
  81. else
  82. write( sText )
  83. end
  84.  
  85. term.setCursorPos( sx + nPos - nScroll, sy )
  86. end
  87.  
  88. local dragTimer = false
  89. while true do
  90. local sEvent, param, key, p2 = os.pullEvent()
  91. if sEvent == "char" then
  92. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  93. nPos = nPos + 1
  94. redraw()
  95.  
  96. elseif sEvent == "mouse_drag" then
  97. local p1 = key
  98. local cPos = p1-sx+1
  99. print( "Mouse dragged (left) " ..p1.. ":" ..p2.. " (" ..p1.. ")")
  100.  
  101. if cPos > 0 then
  102. if dragTimer == false then
  103. dragTimer = os.startTimer(0.9)
  104. cStart = cPos
  105. os.reboot()
  106. end
  107.  
  108. cEnd = cPos
  109. end
  110.  
  111. elseif sEvent == "timer" then
  112. if param == dragTimer then
  113. dragTimer = false
  114. end
  115.  
  116. elseif sEvent == "key" then
  117. if param == keys.enter then
  118. -- Enter
  119. break
  120.  
  121. elseif param == keys.left then
  122. -- Left
  123. if nPos > 0 then
  124. nPos = nPos - 1
  125. redraw()
  126. end
  127.  
  128. elseif param == keys.right then
  129. -- Right
  130. if nPos < string.len(sLine) then
  131. nPos = nPos + 1
  132. redraw()
  133. end
  134.  
  135. elseif param == keys.up or param == keys.down then
  136. -- Up or down
  137. if _tHistory then
  138. redraw(" ");
  139. if param == keys.up then
  140. -- Up
  141. if nHistoryPos == nil then
  142. if #_tHistory > 0 then
  143. nHistoryPos = #_tHistory
  144. end
  145. elseif nHistoryPos > 1 then
  146. nHistoryPos = nHistoryPos - 1
  147. end
  148. else
  149. -- Down
  150. if nHistoryPos == #_tHistory then
  151. nHistoryPos = nil
  152. elseif nHistoryPos ~= nil then
  153. nHistoryPos = nHistoryPos + 1
  154. end
  155. end
  156.  
  157. if nHistoryPos then
  158. sLine = _tHistory[nHistoryPos]
  159. nPos = string.len( sLine )
  160. else
  161. sLine = ""
  162. nPos = 0
  163. end
  164. redraw()
  165. end
  166. elseif param == keys.backspace then
  167. -- Backspace
  168. if nPos > 0 then
  169. redraw(" ");
  170. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  171. nPos = nPos - 1
  172. redraw()
  173. end
  174. elseif param == keys.home then
  175. -- Home
  176. nPos = 0
  177. redraw()
  178. elseif param == keys.delete then
  179. if nPos < string.len(sLine) then
  180. redraw(" ");
  181. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  182. redraw()
  183. end
  184. elseif param == keys["end"] then
  185. -- End
  186. nPos = string.len(sLine)
  187. redraw()
  188. end
  189. end
  190. end
  191.  
  192. term.setCursorBlink( false )
  193. term.setCursorPos( w + 1, sy )
  194. print()
  195.  
  196. return sLine
  197. end
  198.  
  199. function statusBar()
  200. term.setBackgroundColour( colors.black )
  201. local x,y = oldSize()
  202. local function cPrint( txt ) --Version 2.0 of cPrint
  203. local function printC( text )
  204. x2,y2 = term.getCursorPos()
  205. term.forceCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  206. write(text.. "\n")
  207. end
  208.  
  209. if type(txt) == "string" then
  210. printC( txt )
  211. elseif type(txt) == "table" then
  212. for i=1,#txt do
  213. printC( txt[i] )
  214. end
  215. end
  216. end
  217.  
  218. term.forceCursorPos(5,y-1)
  219. write( string.rep( "_", x-8 ) )
  220.  
  221. term.forceCursorPos(4,y)
  222. term.clearLine()
  223. write("/")
  224. term.forceCursorPos( x-3, y )
  225. write("\\")
  226.  
  227. if #windows ~= 0 then
  228. cPrint( string.rep( "o", #windows-1 ).. "o" )
  229. end
  230. end
  231.  
  232. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement