Just a Progress Bar

Started by Missooni, Sep 03, 2024, 06:22 PM

Previous topic - Next topic

Missooni


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
Code: Lua
  1. --If using the file from a folder, use '.' to replace slashes.
  2. progressBar = require("progressBar")
  3.  
  4. actionTable = {}
  5. actionTable[1] = function()
  6. -- Function commands go here...
  7. end
  8. actionTable[2] = function()
  9. -- and here!
  10. end
  11. -- Add entries to this table as necessary.
  12.  
  13. -- If 'true' is passed here, output from functions will be hidden.
  14. progressBar(actionTable, true)
Source Code
Code: Lua
  1. function gen(pTable, hidden)
  2.         local termWidth, termHeight = term.getSize()
  3.         local ogTerm = term.current()
  4.         local bgWindow = window.create(ogTerm, 1, 1, termWidth, termHeight, false)
  5.  
  6.         -- All customizable values.
  7.         local barWidth = math.floor(termWidth*.75)
  8.         local fullChar = " "
  9.         local emptyChar = "\143"
  10.         local bg = "f"
  11.         local fg = "0"
  12.  
  13.         -- Draws the empty progress bar.
  14.         local mouseX, mouseY = term.getCursorPos()
  15.         term.setCursorPos(termWidth/2-barWidth/2, mouseY)
  16.         term.blit(emptyChar:rep(barWidth), bg:rep(barWidth), fg:rep(barWidth))
  17.  
  18.         -- Iterates through the table and performs functions.
  19.         for key, value in pairs(pTable) do
  20.                 if hidden then term.redirect(bgWindow) end
  21.                 value()
  22.                 term.redirect(ogTerm)
  23.         -- Draws 'full characters' to the progress bar as the action finishes.
  24.                 term.setCursorPos(termWidth/2-barWidth/2, mouseY)
  25.                 term.blit(fullChar:rep(math.floor(key / #pTable * barWidth)), bg:rep(math.floor(key / #pTable * barWidth)), fg:rep(math.floor(key / #pTable * barWidth)))
  26.         end
  27.         term.setCursorPos(mouseX, mouseY+1)
  28. end
  29.  
  30. return gen
" We are like the spider. We weave our life and then move along in it.
We are like the dreamer, who dreams and then lives in the dream. "
- Aitareya Upanishad