Class: Bosh::Cli::TaskTracking::StageProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/task_tracking/stage_progress_bar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ StageProgressBar

Returns a new instance of StageProgressBar.



11
12
13
14
15
16
17
18
19
20
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 11

def initialize(output)
  @output = output
  @current = 0
  @total = 100
  @bar_visible = true
  @finished_steps = 0
  @filler = 'o'
  @terminal_width = calculate_terminal_width
  @bar_width = (0.24 * @terminal_width).to_i # characters
end

Instance Attribute Details

#bar_visibleObject

Returns the value of attribute bar_visible.



7
8
9
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 7

def bar_visible
  @bar_visible
end

#currentObject

Returns the value of attribute current.



5
6
7
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 5

def current
  @current
end

#finished_stepsObject

Returns the value of attribute finished_steps.



8
9
10
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 8

def finished_steps
  @finished_steps
end

#labelObject

Returns the value of attribute label.



6
7
8
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 6

def label
  @label
end

#terminal_widthObject

Returns the value of attribute terminal_width.



9
10
11
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 9

def terminal_width
  @terminal_width
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 4

def title
  @title
end

#totalObject

Returns the value of attribute total.



3
4
5
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 3

def total
  @total
end

Instance Method Details

#barObject



31
32
33
34
35
36
37
38
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 31

def bar
  n_fillers = @total == 0 ? 0 : [(@bar_width *
    (@current.to_f / @total.to_f)).floor, 0].max

  fillers = "#{@filler}" * n_fillers
  spaces = ' ' * [(@bar_width - n_fillers), 0].max
  "|#{fillers}#{spaces}|"
end

#calculate_terminal_widthObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 46

def calculate_terminal_width
  if ENV['COLUMNS'].to_s =~ /^\d+$/
    ENV['COLUMNS'].to_i
  elsif !ENV['TERM'].blank?
    width = `tput cols`
    $?.exitstatus == 0 ? [width.to_i, 100].min : 80
  else
    80
  end
rescue
  80
end

#clear_lineObject



40
41
42
43
44
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 40

def clear_line
  @output.print("\r")
  @output.print(' ' * @terminal_width)
  @output.print("\r")
end

#refreshObject



22
23
24
25
26
27
28
29
# File 'lib/cli/task_tracking/stage_progress_bar.rb', line 22

def refresh
  clear_line
  bar_repr = @bar_visible ? bar : ''
  title_width = (0.35 * @terminal_width).to_i
  title = @title.truncate(title_width).ljust(title_width)
  @output.print "#{title} #{bar_repr} #{@finished_steps}/#{@total}"
  @output.print " #{@label}" if @label
end