(https://camo.tmpim.com/ace9fc5c48dd4bf341560ee284a2e76faf220e34/68747470733a2f2f6769746875622e636f6d2f4d6973736f6f6e692f43432f626c6f622f6d61696e2f70726f67726573734261722f6a617661775f6f77415735744c6c684f2e6769663f7261773d74727565)
Code to create (and automatically center) a progress bar.
Created to be customizable and can be be used in any project.
November 22, 2024 Update:
File can now be used with require(), now runs functions provided through a table.
wget https://raw.githubusercontent.com/Missooni/CC/refs/heads/main/progressBar/progressBar.lua
Usage Example
--If using the file from a folder, use '.' to replace slashes.
progressBar = require("progressBar")
actionTable = {}
actionTable[1] = function()
-- Function commands go here...
end
actionTable[2] = function()
-- and here!
end
-- Add entries to this table as necessary.
-- If 'true' is passed here, output from functions will be hidden.
progressBar(actionTable, true)
Source Code
function gen(pTable, hidden)
local termWidth, termHeight = term.getSize()
local ogTerm = term.current()
local bgWindow = window.create(ogTerm, 1, 1, termWidth, termHeight, false)
-- All customizable values.
local barWidth = math.floor(termWidth*.75)
local fullChar = " "
local emptyChar = "\143"
local bg = "f"
local fg = "0"
-- Draws the empty progress bar.
local mouseX, mouseY = term.getCursorPos()
term.setCursorPos(termWidth/2-barWidth/2, mouseY)
term.blit(emptyChar:rep(barWidth), bg:rep(barWidth), fg:rep(barWidth))
-- Iterates through the table and performs functions.
for key, value in pairs(pTable) do
if hidden then term.redirect(bgWindow) end
value()
term.redirect(ogTerm)
-- Draws 'full characters' to the progress bar as the action finishes.
term.setCursorPos(termWidth/2-barWidth/2, mouseY)
term.blit(fullChar:rep(math.floor(key / #pTable * barWidth)), bg:rep(math.floor(key / #pTable * barWidth)), fg:rep(math.floor(key / #pTable * barWidth)))
end
term.setCursorPos(mouseX, mouseY+1)
end
return gen