46

I'd like to create a progress bar to indicate the status of an a batch job in Ruby.

I've read some tutorials / libraries on using (n)curses, none of which were particularly helpful in explaining how to create an "animated" progress bar in the terminal or using curses with Ruby.

I'm already aware of using a separate thread to monitor the progress of a given job, I'm just not sure how to proceed with drawing a progress bar.


Update

ProgressBar class was incredibly straight-forward, perfectly solved my problem.

Lahiru Jayaratne
  • 1,684
  • 4
  • 31
  • 35
Jake McGraw
  • 55,558
  • 10
  • 50
  • 63

4 Answers4

20

You might be able to get some implementation ideas from the Ruby/ProgressBar library, which generates text progress bars. I stumbled across it a couple of months back but haven't made any use of it.

Joey deVilla
  • 8,403
  • 2
  • 29
  • 15
  • Take note: Current as of 2012, implementations of ***ncurses*** in the ruby language use the [ffi-ncurses](https://github.com/seanohalpin/ffi-ncurses) gem. ***Curses*** is included in the STDLIB of ruby. – shadowbq Jan 04 '13 at 19:41
  • 1
    Update Note: As of Ruby 2.1.0 (some time in 2014) - curses in the STDLIB has been removed and [is now a gem](https://github.com/ruby/curses) – mraaroncruz May 05 '15 at 09:06
4

Personally I think curses is overkill in this case. While the curses lib is nice (and I frequently use it myself) it's a PITA to relearn every time I haven't needed it for 12 months which has to be the sign of a bad interface design.

If for some reason you can't get on with the progress bar lib Joey suggested roll your own and release it under a pretty free licence for instant kudos :)

Community
  • 1
  • 1
sparkes
  • 19,343
  • 5
  • 39
  • 46
  • @MeNoMore "PITA" is not code. It is an acronym for "pain in the arse". Do not use code formatting for such things. – Andrew Barber Feb 07 '13 at 17:30
  • @Andrew Barber :) you got me laughing now i rreally didnt know that with PITA as "pain in the arse", just learned something, where i come from PITA is something totally different (Hint: even there it isnt code :), you can google it), thank you anyway – CloudyMarble Feb 08 '13 at 06:42
4

Very late answer and sorry for self promotion, but I created library to show progress in terminal.

tig
  • 25,841
  • 10
  • 64
  • 96
2

On windows, curses works out of the box, ncurses doesn't, and for a progress bar curses should be sufficient. So, use curses instead of ncurses.

Also, both curses and ncurses are wafer-thin wrappers around the c library - that means you don't really need Ruby-specific tutorials.

However, on the site for the PickAxe you can download all the code examples for the book. The file "ex1423.rb" contains a curses demo which plays Pong - that should give you plenty of material to get you going.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134