ComputerCraft/RSS/graphics.lua
2024-03-10 23:45:44 +03:00

71 lines
No EOL
2.1 KiB
Lua

local GRAPHICS = {
target = nil,
contents = {}
}
local LOG = require('log')
-- Set target's colorscheme to gruvbox for better visuals
function GRAPHICS:SetColours()
if self.target == nil then
return
end
self.target.setPaletteColour(colours.red, 0xfb4934)
self.target.setPaletteColour(colours.blue, 0x83a598)
self.target.setPaletteColour(colours.green, 0xb8bb26)
self.target.setPaletteColour(colours.purple, 0xd3869b)
self.target.setPaletteColour(colours.cyan, 0x8ec07c)
self.target.setPaletteColour(colours.white, 0xf9f5d7)
self.target.setPaletteColour(colours.black, 0x1d2021)
end
-- Set target output for our graphics
function GRAPHICS:SetTarget(target)
self.target = target
if self.target == nil then
LOG:Error("Nil display target, items will not display")
end
target.clear()
target.setTextScale(0.5)
self:Refresh(true)
end
function GRAPHICS:Refresh(full)
if self.target == nil then
return
end
local _, y = self.target.getSize()
while #self.contents > y do
table.remove(self.contents, 1)
end
if full then
self.target.scroll(#self.contents)
for k, entry in ipairs(self.contents) do
self.target.setCursorPos(1, y - k + 1)
local output = "[" .. entry.source .. "] " .. entry.title
local fg_blit = "0" .. (entry.colour):rep(#entry.source) .. ("0"):rep(#entry.title + 2)
self.target.blit(output, fg_blit, ("f"):rep(#output))
end
else
self.target.scroll(1)
self.target.setCursorPos(1, y)
local entry = self.contents[#self.contents]
local output = "[" .. entry.source .. "] " .. entry.title
local fg_blit = "0" .. (entry.colour):rep(#entry.source) .. ("0"):rep(#entry.title + 2)
self.target.blit(output, fg_blit, ("f"):rep(#output))
end
end
function GRAPHICS:AddEntry(entry)
table.insert(self.contents, entry)
self:Refresh()
end
function GRAPHICS:Add(entries)
for _, entry in ipairs(entries) do
self:AddEntry(entry)
end
end
return GRAPHICS