Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- --* Table Display API *--
- --* https://pastebin.com/tn4Uuddp *--
- --* by: GravityCube *--
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- Changelog:
- 1.0.0 First release,
- 1.1.0 Highlights added.
- 1.1.1 A method to know wish row is being touch was added.
- 1.2.0 Sorting added.
- 1.2.1 Added text to know which column is being sorted.
- 1.2.2 Changed ">" to "v" and "<" to "^".
- 1.3.0 UP/DOWN buttons were added. They allow more rows in the same screen.
- 1.4.0 Changed scroll system for pages system.
- 1.4.1 Added red response in case of no next/previous page.
- 1.4.2 Method updateRow was added.
- 1.4.3 Replace "_" -> " "
- 1.5.0 Added custom colors functions for columns
- ]]--
- local TableInstance = {
- draw = function(self)
- local mon = self.mon
- mon.setBackgroundColor(colors.white)
- mon.clear()
- local max_x, max_y = mon.getSize()
- local x_string = ""
- for x=1, max_x do
- x_string = x_string .. " "
- end
- self:updatePositions()
- mon.setBackgroundColor(colors.cyan)
- mon.setCursorPos(1,1)
- mon.write(x_string)
- for i,column in pairs(self.columns) do
- local x_pos = self.positions[i]
- mon.setCursorPos(x_pos, 1)
- local column_txt = string.upper(string.sub(column, 1, 1)) .. string.sub(column, 2)
- column_txt = string.gsub(column_txt, "_", "")
- if self.sort_column == column then
- column_txt = column_txt .. " " .. ((self.sort_direction and "^") or "v")
- end
- mon.write(tostring(column_txt))
- end
- mon.setBackgroundColor(colors.white)
- mon.setTextColor(colors.black)
- for y=2,max_y-1,1 do
- self:updateRow(y)
- end
- self:displayButtons()
- end,
- updateRow = function (self, y)
- --SCROLL
- local mon = self.mon
- local max_x, max_y = mon.getSize()
- local row_data = getRowData(self.data, y, self.scrollInt)
- local x_string = ""
- for x=1, max_x do
- x_string = x_string .. " "
- end
- if row_data ~= nil then
- --COSA PARA NO PERDERSE
- if (y + self.scrollInt) % 2 == 0 then
- mon.setBackgroundColor(0x0100)
- end
- --HighLight
- if self:isHighlight(row_data) then
- mon.setBackgroundColor(colors.orange)
- end
- mon.setCursorPos(1,y)
- mon.write(x_string)
- for i,column in pairs(self.columns) do
- local x_pos = self.positions[i]
- mon.setCursorPos(x_pos, y)
- local txt = tostring(row_data[column])
- if column == "price" then
- txt = "$" .. txt
- end
- local cf = self.custom_colors_func[column]
- if cf then
- cf(mon, txt)
- mon.write(txt)
- mon.setTextColor(colors.black)
- else
- mon.write(txt)
- end
- end
- mon.setBackgroundColor(colors.white)
- end
- end,
- highlight = function (self, column, value, state)
- if not self.highlights[column] then
- self.highlights[column] = {}
- end
- local last_state = self.highlights[column][value]
- local new_state = (state ~= nil and state) or (not last_state)
- self.highlights[column][value] = new_state
- end,
- isHighlight = function (self, row_data)
- for column, value in pairs(row_data) do
- if self.highlights[column] and self.highlights[column][value] then
- return true
- end
- end
- return false
- end,
- clearHighlights = function(self)
- for column, values in pairs(self.highlights) do
- self.highlights[column] = {}
- end
- end,
- getRowOnEvent = function (self, tEvent)
- if tEvent[1] == "monitor_touch" then
- local max_x, max_y = self.mon.getSize()
- local x = tEvent[3]
- local y = tEvent[4]
- self:sortingRow(x,y)
- self:pageButtons(x,y)
- if y > 1 and y < max_y then
- local i = y-1+self.scrollInt
- return self.data[i], y
- end
- if self.playSound then
- self:playSound()
- end
- end
- end,
- pageButtons = function(self, x, y)
- local max_x, max_y = self.mon.getSize()
- if y ~= max_y then return end
- local center_x = getCenter(max_x)
- local new_scroll_int = 0
- local button_state_name = nil
- if x < center_x then
- new_scroll_int = self.scrollInt - (max_y - 2)
- button_state_name = "button1_state"
- else
- new_scroll_int = self.scrollInt + (max_y - 2)
- button_state_name = "button2_state"
- end
- if getRowData(self.data, 2, new_scroll_int) then
- self.scrollInt = new_scroll_int
- self[button_state_name] = true
- else
- self[button_state_name] = colors.red
- end
- self:displayButtons()
- sleep(0.05)
- self[button_state_name] = false
- self:draw()
- end,
- displayButtons = function(self)
- local mon = self.mon
- local max_x, max_y = self.mon.getSize()
- local b1 = self.button1_state
- local b2 = self.button2_state
- local x_string = ""
- for x=1, max_x do
- x_string = x_string .. " "
- end
- local max_x, max_y = self.mon.getSize()
- local center_x = getCenter(max_x)
- local x_b1 = getCenter(center_x, "Previous Page")
- local x_b2 = getCenter(max_x-center_x, "Next Page") + center_x
- mon.setBackgroundColor(colors.cyan)
- if b1 then mon.setBackgroundColor( ( (b1 == true) and colors.orange ) or b1) end
- mon.setCursorPos(1, max_y)
- mon.write(x_string)
- mon.setCursorPos(x_b1, max_y)
- mon.write("Previous Page")
- mon.setBackgroundColor(colors.cyan)
- if b2 then mon.setBackgroundColor( ( (b2 == true) and colors.orange ) or b2) end
- mon.setCursorPos(center_x, max_y)
- mon.write(x_string)
- mon.setCursorPos(x_b2, max_y)
- mon.write("Next Page")
- end,
- sortingRow = function (self, x, y)
- if y ~= 1 then return end
- --** Sorting
- local positions = self.positions
- for i,column in pairs(self.columns) do
- local x_pos = positions[i]
- if x >= x_pos and x <= (string.len(column) + x_pos) then
- self:sort(column)
- return true
- end
- end
- return false
- end,
- sort = function(self, column)
- if self.sort_column ~= column then
- self.sort_direction = false
- end
- if self.sort_direction then
- table.sort(self.data, function(a,b) return (a[column] > b[column]) end)
- else
- table.sort(self.data, function(a,b) return (a[column] < b[column]) end)
- end
- self.sort_column = column
- self.sort_direction = (not self.sort_direction)
- self:draw()
- end,
- getLengths = function(self)
- local max_x, max_y = self.mon.getSize()
- local max_x_table = (max_x - #self.columns) - 1
- local lengths = {}
- lengths[0] = 0
- for i,column in pairs(self.columns) do
- lengths[i] = getMaxLength(self.data, column)
- end
- local a = 1
- while totalLength(lengths) < max_x_table do
- if a > #self.columns then
- a = 1
- end
- lengths[a] = lengths[a] + 1
- a = a + 1
- end
- return lengths
- end,
- updatePositions = function (self)
- local lengths = self:getLengths()
- local positions = {}
- local x_pos = 1
- for i,column in pairs(self.columns) do
- x_pos = x_pos + lengths[(i-1)]+1
- positions[i] = x_pos
- end
- self.positions = positions
- end,
- }
- --------------------------------------
- --> Utils <--
- --------------------------------------
- function getMaxLength(eTable, column)
- local length = 1
- for k,v in pairs(eTable) do
- if length < string.len(v[column]) then
- length = string.len(v[column])
- end
- end
- if length < string.len(column) then
- length = string.len(column)
- end
- return length
- end
- function getRowData(eTable, y, scrollInt)
- local i = y-1+scrollInt
- return eTable[i]
- end
- function totalLength(lengths)
- tLength = 0
- for _,le in pairs(lengths) do
- tLength = tLength + le
- end
- return tLength
- end
- function getCenter(f, varP)
- if not varP then
- varP = 0
- end
- local lengthOfVar = 0
- if type(varP) == "number" then
- lengthOfVar = varP
- elseif type(varP) == "string" then
- lengthOfVar = string.len(varP)
- else
- error("For function gcapi.getCenter expected number or string in argument #2")
- end
- if (f - string.len(varP) % 2) == 0 then
- return math.floor((f - lengthOfVar)/2)
- end
- return math.floor((f - lengthOfVar)/2)+1
- end
- --------------------------------------
- --> Public functions <--
- --------------------------------------
- function new(monitor, data_table, columns_ins, x, y, width, heigth)
- local calculated_columns = {}
- if not columns_ins then
- for k,v in pairs(data_table[1]) do
- table.insert(calculated_columns, k)
- end
- else
- calculated_columns = columns_ins
- end
- local instance = {
- highlights = {},
- mon = monitor,
- columns = calculated_columns,
- data = data_table,
- scrollInt = 0,
- custom_colors_func = {}
- }
- setmetatable(instance, {__index = TableInstance})
- instance:sort(instance.columns[1])
- return instance
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement