Class: Bosh::Cli::StageProgressBar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ StageProgressBar

Returns a new instance of StageProgressBar.



343
344
345
346
347
348
349
350
351
352
# File 'lib/cli/event_log_renderer.rb', line 343

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.



339
340
341
# File 'lib/cli/event_log_renderer.rb', line 339

def bar_visible
  @bar_visible
end

#currentObject

Returns the value of attribute current.



337
338
339
# File 'lib/cli/event_log_renderer.rb', line 337

def current
  @current
end

#finished_stepsObject

Returns the value of attribute finished_steps.



340
341
342
# File 'lib/cli/event_log_renderer.rb', line 340

def finished_steps
  @finished_steps
end

#labelObject

Returns the value of attribute label.



338
339
340
# File 'lib/cli/event_log_renderer.rb', line 338

def label
  @label
end

#terminal_widthObject

Returns the value of attribute terminal_width.



341
342
343
# File 'lib/cli/event_log_renderer.rb', line 341

def terminal_width
  @terminal_width
end

#titleObject

Returns the value of attribute title.



336
337
338
# File 'lib/cli/event_log_renderer.rb', line 336

def title
  @title
end

#totalObject

Returns the value of attribute total.



335
336
337
# File 'lib/cli/event_log_renderer.rb', line 335

def total
  @total
end

Instance Method Details

#barObject



363
364
365
366
367
368
369
370
# File 'lib/cli/event_log_renderer.rb', line 363

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



378
379
380
381
382
383
384
385
386
387
# File 'lib/cli/event_log_renderer.rb', line 378

def calculate_terminal_width
  if !ENV["TERM"].blank?
    width = `tput cols`
    $?.exitstatus == 0 ? [width.to_i, 100].min : 80
  else
    80
  end
rescue
  80
end

#clear_lineObject



372
373
374
375
376
# File 'lib/cli/event_log_renderer.rb', line 372

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

#refreshObject



354
355
356
357
358
359
360
361
# File 'lib/cli/event_log_renderer.rb', line 354

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