Advertisement
Guest User

util.lua

a guest
Oct 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.85 KB | None | 0 0
  1. function prompt( _tOptions )
  2.     local tVerify = {replaceChar = "string", history = "table", complete = "function", prefix = "string", limit = "number", newline = "boolean", completeBGColor = "number", completeTextColor = "number", filter = "function", customkeys = "table", writer = "function"}
  3.     if not _tOptions then
  4.         _tOptions = {}
  5.     end
  6.     for k, v in pairs(tVerify) do
  7.         if _tOptions[k] ~= nil and type( _tOptions[k] ) ~= v then
  8.             error( "bad argument " .. k .. " (expected " .. v .. " got ".. type( _tOptions[k] ) .. ")", 2)
  9.         end
  10.     end
  11.    
  12.     term.setCursorBlink( true )
  13.  
  14.     local sLine
  15.     if type( _tOptions.prefix ) == "string" then
  16.         sLine = _tOptions.prefix
  17.     else
  18.         sLine = ""
  19.     end
  20.     local nhistoryPos
  21.     local nPos = #sLine
  22.     if _tOptions.replaceChar then
  23.         _tOptions.replaceChar = string.sub( _tOptions.replaceChar, 1, 1 )
  24.     end
  25.     if not _tOptions.customkeys then
  26.         _tOptions.customkeys = {}
  27.     end
  28.     local tCustomKeyNames = {enter = keys.enter, up = keys.up, down = keys.down, left = keys.left, right = keys.right, backspace = keys.backspace, home = keys.home, delete = keys.delete, tab = keys.tab, ["end"] = keys["end"]}
  29.     for k, v in pairs(tCustomKeyNames) do
  30.         if not _tOptions.customkeys[k] then
  31.             _tOptions.customkeys[k] = v
  32.         end
  33.     end
  34.  
  35.     local tCompletions
  36.     local nCompletion
  37.     local function recomplete()
  38.         if _tOptions.complete and nPos == string.len(sLine) then
  39.             tCompletions = _tOptions.complete( sLine )
  40.             if tCompletions and #tCompletions > 0 then
  41.                 nCompletion = 1
  42.             else
  43.                 nCompletion = nil
  44.             end
  45.         else
  46.             tCompletions = nil
  47.             nCompletion = nil
  48.         end
  49.     end
  50.  
  51.     local function uncomplete()
  52.         tCompletions = nil
  53.         nCompletion = nil
  54.     end
  55.    
  56.     local sx = term.getCursorPos()
  57.    
  58.     local w
  59.     if not _tOptions.limit then
  60.         w = term.getSize()
  61.     else
  62.         w = _tOptions.limit+sx
  63.     end
  64.    
  65.     local writeFunc = term.write
  66.    
  67.     if _tOptions.writer then
  68.         writeFunc = _tOptions.writer
  69.     end
  70.  
  71.     local function redraw( _bClear )
  72.         local nScroll = 0
  73.         if sx + nPos >= w then
  74.             nScroll = (sx + nPos) - w
  75.         end
  76.  
  77.         local cx,cy = term.getCursorPos()
  78.         term.setCursorPos( sx, cy )
  79.         local sReplace = (_bClear and " ") or _tOptions.replaceChar
  80.         if sReplace then
  81.             writeFunc( string.sub( string.rep( sReplace, math.max( string.len(sLine) + 1, 0 ) ),  nScroll + 1, nScroll + w ) )
  82.         else
  83.             writeFunc( string.sub( sLine, nScroll + 1, nScroll + w ) )
  84.         end
  85.  
  86.         if nCompletion then
  87.             local sCompletion = tCompletions[ nCompletion ]
  88.             local oldText, oldBg
  89.             if not _bClear then
  90.                 oldText = term.getTextColor()
  91.                 oldBg = term.getBackgroundColor()
  92.                 if not _tOptions.completeTextColor then
  93.                     term.setTextColor( colors.white )
  94.                 else
  95.                     term.setTextColor( _tOptions.completeTextColor )
  96.                 end
  97.                 if not _tOptions.completeBGColor then
  98.                     term.setBackgroundColor( colors.gray )
  99.                 else
  100.                     term.setBackgroundColor( _tOptions.completeBGColor )
  101.                 end
  102.             end
  103.             if sReplace then
  104.                 writeFunc( string.rep( sReplace, string.len( sCompletion ) ) )
  105.             else
  106.                 writeFunc( sCompletion )
  107.             end
  108.             if not _bClear then
  109.                 term.setTextColor( oldText )
  110.                 term.setBackgroundColor( oldBg )
  111.             end
  112.         end
  113.  
  114.         term.setCursorPos( sx + nPos - nScroll, cy )
  115.     end
  116.    
  117.     local function clear()
  118.         redraw( true )
  119.     end
  120.    
  121.     recomplete()
  122.     redraw()
  123.  
  124.     local function acceptCompletion()
  125.         if nCompletion then
  126.             -- Clear
  127.             clear()
  128.  
  129.             -- Find the common prefix of all the other suggestions which start with the same letter as the current one
  130.             local sCompletion = tCompletions[ nCompletion ]
  131.             sLine = sLine .. sCompletion
  132.             nPos = string.len( sLine )
  133.  
  134.             -- Redraw
  135.             recomplete()
  136.             redraw()
  137.         end
  138.     end
  139.     while true do
  140.         local sEvent, param = os.pullEvent()
  141.  
  142.         if sEvent == "char" then
  143.             -- Typed key
  144.             clear()
  145.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  146.             nPos = nPos + 1
  147.             recomplete()
  148.             redraw()
  149.  
  150.         elseif sEvent == "paste" then
  151.             -- Pasted text
  152.             clear()
  153.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  154.             nPos = nPos + string.len( param )
  155.             recomplete()
  156.             redraw()
  157.  
  158.         elseif sEvent == "key" then
  159.             if _tOptions.customkeys.enter == param then
  160.                 -- Enter
  161.                 if nCompletion then
  162.                     clear()
  163.                     uncomplete()
  164.                     redraw()
  165.                 end
  166.                 break
  167.                
  168.             elseif _tOptions.customkeys.left == param then
  169.                 -- Left
  170.                 if nPos > 0 then
  171.                     clear()
  172.                     nPos = nPos - 1
  173.                     recomplete()
  174.                     redraw()
  175.                 end
  176.                
  177.             elseif _tOptions.customkeys.right == param then
  178.                 -- Right                
  179.                 if nPos < string.len(sLine) then
  180.                     -- Move right
  181.                     clear()
  182.                     nPos = nPos + 1
  183.                     recomplete()
  184.                     redraw()
  185.                 else
  186.                     -- Accept autocomplete
  187.                     acceptCompletion()
  188.                 end
  189.  
  190.             elseif  _tOptions.customkeys.up == param or _tOptions.customkeys.down == param then
  191.                 -- Up or down
  192.                 if nCompletion then
  193.                     -- Cycle completions
  194.                     clear()
  195.                     if _tOptions.customkeys.up == param then
  196.                         nCompletion = nCompletion - 1
  197.                         if nCompletion < 1 then
  198.                             nCompletion = #tCompletions
  199.                         end
  200.                     elseif _tOptions.customkeys.down == param then
  201.                         nCompletion = nCompletion + 1
  202.                         if nCompletion > #tCompletions then
  203.                             nCompletion = 1
  204.                         end
  205.                     end
  206.                     redraw()
  207.  
  208.                 elseif _tOptions.history then
  209.                     -- Cycle history
  210.                     clear()
  211.                     if _tOptions.customkeys.up == param then
  212.                         -- Up
  213.                         if nhistoryPos == nil then
  214.                             if #_tOptions.history > 0 then
  215.                                 nhistoryPos = #_tOptions.history
  216.                             end
  217.                         elseif nhistoryPos > 1 then
  218.                             nhistoryPos = nhistoryPos - 1
  219.                         end
  220.                     elseif _tOptions.customkeys.down == param then
  221.                         -- Down
  222.                         if nhistoryPos == #_tOptions.history then
  223.                             nhistoryPos = nil
  224.                         elseif nhistoryPos ~= nil then
  225.                             nhistoryPos = nhistoryPos + 1
  226.                         end                        
  227.                     end
  228.                     if nhistoryPos then
  229.                         sLine = _tOptions.history[nhistoryPos]
  230.                         nPos = string.len( sLine )
  231.                     else
  232.                         sLine = ""
  233.                         nPos = 0
  234.                     end
  235.                     uncomplete()
  236.                     redraw()
  237.  
  238.                 end
  239.  
  240.             elseif _tOptions.customkeys.backspace == param then
  241.                 -- Backspace
  242.                 if nPos > 0 then
  243.                     clear()
  244.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  245.                     nPos = nPos - 1
  246.                     recomplete()
  247.                     redraw()
  248.                 end
  249.  
  250.             elseif _tOptions.customkeys.home == param then
  251.                 -- Home
  252.                 if nPos > 0 then
  253.                     clear()
  254.                     nPos = 0
  255.                     recomplete()
  256.                     redraw()
  257.                 end
  258.  
  259.             elseif _tOptions.customkeys.delete == param then
  260.                 -- Delete
  261.                 if nPos < string.len(sLine) then
  262.                     clear()
  263.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  264.                     recomplete()
  265.                     redraw()
  266.                 end
  267.  
  268.             elseif _tOptions.customkeys["end"] == param then
  269.                 -- End
  270.                 if nPos < string.len(sLine ) then
  271.                     clear()
  272.                     nPos = string.len(sLine)
  273.                     recomplete()
  274.                     redraw()
  275.                 end
  276.  
  277.             elseif _tOptions.customkeys.tab == param then
  278.                 -- Tab (accept autocomplete)
  279.                 acceptCompletion()
  280.  
  281.             end
  282.        
  283.         elseif sEvent == "term_resize" then
  284.             -- Terminal resized
  285.             if not _tOptions.limit then
  286.                 w = term.getSize()
  287.             else
  288.                 w = _tOptions.limit
  289.             end
  290.             redraw()
  291.  
  292.         end
  293.  
  294.         if _tOptions.filter then
  295.             -- Filter out all unwanted characters/strings using a function defined by the user
  296.             local sPreFilterLine = sLine
  297.             sLine = _tOptions.filter( sLine )
  298.             if string.len( sPreFilterLine ) ~= string.len( sLine ) then
  299.                 local sPreClearLine = sLine
  300.                 sLine = sPreFilterLine
  301.                 clear()
  302.                 sLine = sPreClearLine
  303.             end
  304.             if not sLine then
  305.                 sLine = sPreFilterLine
  306.             else
  307.                 if nPos >= ( string.len( sPreFilterLine ) - string.len( sLine ) ) then
  308.                     nPos = nPos - ( string.len( sPreFilterLine ) - string.len( sLine ) )
  309.                 else
  310.                     nPos = 0
  311.                 end
  312.             end
  313.             redraw()
  314.         end
  315.     end
  316.  
  317.     local cx, cy = term.getCursorPos()
  318.     term.setCursorBlink( false )
  319.     if _tOptions.newline == nil or _tOptions.newline == true then
  320.         term.setCursorPos( 1, cy + 1)
  321.     end
  322.    
  323.     return sLine
  324. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement