Class: Nitrous::ProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/nitrous/progress_bar.rb

Constant Summary collapse

RED =
101
GREEN =
102

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps) ⇒ ProgressBar

Returns a new instance of ProgressBar.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nitrous/progress_bar.rb', line 44

def initialize(steps)
  @total_steps = steps
  @step = 0
  @color = GREEN
  Curses.init_screen
  @dimensions = [Curses.stdscr.maxx, Curses.stdscr.maxy]
  Curses.close_screen
  $stdout.puts ""
  $stdout = ProgressBarAwareStandardOut.new($stdout, self)
  @text = ""
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



42
43
44
# File 'lib/nitrous/progress_bar.rb', line 42

def color
  @color
end

#textObject

Returns the value of attribute text.



42
43
44
# File 'lib/nitrous/progress_bar.rb', line 42

def text
  @text
end

Instance Method Details

#delete_barObject



68
69
70
# File 'lib/nitrous/progress_bar.rb', line 68

def delete_bar
  $stdout.direct_write("\e[1F\e[0K")
end

#drawObject



72
73
74
75
# File 'lib/nitrous/progress_bar.rb', line 72

def draw
  delete_bar
  redraw_bar
end

#draw_bar(color, width) ⇒ Object



61
62
63
64
65
66
# File 'lib/nitrous/progress_bar.rb', line 61

def draw_bar(color, width)
  content = @text[0..[width, @text.length].min] + " " * [width - @text.length, 0].max
  $stdout.direct_write("\e[#{color}m#{content}\e[0m")
  $stdout.direct_write(@text[(width + 1)..-1]) if @text.length > width
  $stdout.direct_write("\n")
end

#redraw_barObject



77
78
79
80
# File 'lib/nitrous/progress_bar.rb', line 77

def redraw_bar
  draw_bar(@color, (@dimensions[0].to_f/@total_steps)*@step)
  STDOUT.flush
end

#stepObject



56
57
58
59
# File 'lib/nitrous/progress_bar.rb', line 56

def step
  @step += 1
  draw
end