Theshadow989

Test File 5 (multi-thread)

May 2nd, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. function read( _sReplaceChar, _tHistory )    
  2.     term.setCursorBlink( true )
  3.  
  4.     local sLine = ""
  5.     local nHistoryPos = nil
  6.     local nPos = 0
  7.     if _sReplaceChar then
  8.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  9.     end
  10.    
  11.     local w, h = term.getSize()
  12.     local sx, sy = term.getCursorPos()    
  13.     local function redraw()
  14.         local nScroll = 0
  15.         if sx + nPos >= w then
  16.             nScroll = (sx + nPos) - w
  17.         end
  18.            
  19.         term.setCursorPos( sx, sy )
  20.         term.write( string.rep(" ", w - sx + 1) )
  21.         term.setCursorPos( sx, sy )
  22.         if _sReplaceChar then
  23.             term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  24.         else
  25.             term.write( string.sub( sLine, nScroll + 1 ) )
  26.         end
  27.         term.setCursorPos( sx + nPos - nScroll, sy )
  28.     end
  29.    
  30.     while true do
  31.         local sEvent, param = os.pullEvent()
  32.         if sEvent == "char" then
  33.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  34.             nPos = nPos + 1
  35.             redraw()
  36.            
  37.     elseif sEvent == "timer" and param == TimeDisplay then
  38.           DisplayTime()
  39.         elseif sEvent == "key" then
  40.          if param == 28 then
  41.                 -- Enter
  42.                 break
  43.                
  44.             elseif param == 203 then
  45.                 -- Left
  46.                 if nPos > 0 then
  47.                     nPos = nPos - 1
  48.                     redraw()
  49.                 end
  50.                
  51.             elseif param == 205 then
  52.                 -- Right                
  53.                 if nPos < string.len(sLine) then
  54.                     nPos = nPos + 1
  55.                     redraw()
  56.                 end
  57.            
  58.             elseif param == 200 or param == 208 then
  59.                             -- Up or down
  60.                 if _tHistory then
  61.                     if param == 200 then
  62.                         -- Up
  63.                         if nHistoryPos == nil then
  64.                             if #_tHistory > 0 then
  65.                                 nHistoryPos = #_tHistory
  66.                             end
  67.                         elseif nHistoryPos > 1 then
  68.                             nHistoryPos = nHistoryPos - 1
  69.                         end
  70.                     else
  71.                         -- Down
  72.                         if nHistoryPos == #_tHistory then
  73.                             nHistoryPos = nil
  74.                         elseif nHistoryPos ~= nil then
  75.                             nHistoryPos = nHistoryPos + 1
  76.                         end                        
  77.                     end
  78.                    
  79.                     if nHistoryPos then
  80.                                         sLine = _tHistory[nHistoryPos]
  81.                                         nPos = string.len( sLine )
  82.                                     else
  83.                         sLine = ""
  84.                         nPos = 0
  85.                     end
  86.                     redraw()
  87.                             end
  88.             elseif param == 14 then
  89.                 -- Backspace
  90.                 if nPos > 0 then
  91.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  92.                     nPos = nPos - 1                    
  93.                     redraw()
  94.                 end
  95.             end
  96.         end
  97.     end
  98.    
  99.     term.setCursorBlink( false )
  100.     term.setCursorPos( w + 1, sy )
  101.     print()
  102.    
  103.     return sLine
  104. end
  105.  
  106. function DisplayTime()
  107. local x, y = term.getCursorPos()
  108. local nTime = os.time()
  109. term.setCursorPos(1, 1)
  110. print ("It is "..textutils.formatTime(nTime, bTwentyFourHour))
  111. TimeDisplay = os.startTimer(1)
  112. term.setCursorPos(x,y)
  113. end
  114.  
  115. while true do
  116.     DisplayTime()
  117.     term.setCursorPos(4,4)
  118.     term.write("Logged in as: ")
  119.     input = read()
  120. end
Add Comment
Please, Sign In to add comment