Class: Fatty::Terminal::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/fatty/terminal/progress.rb

Constant Summary collapse

PARTIAL_BLOCKS =
["", "", "", "", "", "", "", ""].freeze
FULL_BLOCK =
""
EMPTY_BAR =
"."
SHADE_EMPTY =
""
SHADE_HALF =
""
SHADE_FULL =
""
BRAILLE_STEPS =
["", "", "", "", "", "", "", ""].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(terminal:, label:, total: nil, style: :percent, role: :info, width: 40) ⇒ Progress

The width parameter's purpose varies by style:

:trail width = max visible width of the trail portion :bar width = max bar width :unicode_bar width = max bar width :braille_bar width = max bar width :spinner width ignored, unless later used for suffix/trail :count width ignored :percent width ignored



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fatty/terminal/progress.rb', line 27

def initialize(terminal:, label:, total: nil, style: :percent, role: :info, width: 40)
  @terminal = terminal
  @label = label.to_s
  @total = total&.to_i
  @style = style.to_sym
  @role = role
  @width = width.to_i
  @trail = []
  @current = 0
  @spinner_index = 0

  validate_total_requirement!

  refresh
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



16
17
18
# File 'lib/fatty/terminal/progress.rb', line 16

def label
  @label
end

#roleObject (readonly)

Returns the value of attribute role.



16
17
18
# File 'lib/fatty/terminal/progress.rb', line 16

def role
  @role
end

#styleObject (readonly)

Returns the value of attribute style.



16
17
18
# File 'lib/fatty/terminal/progress.rb', line 16

def style
  @style
end

#terminalObject (readonly)

Returns the value of attribute terminal.



16
17
18
# File 'lib/fatty/terminal/progress.rb', line 16

def terminal
  @terminal
end

#totalObject (readonly)

Returns the value of attribute total.



16
17
18
# File 'lib/fatty/terminal/progress.rb', line 16

def total
  @total
end

#widthObject (readonly)

Returns the value of attribute width.



16
17
18
# File 'lib/fatty/terminal/progress.rb', line 16

def width
  @width
end

Instance Method Details

#clearObject



76
77
78
79
# File 'lib/fatty/terminal/progress.rb', line 76

def clear
  terminal.clear_status
  self
end

#finish(message = nil, clear: false, role: @role, render: false, transient: true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fatty/terminal/progress.rb', line 59

def finish(message = nil, clear: false, role: @role, render: false, transient: true)
  if clear
    terminal.clear_status
  else
    text =
      if message && !message.empty?
        render_text(suffix: message)
      else
        render_text
      end
    terminal.set_status(text, role: role, transient: transient)
  end

  terminal.render_now if render
  self
end

#update(current: nil, total: @total, label: @label, indicator: nil, render: false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fatty/terminal/progress.rb', line 43

def update(current: nil, total: @total, label: @label, indicator: nil, render: false)
  @current = current.to_i unless current.nil?
  @total = total&.to_i
  @label = label.to_s

  if style == :spinner
    advance_spinner
  else
    append_indicator(indicator)
  end

  refresh
  terminal.render_now if render
  self
end