Skip to content

Instantly share code, notes, and snippets.

@Thomascountz
Created January 2, 2025 19:30
Show Gist options
  • Save Thomascountz/e69872ef5b79fe43def0fbea19e9d492 to your computer and use it in GitHub Desktop.
Save Thomascountz/e69872ef5b79fe43def0fbea19e9d492 to your computer and use it in GitHub Desktop.
Progress bar in Ruby
def print_progress(title, total, current_progress, bar_width: 50)
progress_pct = (current_progress.to_f / total) * bar_width
printf("\r#{title}: [%-#{bar_width}s ] -- %s", "▤" * progress_pct.round, "#{current_progress}/#{total} ")
end
# Usage
1.upto(10) do |i|
print_progress("Here we go!", 10, i)
sleep 0.2
end;print("\n")
# Output Example:
# Here we go!: [▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤▤ ] -- 7/10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment