Advertisement
Guest User

fix.lua

a guest
Sep 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. function read(_sDefault )
  2.     if _sDefault ~= nil and type( _sDefault ) ~= "string" then
  3.         error( "bad argument #1 (expected string, got " .. type( _sDefault ) .. ")", 2 )
  4.     end
  5.     term.setCursorBlink( true )
  6.  
  7.     local sLine
  8.     if type( _sDefault ) == "string" then
  9.         sLine = _sDefault
  10.     else
  11.         sLine = ""
  12.     end
  13.     local nHistoryPos
  14.     local nPos = #sLine
  15.  
  16.     local w = term.getSize()
  17.     local sx = term.getCursorPos()
  18.  
  19.     local function redraw( _bClear )
  20.         local nScroll = 0
  21.         if sx + nPos >= w then
  22.             nScroll = (sx + nPos) - w
  23.         end
  24.  
  25.         local cx,cy = term.getCursorPos()
  26.         term.setCursorPos( sx, cy )
  27.         local sReplace = (_bClear and " ") or _sReplaceChar
  28.         if sReplace then
  29.             term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
  30.         else
  31.             term.write( string.sub( sLine, nScroll + 1 ) )
  32.         end
  33.         term.restoreCursor()
  34.         term.setCursorPos( sx + nPos - nScroll, cy )
  35.     end
  36.    
  37.     local function clear()
  38.         redraw( true )
  39.     end
  40.  
  41.     while true do
  42.         local sEvent, param = os.pullEvent()
  43.         if sEvent == "char" then
  44.             -- Typed key
  45.             clear()
  46.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  47.             nPos = nPos + 1
  48.             redraw()
  49.  
  50.         elseif sEvent == "paste" then
  51.             -- Pasted text
  52.             clear()
  53.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  54.             nPos = nPos + string.len( param )
  55.             redraw()
  56.  
  57.         elseif sEvent == "key" then
  58.             if param == keys.enter then
  59.                 break
  60.                
  61.             elseif param == keys.left then
  62.                 -- Left
  63.                 if nPos > 0 then
  64.                     clear()
  65.                     nPos = nPos - 1
  66.                     recomplete()
  67.                     redraw()
  68.                 end
  69.                
  70.             elseif param == keys.right then
  71.                 -- Right                
  72.                 if nPos < string.len(sLine) then
  73.                     -- Move right
  74.                     clear()
  75.                     nPos = nPos + 1
  76.                     recomplete()
  77.                     redraw()
  78.                 else
  79.                     -- Accept autocomplete
  80.                     acceptCompletion()
  81.                 end
  82.  
  83.    
  84.             elseif param == keys.backspace then
  85.                 -- Backspace
  86.                 if nPos > 0 then
  87.                     clear()
  88.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  89.                     nPos = nPos - 1
  90.                     recomplete()
  91.                     redraw()
  92.                 end
  93.  
  94.             elseif param == keys.home then
  95.                 -- Home
  96.                 if nPos > 0 then
  97.                     clear()
  98.                     nPos = 0
  99.                     recomplete()
  100.                     redraw()
  101.                 end
  102.  
  103.             elseif param == keys.delete then
  104.                 -- Delete
  105.                 if nPos < string.len(sLine) then
  106.                     clear()
  107.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )                
  108.                     recomplete()
  109.                     redraw()
  110.                 end
  111.  
  112.             elseif param == keys["end"] then
  113.                 -- End
  114.                 if nPos < string.len(sLine ) then
  115.                     clear()
  116.                     nPos = string.len(sLine)
  117.                     recomplete()
  118.                     redraw()
  119.                 end
  120.  
  121.             end
  122.  
  123.         elseif sEvent == "term_resize" then
  124.             -- Terminal resized
  125.             w = term.getSize()
  126.             redraw()
  127.  
  128.         end
  129.     end
  130.  
  131.     local cx, cy = term.getCursorPos()
  132.     term.setCursorBlink( false )
  133.     term.setCursorPos( w + 1, cy )
  134.     print()
  135.    
  136.     return sLine
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement