Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local util = require("gab.util")
- local gfx = require("gab.gfx")
- -- because `table` is already an API name
- local Grid = {}
- function Grid.new(t, x, y, h)
- local grid = {
- t = t,
- x = x,
- y = y,
- h = h,
- columns = {},
- rows = {}
- }
- function grid:addColumn(width, header, alignRight, headerColour)
- table.insert(self.columns, {
- width = width,
- header = header,
- alignRight = alignRight,
- headerColour = headerColour
- })
- end
- function grid:addRow(values)
- table.insert(self.rows, values)
- end
- function grid:draw()
- t.setBackgroundColour(colours.black)
- t.setTextColour(colours.grey)
- local columnCount = #self.columns
- local x = self.x
- local columnLines = ""
- local bottomLine = ""
- -- draw the headers + get necessary column info
- for i, column in pairs(self.columns) do
- local width = column.width
- local header = column.header
- -- top header line, \156 -- intersections, \140 -- everything else
- t.setCursorPos(x, self.y)
- t.write("\156" .. ("\140"):rep(width))
- -- bottom header line, \157 -- intersections, \140 -- everything else
- t.setCursorPos(x, self.y + 2)
- t.write("\157" .. ("\140"):rep(width))
- -- header text
- local headerColourBlit = gfx.colourToBlit(column.headerColour or colours.lightGrey)
- t.setCursorPos(x, self.y + 1)
- t.blit("\149 " .. header, "7f" .. (headerColourBlit):rep(#header), ("f"):rep(#header + 2))
- columnLines = columnLines .. "\149" .. (" "):rep(width)
- bottomLine = bottomLine .. "\141" .. ("\140"):rep(width)
- x = x + column.width + 1
- end
- -- draw the main column column lines (including rightmost line)
- for y = 1, self.h do
- if y > 3 then -- draw all the column lines
- t.setCursorPos(self.x, self.y - 1 + y)
- t.blit(columnLines .. "\149", ("7"):rep(#columnLines + 1), ("f"):rep(#columnLines + 1))
- else -- draw only the right one for the header
- t.setCursorPos(x, self.y - 1 + y)
- t.blit(y == 1 and "\148" or "\149", "7", "f")
- end
- end
- -- draw the bottom line
- t.setCursorPos(self.x, self.y + self.h)
- t.blit(bottomLine .. "\133", ("7"):rep(#columnLines + 1), ("f"):rep(#columnLines + 1))
- -- draw the rows
- for y, row in pairs(self.rows) do
- local x = self.x
- for i, column in pairs(self.columns) do
- local value = row[i]
- local text = value.text
- local na = false
- if type(text) == "number" then
- text = util.formatCommas(text)
- end
- if text == "0" then
- na = true
- text = "N/A"
- end
- if column.alignRight then
- local format = string.format("%%%ds", column.width - 2) -- yay for nested string.format...
- text = string.format(format, text)
- end
- local colour = gfx.colourToBlit(na and colours.lightGrey or (value.colour or colours.white))
- t.setCursorPos(x + 2, self.y + y + 2)
- t.blit(text, colour:rep(#text), ("f"):rep(#text))
- x = x + column.width + 1
- end
- end
- end
- return grid
- end
- return Grid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement