Created
October 24, 2015 10:36
-
-
Save SafeAF/ac6ddcd16ee1d5079f4f to your computer and use it in GitHub Desktop.
ruby curses example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
initialize ten workers. Each worker is responsible for keeping track of the work she has completed, @percent, and then reporting that progress on her own line, @index, of the console. | |
#!/usr/bin/env ruby | |
require 'curses' | |
Curses.noecho | |
Curses.init_screen | |
class Worker | |
def initialize(index) | |
@index = index | |
@percent = 0 | |
end | |
def run | |
(1..10).each do | |
work | |
report | |
sleep(rand()) | |
end | |
end | |
def to_s | |
"Worker ##{'%2d' % @index} is #{'%3d' % @percent}% complete" | |
end | |
private | |
def work | |
@percent += 10 | |
end | |
def report | |
Curses.setpos(@index, 0) | |
Curses.addstr(to_s) | |
Curses.refresh | |
end | |
end | |
workers = (1..10).map{ |index| Worker.new(index) } | |
at_exit do | |
workers.each{ |worker| puts worker } | |
end | |
workers.map{ |worker| Thread.new{ worker.run } }.each(&:join) | |
Curses.close_screen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment