Module: Liri::Common::TtyProgressbar

Defined in:
lib/common/tty_progressbar.rb

Overview

Este módulo se encarga de mostrar una barra de progreso

Defined Under Namespace

Classes: TimeFormatter

Constant Summary collapse

ANIMATION =
[
  "■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□",
  "□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■",
  "□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□",
  "□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□□■□□",
]
ANIMATION2 =
[
    "■□□□■□□□■□□□■□□□■□□",
    "□■□□□■□□□■□□□■□□□■□",
    "□□■□□□■□□□■□□□■□□□■",
    "□□□■□□□■□□□■□□□■□□□",
]

Class Method Summary collapse

Class Method Details

.start(format, params = {}) ⇒ Object

Example:

Common::TtyProgressbar.start("Compressing source code |:bar| Time: :elapsed", total: nil, width: 80) do
  ...code
end


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/common/tty_progressbar.rb', line 30

def start(format, params = {})
  params[:unknown] = ANIMATION[0]
  progressbar = TTY::ProgressBar.new(format, params)
  # Es importante iniciar la barra porque TimeFormatter.call accede a su start_time y si no se inició la barra
  # entonces ocurre un error
  progressbar.start
  progressbar.use(Common::TtyProgressbar::TimeFormatter)

  Thread.new do
    animation_count = 0
    while !progressbar.stopped?
      progressbar.advance

      progressbar.update(unknown: ANIMATION[animation_count])
      animation_count += 1
      animation_count = 0 if animation_count == 3

      sleep(0.1)
    end
  end
  yield
  progressbar.update(total: 1) # Esto hace que la barra cambie a al estilo completado con un porcentaje del 100%
  progressbar.stop
rescue TypeError
  # Se captura la excepción solo para evitar un error en start_time mas abajo
end