Just a Progress Bar

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

Previous topic - Next topic

Missooni


Program to create (and automatically center) a progress bar.
Created to be customizable and can be be used in any project.
pastebin get 3FKLcz1C progressBar
Source Code
Code: Lua
  1. -- Define or alias values using a table. I'm using strings for this example.
  2. local barTable = {"val1","val2","val3","val4","val5"}
  3.  
  4. local termWidth, termHeight = term.getSize()
  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 an action.
  19. for key, value in pairs(barTable) do
  20.     os.sleep(1)
  21.  
  22. -- Draws 'full characters' to the progress bar as the action finishes.
  23.     term.setCursorPos(termWidth/2-barWidth/2, mouseY)
  24.     term.blit(fullChar:rep(math.floor(i / #table * barWidth)), bg:rep(math.floor(i / #table * barWidth)), fg:rep(math.floor(i / #table * barWidth)))
  25. end
  26.  
  27. print(" Done!")
  28. -- For this example, the program will end once any key is pressed.
  29. os.pullEvent( "key" )
" 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