Advertisement
infiniteblock

grid

Apr 23rd, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. local util = require("gab.util")
  2. local gfx = require("gab.gfx")
  3.  
  4. -- because `table` is already an API name
  5. local Grid = {}
  6.  
  7. function Grid.new(t, x, y, h)
  8. local grid = {
  9. t = t,
  10. x = x,
  11. y = y,
  12. h = h,
  13.  
  14. columns = {},
  15. rows = {}
  16. }
  17.  
  18. function grid:addColumn(width, header, alignRight, headerColour)
  19. table.insert(self.columns, {
  20. width = width,
  21. header = header,
  22. alignRight = alignRight,
  23. headerColour = headerColour
  24. })
  25. end
  26.  
  27. function grid:addRow(values)
  28. table.insert(self.rows, values)
  29. end
  30.  
  31. function grid:draw()
  32. t.setBackgroundColour(colours.black)
  33. t.setTextColour(colours.grey)
  34.  
  35. local columnCount = #self.columns
  36. local x = self.x
  37. local columnLines = ""
  38. local bottomLine = ""
  39.  
  40. -- draw the headers + get necessary column info
  41. for i, column in pairs(self.columns) do
  42. local width = column.width
  43. local header = column.header
  44.  
  45. -- top header line, \156 -- intersections, \140 -- everything else
  46. t.setCursorPos(x, self.y)
  47. t.write("\156" .. ("\140"):rep(width))
  48.  
  49. -- bottom header line, \157 -- intersections, \140 -- everything else
  50. t.setCursorPos(x, self.y + 2)
  51. t.write("\157" .. ("\140"):rep(width))
  52.  
  53. -- header text
  54. local headerColourBlit = gfx.colourToBlit(column.headerColour or colours.lightGrey)
  55. t.setCursorPos(x, self.y + 1)
  56. t.blit("\149 " .. header, "7f" .. (headerColourBlit):rep(#header), ("f"):rep(#header + 2))
  57.  
  58. columnLines = columnLines .. "\149" .. (" "):rep(width)
  59. bottomLine = bottomLine .. "\141" .. ("\140"):rep(width)
  60. x = x + column.width + 1
  61. end
  62.  
  63. -- draw the main column column lines (including rightmost line)
  64. for y = 1, self.h do
  65. if y > 3 then -- draw all the column lines
  66. t.setCursorPos(self.x, self.y - 1 + y)
  67. t.blit(columnLines .. "\149", ("7"):rep(#columnLines + 1), ("f"):rep(#columnLines + 1))
  68. else -- draw only the right one for the header
  69. t.setCursorPos(x, self.y - 1 + y)
  70. t.blit(y == 1 and "\148" or "\149", "7", "f")
  71. end
  72. end
  73.  
  74. -- draw the bottom line
  75. t.setCursorPos(self.x, self.y + self.h)
  76. t.blit(bottomLine .. "\133", ("7"):rep(#columnLines + 1), ("f"):rep(#columnLines + 1))
  77.  
  78. -- draw the rows
  79. for y, row in pairs(self.rows) do
  80. local x = self.x
  81.  
  82. for i, column in pairs(self.columns) do
  83. local value = row[i]
  84. local text = value.text
  85. local na = false
  86.  
  87. if type(text) == "number" then
  88. text = util.formatCommas(text)
  89. end
  90.  
  91. if text == "0" then
  92. na = true
  93. text = "N/A"
  94. end
  95.  
  96. if column.alignRight then
  97. local format = string.format("%%%ds", column.width - 2) -- yay for nested string.format...
  98. text = string.format(format, text)
  99. end
  100.  
  101. local colour = gfx.colourToBlit(na and colours.lightGrey or (value.colour or colours.white))
  102. t.setCursorPos(x + 2, self.y + y + 2)
  103. t.blit(text, colour:rep(#text), ("f"):rep(#text))
  104.  
  105. x = x + column.width + 1
  106. end
  107. end
  108. end
  109.  
  110. return grid
  111. end
  112.  
  113. return Grid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement