Skip to content

Instantly share code, notes, and snippets.

@SammyForReal
Created October 6, 2023 21:48
Show Gist options
  • Save SammyForReal/19ace8b669e3e45397c2704327e52d8c to your computer and use it in GitHub Desktop.
Save SammyForReal/19ace8b669e3e45397c2704327e52d8c to your computer and use it in GitHub Desktop.
Display vertical progressbars dynamically.
-- Gather peripherals
local mon = peripheral.find("monitor")
-- Create is used as an example here.
local vault = peripheral.find("create:item_vault")
local speedometer = peripheral.find("Create_Speedometer")
local stressometer = peripheral.find("Create_Stressometer")
-- Init
mon.setTextScale(0.5)
local refresh_rate = 5 -- In seconds
local unit = "su"
local display = {
function()
return stressometer.getStress(), stressometer.getStressCapacity(), "Stressometer", unit
end,
function()
return speedometer.getSpeed(), 256, "Speedometer", unit
end,
function()
local name = "minecraft:iron_ingot"
local items = 0
for slot, item in pairs(vault.list()) do
if item.name == name then
items = items+item.count
end
end
return items, vault.size()*64, "Vault [Iron Ingot"..(items > 1 and "s" or "").."]", ""
end,
}
local colorscheme = {
primary = colors.green,
warning = colors.yellow,
critical = colors.red,
foreground = colors.white,
background = colors.black,
}
---------------------------------------------------
local function horizontalLine(x, y, length, char)
local whitespace = (char or ' '):rep(length)
mon.setCursorPos(x, y)
mon.write(whitespace)
end
local function verticalLine(x, y, length, char)
for i=1, length do
mon.setCursorPos(x, y+i-1)
mon.write(char or " ")
end
end
local function setColor(fg, bg)
mon.setTextColor(fg)
mon.setBackgroundColor(bg)
end
local function label(x, y, text)
mon.setCursorPos(x, y)
mon.write(text or "")
end
local function clear()
setColor(colorscheme.foreground, colorscheme.background)
mon.clear()
end
local function progressBar(title, x, y, width, height, cur, max, cur_unit, pattern)
if not pattern then pattern = "%s%s" end
if not title then title = "Unknown" end
local accent = colorscheme.primary
local percentage = 100/max*cur
if percentage >= 80 then
accent = colorscheme.critical
elseif percentage >= 60 then
accent = colorscheme.warning
end
setColor(accent, colorscheme.background)
-- Progressbar
local cur_height = math.floor((height-4)/max*cur)
if cur_height > height-4 then
cur_height = height-4
end
for i=1, cur_height do
horizontalLine(x+1, y-i-1, width-2, "\140")
end
-- Labels
setColor(colorscheme.foreground, colorscheme.background)
label(x-1+(width/2 - (#title)/2), y-height+1, title) -- Title
local data = (("%s / %s"):format(pattern, pattern)):format(cur, cur_unit, max, cur_unit)
label(x-1+(width/2 - #data/2), y, data) -- Data
-- Decoration
setColor(accent, colorscheme.background)
horizontalLine(x+1, y-1, width-2, "\140")
label(x, y-1, "\141")
label(x+width-1, y-1, "\142")
horizontalLine(x+1, y-height+2, width-2, "\140")
label(x, y-height+2, "\156")
setColor(colorscheme.background, accent)
label(x+width-1, y-height+2, "\147")
setColor(accent, colorscheme.background)
verticalLine(x, y-height+3, height-4, "\149")
setColor(colorscheme.background, accent)
verticalLine(x+width-1, y-height+3, height-4, "\149")
setColor(accent, colorscheme.background)
verticalLine(x+1, y-cur_height-1, cur_height, "\136")
verticalLine(x-2+width, y-cur_height-1, cur_height, "\132")
verticalLine(x-2+width/2, y-cur_height-1, cur_height, " ")
end
---------------------------------------------------
while true do
local width, height = mon.getSize()
local module_width = math.floor((width-3-#display) / #display)
clear()
for index, func in ipairs(display) do
local cur, max, title, cur_unit = func()
progressBar(title, math.floor(2+((module_width+2)*(index-1))), height-1, module_width, height-2, cur, max, cur_unit)
end
sleep(refresh_rate)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment