Advertisement
derkoch

helper v1.0

Oct 6th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. local version = 1.0
  2. --- Debugging Mode
  3. -- <code>true</code> enabled, <code>false</code> disabled
  4. -- @class table
  5. -- @name debugging
  6. -- @field debugging enables debugging by setting to <code>true</code>
  7. local debugging = false
  8.  
  9. --- Table with positions<br>
  10. -- <code>"top", "t", "center", "c", "bottom", "b", "left", "l", "right", "r"</code>
  11. -- @class table
  12. -- @name validPositions
  13. -- @field validPositions Table with valid positions, needed for <code>isValidPosition()</code>
  14. local validPositions = {"top", "t", "center", "c", "bottom", "b", "left", "l", "right", "r"}
  15.  
  16.  
  17. --- Prints a debug message
  18. -- Only print of @field debugging is true
  19. -- @param source The source of the debug messsage
  20. -- @param msg The actual message
  21. function debug_print(source, msg)
  22.     if debugging then
  23.         print("DEBUG: '" .. source .."': " .. msg) 
  24.     end
  25. end
  26.  
  27. --- Prints errors in red color and sets text color to white again
  28. -- @param msg   The error message
  29. function printError(msg)
  30.     term.setTextColor(colors.red)
  31.     print(msg)
  32.     term.setTextColor(colors.white)
  33. end
  34.  
  35. --[[ rPrint(struct, [limit], [indent]) Recursively print arbitrary data.
  36. Set limit (default 100) to stanch infinite loops.
  37. Indents tables as [KEY] VALUE, nested tables as [KEY] [KEY]...[KEY] VALUE
  38. Set indent ("") to prefix each line: Mytable [KEY] [KEY]...[KEY] VALUE
  39. --]]
  40. function rPrint(s, l, i) -- recursive Print (structure, limit, indent)
  41.     l = (l) or 100; i = i or "";    -- default item limit, indent string
  42.     if (l<1) then print "ERROR: Item limit reached."; return l-1 end;
  43.     local ts = type(s);
  44.     if (ts ~= "table") then print (i,ts,s); return l-1 end
  45.     print (i,ts); -- print "table"
  46.     for k,v in pairs(s) do -- print "[KEY] VALUE"
  47.         l = rPrint(v, l, i.."\t["..tostring(k).."]");
  48.         if (l < 0) then break end
  49.     end
  50.     return l
  51. end
  52.  
  53. --- Checks if an element is in a table.  
  54. -- (<a href=http://stackoverflow.com/questions/2282444/how-to-check-if-a-table-contains-an-element-in-lua>Source</a>)
  55. -- @param table the table that should be searched in
  56. -- @param element the element that should be tested
  57. function table.contains(table, element)
  58.   for _, value in pairs(table) do
  59.     if value == element then
  60.       return true
  61.     end
  62.   end
  63.   return false
  64. end
  65.  
  66. --- Formats <code>text</code> to fit into <code>width</code>, filling missing characters with
  67. -- <code>fillchar</code> and also applies <code>orientation</code>
  68. -- @param text  The text
  69. -- @param width The length of the returned string
  70. -- @param fillchar  Char to fill the "spaces"
  71. -- @param orientation   Orientation of the text (<code>"center", "c", "left", "l", "right", "r"</code>)
  72. function formatText(text, width, fillchar, orientation)
  73.     local validOrientations = {"left", "l", "right", "r", "center", "c" }
  74.     text = tostring(text)
  75.     if #text >= width then
  76.         return string.sub(text, 1, width), string.sub(text, width + 1) 
  77.     end
  78.    
  79.     if table.contains(validOrientations, orientation) then
  80.         local value = ""
  81.         local diff = width - #text     
  82.         if orientation == "left" or orientation == "l" then
  83.             value = text .. string.rep(fillchar, diff)
  84.             return value
  85.         elseif orientation == "right" or orientation == "r" then
  86.             value = string.rep(fillchar, diff) .. text
  87.             return value
  88.         elseif orientation == "center" or orientation == "c" then
  89.             local leftside = math.floor(diff/2)
  90.             local rightside = math.ceil(diff/2)
  91.             value = string.rep(fillchar, leftside) .. text .. string.rep(fillchar, rightside)          
  92.             return value
  93.         end
  94.     end
  95.     return text
  96. end
  97.  
  98. --- Validaes the position
  99. -- @param position  string: the position
  100. -- @return true - valid, false - not valid
  101. function isValidPosition(position)
  102.     for _, value in pairs(validPositions) do
  103.         if value == position then
  104.             return true
  105.         end
  106.     end
  107.     return false
  108. end
  109.  
  110. --- Creates a table with a range numbers
  111. -- @param startval  Star value
  112. -- @param endval    End value
  113. -- @param steps     Stepping
  114. -- @return Table with range of number values
  115. -- @usage createNumberList(0, 10, 2) -> {0,2,4,6,8,10}
  116. function createNumberList(startval, endval, steps)
  117.     local list = {}
  118.     local index = 1
  119.     for i = startval, endval, steps do
  120.         list[index] = i
  121.         index = index + 1
  122.     end
  123.     return list
  124. end
  125.  
  126. --- Returns the length of the longest element in a table
  127. -- Turn the elements to strings and compares the lengths
  128. -- @param list  The list
  129. -- @return Length of longest element
  130. function longestElement(list)
  131.     local length = 0
  132.     for _, value in pairs(list) do
  133.         if #tostring(value) > length then
  134.             length = #tostring(value)
  135.         end
  136.     end
  137.     return length
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement