Advertisement
Stiepen

/etc/tweaks - Lib for KiloShell craftos port

Apr 28th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.44 KB | None | 0 0
  1. function read( _sReplaceChar, _tHistory, _fEventCallback, _fRedraw )
  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.    
  14.     local function redraw2( _sCustomReplaceChar )
  15.         local nScroll = 0
  16.         if sx + nPos >= w then
  17.             nScroll = (sx + nPos) - w
  18.         end
  19.            
  20.         term.setCursorPos( sx, sy )
  21.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  22.         if sReplace then
  23.             term.write( string.rep(sReplace, 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.     local redraw
  31.     if _fRedraw then
  32.       redraw = function(cc)
  33.         local pos, line = _fRedraw(cc, redraw2, nPos, sLine, sx, sy)
  34.         nPos = pos or nPos
  35.         sLine = line or sLine
  36.         if red then
  37.           redraw()
  38.         end
  39.       end
  40.     else
  41.       redraw = redraw2
  42.     end
  43.    
  44.     while true do
  45.         local sEvent, param = os.pullEvent()
  46.         if sEvent == "char" then
  47.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  48.             nPos = nPos + 1
  49.             redraw()
  50.            
  51.         elseif sEvent == "key" then
  52.             if param == keys.enter then
  53.                 -- Enter
  54.                 break
  55.                
  56.             elseif param == keys.left then
  57.                 -- Left
  58.                 if nPos > 0 then
  59.                     nPos = nPos - 1
  60.                     redraw()
  61.                 end
  62.                
  63.             elseif param == keys.right then
  64.                 -- Right                
  65.                 if nPos < string.len(sLine) then
  66.                     nPos = nPos + 1
  67.                     redraw()
  68.                 end
  69.            
  70.             elseif param == keys.up or param == keys.down then
  71.                 -- Up or down
  72.                 if _tHistory then
  73.                     redraw(" ");
  74.                     if param == keys.up then
  75.                         -- Up
  76.                         if nHistoryPos == nil then
  77.                             if #_tHistory > 0 then
  78.                                 nHistoryPos = #_tHistory
  79.                             end
  80.                         elseif nHistoryPos > 1 then
  81.                             nHistoryPos = nHistoryPos - 1
  82.                         end
  83.                     else
  84.                         -- Down
  85.                         if nHistoryPos == #_tHistory then
  86.                             nHistoryPos = nil
  87.                         elseif nHistoryPos ~= nil then
  88.                             nHistoryPos = nHistoryPos + 1
  89.                         end                    
  90.                     end
  91.                    
  92.                     if nHistoryPos then
  93.                         sLine = _tHistory[nHistoryPos]
  94.                         nPos = string.len( sLine )
  95.                     else
  96.                         sLine = ""
  97.                         nPos = 0
  98.                     end
  99.                     redraw()
  100.                 end
  101.             elseif param == keys.backspace then
  102.                 -- Backspace
  103.                 if nPos > 0 then
  104.                     redraw(" ");
  105.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  106.                     nPos = nPos - 1                
  107.                     redraw()
  108.                 end
  109.             elseif param == keys.home then
  110.                 -- Home
  111.                 nPos = 0
  112.                 redraw()        
  113.             elseif param == keys.delete then
  114.                 if nPos < string.len(sLine) then
  115.                     redraw(" ");
  116.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  117.                     redraw()
  118.                 end
  119.             elseif param == keys["end"] then
  120.                 -- End
  121.                 nPos = string.len(sLine)
  122.                 redraw()
  123.             elseif type(_fEventCallback) == "function" then
  124.                 local pos, line = _fEventCallback(sEvent, param, nPos, sLine, redraw, sx, sy)
  125.                 nPos = pos or nPos
  126.                 sLine = line or sLine
  127.                 redraw()
  128.             end
  129.         end
  130.     end
  131.    
  132.     term.setCursorBlink( false )
  133.     term.setCursorPos( w + 1, sy )
  134.     print()
  135.    
  136.     return sLine
  137. end
  138.  
  139. function setColor(c1, c2)
  140.   if term.isColor() then
  141.     if c1 then term.setTextColor(c1) end
  142.     if c2 then term.setBackgroundColor(c2) end
  143.   end
  144. end
  145.  
  146. function printColored(...)
  147.   writeColored(..., "\n")
  148. end
  149.  
  150. function writeColored(...)
  151.   local tmp = ""
  152.   for k, v in ipairs({...}) do
  153.     tmp = tmp..v
  154.   end
  155.   for match in tmp:gmatch("[^§]+") do
  156.     local c = match:sub(1,1):lower()
  157.     local c2, c3
  158.     local text = match:sub(2)
  159.     if c == "§" then
  160.       write("§"..text)
  161.     else
  162.       local colors = "0123456789abcdef"
  163.       for a = 1, colors:len() do
  164.         if c == colors:sub(a,a) then
  165.           c2 = 2^(a - 1)
  166.         end
  167.       end
  168.       text = text:sub(2)
  169.       c = match:sub(2,2):lower()
  170.       for a = 1, colors:len() do
  171.         if c == colors:sub(a,a) then
  172.           c3 = 2^(a - 1)
  173.         end
  174.       end
  175.       setColor(c2, c3)
  176.       write(text)
  177.     end
  178.   end
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement