CapsAdmin

Untitled

Jun 5th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1.  
  2.  
  3. do -- console input (but you have to use the main window.... for now)
  4.     local buffer = ""
  5.     local history = {}
  6.     local i = 0
  7.    
  8.     function asdfml.OnTextEntered(params)
  9.         local str = string.char(params.text.unicode)
  10.        
  11.         if str and str:byte() >= 32 then
  12.             buffer = buffer .. str
  13.             io.write(str)
  14.         end
  15.     end
  16.    
  17.     local caret_position = 0
  18.    
  19.     local function set_caret_pos(pos)      
  20.         -- let's say pos should be somewhere at #buffer * 0.75
  21.        
  22.         -- our carret position is here
  23.         --  1 2 3 4 5 6 7 8 9 |
  24.        
  25.         -- since it may not be we need to add any offsets
  26.         pos = pos + caret_position
  27.        
  28.         -- when we add 9 (the length of the buffer) stars (\b) it will go back to the start
  29.         -- | 1 2 3 4 5 6 7 8 9 * * * * * * * * *
  30.        
  31.         -- so the length of the line + the start position
  32.         -- would be the position from the beginning
  33.        
  34.         -- but if we add one more, it will go even further out
  35.         -- because it's in /reverse/
  36.            
  37.         pos = -pos
  38.        
  39.         -- so if we reverse it, we should get it somewhere here
  40.         --  1 2 3 4 5 6 | 7 8 9  
  41.        
  42.         io.write(("\b"):rep(#buffer + pos))    
  43.         caret_position = pos
  44.     end
  45.    
  46.     local function remove(start, stop)
  47.         if not start and not stop then
  48.             start = 0
  49.             stop = #buffer
  50.         end
  51.                
  52.     --  io.write(("\b"):rep(start#buffer), (" "):rep((#buffer%start) + #buffer%stop), ("\b"):rep(#buffer%start), buffer)
  53.     end
  54.  
  55.     function asdfml.OnKeyPressed(params)
  56.         local key = params.key.code
  57.        
  58.         if key == e.KEY_END then
  59.             set_caret_pos(#buffer*0.75)
  60.         end
  61.        
  62.         if key == e.KEY_LEFT then
  63.             io.write("\b")
  64.         elseif key == e.KEY_RIGHT then
  65.             io.write(buffer:sub(0,1):rep(2), "\b")
  66.         end
  67.        
  68.         if key == e.KEY_UP then
  69.             i = i - 1
  70.             local prev = buffer
  71.             buffer = history[i%#history+1]
  72.             io.write(("\b"):rep(#prev), (" "):rep(#prev), ("\b"):rep(#prev), buffer)
  73.         elseif key == e.KEY_DOWN then
  74.             i = i + 1
  75.             local prev = buffer
  76.             buffer = history[i%#history+1]
  77.             io.write(("\b"):rep(#prev), (" "):rep(#prev), ("\b"):rep(#prev), buffer)
  78.         end
  79.        
  80.         if key == e.KEY_BACK then
  81.             buffer = buffer:sub(0, -2)
  82.             io.write("\b \b")
  83.         end
  84.                
  85.         if key == e.KEY_RETURN then
  86.             io.write("\n")
  87.            
  88.             local ok, err = console.CallCommandLine(buffer)
  89.            
  90.             if not ok then
  91.                 print(err)
  92.             end
  93.            
  94.             table.insert(history, buffer)
  95.            
  96.             buffer = ""
  97.         end
  98.     end
  99.    
  100.     console.AddCommand("lua_run", function(...)
  101.         print(...)
  102.     end)
  103. end
Advertisement
Add Comment
Please, Sign In to add comment