Advertisement
pixellover2

textutils

Jun 13th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.18 KB | None | 0 0
  1.  
  2. function slowWrite( sText, nRate )
  3.     nRate = nRate or 20
  4.     if nRate < 0 then
  5.         error( "Rate must be positive", 2 )
  6.     end
  7.     local nSleep = 1 / nRate
  8.        
  9.     sText = tostring( sText )
  10.     local x,y = term.getCursorPos(x,y)
  11.     local len = string.len( sText )
  12.    
  13.     for n=1,len do
  14.         term.setCursorPos( x, y )
  15.         sleep( nSleep )
  16.         local nLines = write( string.sub( sText, 1, n ) )
  17.         local newX, newY = term.getCursorPos()
  18.         y = newY - nLines
  19.     end
  20. end
  21.  
  22. function slowPrint( sText, nRate )
  23.     slowWrite( sText, nRate)
  24.     print()
  25. end
  26.  
  27. function formatTime( nTime, bTwentyFourHour )
  28.     local sTOD = nil
  29.     if not bTwentyFourHour then
  30.         if nTime >= 12 then
  31.             sTOD = "PM"
  32.         else
  33.             sTOD = "AM"
  34.         end
  35.         if nTime >= 13 then
  36.             nTime = nTime - 12
  37.         end
  38.     end
  39.  
  40.     local nHour = math.floor(nTime)
  41.     local nMinute = math.floor((nTime - nHour)*60)
  42.     if sTOD then
  43.         return string.format( "%d:%02d %s", nHour, nMinute, sTOD )
  44.     else
  45.         return string.format( "%d:%02d", nHour, nMinute )
  46.     end
  47. end
  48.  
  49. local function makePagedScroll( _term, _nFreeLines )
  50.     local nativeScroll = _term.scroll
  51.     local nFreeLines = _nFreeLines or 0
  52.     return function( _n )
  53.         for n=1,_n do
  54.             nativeScroll( 1 )
  55.            
  56.             if nFreeLines <= 0 then
  57.                 local w,h = _term.getSize()
  58.                 _term.setCursorPos( 1, h )
  59.                 _term.write( "Press any key to continue" )
  60.                 os.pullEvent( "key" )
  61.                 _term.clearLine()
  62.                 _term.setCursorPos( 1, h )
  63.             else
  64.                 nFreeLines = nFreeLines - 1
  65.             end
  66.         end
  67.     end
  68. end
  69.  
  70. function pagedPrint( _sText, _nFreeLines )
  71.     -- Setup a redirector
  72.     local oldTerm = term.current()
  73.     local newTerm = {}
  74.     for k,v in pairs( oldTerm ) do
  75.         newTerm[k] = v
  76.     end
  77.     newTerm.scroll = makePagedScroll( oldTerm, _nFreeLines )
  78.     term.redirect( newTerm )
  79.  
  80.     -- Print the text
  81.     local result
  82.     local ok, err = pcall( function()
  83.         result = print( _sText )
  84.     end )
  85.  
  86.     -- Removed the redirector
  87.     term.redirect( oldTerm )
  88.  
  89.     -- Propogate errors
  90.     if not ok then
  91.         error( err, 0 )
  92.     end
  93.     return result
  94. end
  95.  
  96. local function tabulateCommon( bPaged, ... )
  97.     local tAll = { ... }
  98.    
  99.     local w,h = term.getSize()
  100.     local nMaxLen = w / 8
  101.     for n, t in ipairs( tAll ) do
  102.         if type(t) == "table" then
  103.             for n, sItem in pairs(t) do
  104.                 nMaxLen = math.max( string.len( sItem ) + 1, nMaxLen )
  105.             end
  106.         end
  107.     end
  108.     local nCols = math.floor( w / nMaxLen )
  109.     local nLines = 0
  110.     local function newLine()
  111.         if bPaged and nLines >= (h-3) then
  112.             pagedPrint()
  113.         else
  114.             print()
  115.         end
  116.         nLines = nLines + 1
  117.     end
  118.    
  119.     local function drawCols( _t )
  120.         local nCol = 1
  121.         for n, s in ipairs( _t ) do
  122.             if nCol > nCols then
  123.                 nCol = 1
  124.                 newLine()
  125.             end
  126.  
  127.             local cx, cy = term.getCursorPos()
  128.             cx = 1 + ((nCol - 1) * nMaxLen)
  129.             term.setCursorPos( cx, cy )
  130.             term.write( s )
  131.  
  132.             nCol = nCol + 1      
  133.         end
  134.         print()
  135.     end
  136.     for n, t in ipairs( tAll ) do
  137.         if type(t) == "table" then
  138.             if #t > 0 then
  139.                 drawCols( t )
  140.             end
  141.         elseif type(t) == "number" then
  142.             term.setTextColor( t )
  143.         end
  144.     end    
  145. end
  146.  
  147. function tabulate( ... )
  148.     tabulateCommon( false, ... )
  149. end
  150.  
  151. function pagedTabulate( ... )
  152.     tabulateCommon( true, ... )
  153. end
  154.  
  155. local g_tLuaKeywords = {
  156.     [ "and" ] = true,
  157.     [ "break" ] = true,
  158.     [ "do" ] = true,
  159.     [ "else" ] = true,
  160.     [ "elseif" ] = true,
  161.     [ "end" ] = true,
  162.     [ "false" ] = true,
  163.     [ "for" ] = true,
  164.     [ "function" ] = true,
  165.     [ "if" ] = true,
  166.     [ "in" ] = true,
  167.     [ "local" ] = true,
  168.     [ "nil" ] = true,
  169.     [ "not" ] = true,
  170.     [ "or" ] = true,
  171.     [ "repeat" ] = true,
  172.     [ "return" ] = true,
  173.     [ "then" ] = true,
  174.     [ "true" ] = true,
  175.     [ "until" ] = true,
  176.     [ "while" ] = true,
  177. }
  178.  
  179. local function serializeImpl( t, tTracking, sIndent )
  180.     local sType = type(t)
  181.     if sType == "table" then
  182.         if tTracking[t] ~= nil then
  183.             error( "Cannot serialize table with recursive entries", 0 )
  184.         end
  185.         tTracking[t] = true
  186.  
  187.         if next(t) == nil then
  188.             -- Empty tables are simple
  189.             return "{}"
  190.         else
  191.             -- Other tables take more work
  192.             local sResult = "{\n"
  193.             local sSubIndent = sIndent .. "  "
  194.             local tSeen = {}
  195.             for k,v in ipairs(t) do
  196.                 tSeen[k] = true
  197.                 sResult = sResult .. sSubIndent .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  198.             end
  199.             for k,v in pairs(t) do
  200.                 if not tSeen[k] then
  201.                     local sEntry
  202.                     if type(k) == "string" and not g_tLuaKeywords[k] and string.match( k, "^[%a_][%a%d_]*$" ) then
  203.                         sEntry = k .. " = " .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  204.                     else
  205.                         sEntry = "[ " .. serializeImpl( k, tTracking, sSubIndent ) .. " ] = " .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  206.                     end
  207.                     sResult = sResult .. sSubIndent .. sEntry
  208.                 end
  209.             end
  210.             sResult = sResult .. sIndent .. "}"
  211.             return sResult
  212.         end
  213.        
  214.     elseif sType == "string" then
  215.         return string.format( "%q", t )
  216.    
  217.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  218.         return tostring(t)
  219.        
  220.     else
  221.         error( "Cannot serialize type "..sType, 0 )
  222.        
  223.     end
  224. end
  225.  
  226. empty_json_array = {}
  227.  
  228. local function serializeJSONImpl( t, tTracking, bNBTStyle )
  229.     local sType = type(t)
  230.     if t == empty_json_array then
  231.         return "[]"
  232.  
  233.     elseif sType == "table" then
  234.         if tTracking[t] ~= nil then
  235.             error( "Cannot serialize table with recursive entries", 0 )
  236.         end
  237.         tTracking[t] = true
  238.  
  239.         if next(t) == nil then
  240.             -- Empty tables are simple
  241.             return "{}"
  242.         else
  243.             -- Other tables take more work
  244.             local sObjectResult = "{"
  245.             local sArrayResult = "["
  246.             local nObjectSize = 0
  247.             local nArraySize = 0
  248.             for k,v in pairs(t) do
  249.                 if type(k) == "string" then
  250.                     local sEntry
  251.                     if bNBTStyle then
  252.                         sEntry = tostring(k) .. ":" .. serializeJSONImpl( v, tTracking, bNBTStyle )
  253.                     else
  254.                         sEntry = string.format( "%q", k ) .. ":" .. serializeJSONImpl( v, tTracking, bNBTStyle )
  255.                     end
  256.                     if nObjectSize == 0 then
  257.                         sObjectResult = sObjectResult .. sEntry
  258.                     else
  259.                         sObjectResult = sObjectResult .. "," .. sEntry
  260.                     end
  261.                     nObjectSize = nObjectSize + 1
  262.                 end
  263.             end
  264.             for n,v in ipairs(t) do
  265.                 local sEntry = serializeJSONImpl( v, tTracking, bNBTStyle )
  266.                 if nArraySize == 0 then
  267.                     sArrayResult = sArrayResult .. sEntry
  268.                 else
  269.                     sArrayResult = sArrayResult .. "," .. sEntry
  270.                 end
  271.                 nArraySize = nArraySize + 1
  272.             end
  273.             sObjectResult = sObjectResult .. "}"
  274.             sArrayResult = sArrayResult .. "]"
  275.             if nObjectSize > 0 or nArraySize == 0 then
  276.                 return sObjectResult
  277.             else
  278.                 return sArrayResult
  279.             end
  280.         end
  281.  
  282.     elseif sType == "string" then
  283.         return string.format( "%q", t )
  284.  
  285.     elseif sType == "number" or sType == "boolean" then
  286.         return tostring(t)
  287.  
  288.     else
  289.         error( "Cannot serialize type "..sType, 0 )
  290.  
  291.     end
  292. end
  293.  
  294. function serialize( t )
  295.     local tTracking = {}
  296.     return serializeImpl( t, tTracking, "" )
  297. end
  298.  
  299. function unserialize( s )
  300.     local func = load( "return "..s, "unserialize", "t", {} )
  301.     if func then
  302.         local ok, result = pcall( func )
  303.         if ok then
  304.             return result
  305.         end
  306.     end
  307.     return nil
  308. end
  309.  
  310. function serializeJSON( t, bNBTStyle )
  311.     local tTracking = {}
  312.     return serializeJSONImpl( t, tTracking, bNBTStyle or false )
  313. end
  314.  
  315. function urlEncode( str )
  316.     if str then
  317.         str = string.gsub(str, "\n", "\r\n")
  318.         str = string.gsub(str, "([^A-Za-z0-9 %-%_%.])", function(c)
  319.             local n = string.byte(c)
  320.             if n < 128 then
  321.                 -- ASCII
  322.                 return string.format("%%%02X", n)
  323.             else
  324.                 -- Non-ASCII (encode as UTF-8)
  325.                 return
  326.                     string.format("%%%02X", 192 + bit32.band( bit32.arshift(n,6), 31 ) ) ..
  327.                     string.format("%%%02X", 128 + bit32.band( n, 63 ) )
  328.             end
  329.         end )
  330.         str = string.gsub(str, " ", "+")
  331.     end
  332.     return str    
  333. end
  334.  
  335. local tEmpty = {}
  336. function complete( sSearchText, tSearchTable )
  337.     local nStart = 1
  338.     local nDot = string.find( sSearchText, ".", nStart, true )
  339.     local tTable = tSearchTable or _ENV
  340.     while nDot do
  341.         local sPart = string.sub( sSearchText, nStart, nDot - 1 )
  342.         local value = tTable[ sPart ]
  343.         if type( value ) == "table" then
  344.             tTable = value
  345.             nStart = nDot + 1
  346.             nDot = string.find( sSearchText, ".", nStart, true )
  347.         else
  348.             return tEmpty
  349.         end
  350.     end
  351.  
  352.     local sPart = string.sub( sSearchText, nStart, nDot )
  353.     local nPartLength = string.len( sPart )
  354.  
  355.     local tResults = {}
  356.     local tSeen = {}
  357.     while tTable do
  358.         for k,v in pairs( tTable ) do
  359.             if not tSeen[k] and type(k) == "string" then
  360.                 if string.find( k, sPart, 1, true ) == 1 then
  361.                     if not g_tLuaKeywords[k] and string.match( k, "^[%a_][%a%d_]*$" ) then
  362.                         local sResult = string.sub( k, nPartLength + 1 )
  363.                         if type(v) == "function" then
  364.                             sResult = sResult .. "("
  365.                         elseif type(v) == "table" and next(v) ~= nil then
  366.                             sResult = sResult .. "."
  367.                         end
  368.                         table.insert( tResults, sResult )
  369.                     end
  370.                 end
  371.             end
  372.             tSeen[k] = true
  373.         end
  374.         local tMetatable = getmetatable( tTable )
  375.         if tMetatable and type( tMetatable.__index ) == "table" then
  376.             tTable = tMetatable.__index
  377.         else
  378.             tTable = nil
  379.         end
  380.     end
  381.  
  382.     table.sort( tResults )
  383.     return tResults
  384. end
  385.  
  386. -- GB versions
  387. serialise = serialize
  388. unserialise = unserialize
  389. serialiseJSON = serializeJSON
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement