Class: Fatty::Progress

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

Constant Summary collapse

PARTIAL_BLOCKS =
["", "", "", "", "", "", "", ""].freeze
FULL_BLOCK =
""
EMPTY_BAR =
"."
SHADE_EMPTY =
""
SHADE_HALF =
""
SHADE_FULL =
""
BRAILLE_STEPS =
["", "", "", "", "", "", "", ""].freeze
STYLES =
%i[
count
percent
count_percent
trail
bar
unicode_bar
braille_bar
spinner
].freeze
DEFAULT_LABEL =
"Progress"
DEFAULT_ROLE =
:info
DEFAULT_STYLE =
:percent
DEFAULT_WIDTH =
40

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(terminal:, label: DEFAULT_LABEL, total: nil, style: DEFAULT_STYLE, role: DEFAULT_ROLE, width: DEFAULT_WIDTH) ⇒ Progress

The width parameter's purpose varies by style:

| Style | Width Purpose | |----------------+---------------------------------------------------| | :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 | | :count_percent | width ignored |



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fatty/progress.rb', line 45

def initialize(
  terminal:,
  label: DEFAULT_LABEL,
  total: nil,
  style: DEFAULT_STYLE,
  role: DEFAULT_ROLE,
  width: DEFAULT_WIDTH
)
  @terminal = terminal
  @label = normalize_label(label)
  @total = normalize_total(total)
  @style = normalize_style(style)
  @role = normalize_role(role)
  @width = normalize_width(width)
  @trail = []
  @current = 0
  @spinner_index = 0
  validate_total_requirement!
  refresh
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



31
32
33
# File 'lib/fatty/progress.rb', line 31

def current
  @current
end

#labelObject (readonly)

Returns the value of attribute label.



31
32
33
# File 'lib/fatty/progress.rb', line 31

def label
  @label
end

#roleObject (readonly)

Returns the value of attribute role.



31
32
33
# File 'lib/fatty/progress.rb', line 31

def role
  @role
end

#styleObject (readonly)

Returns the value of attribute style.



31
32
33
# File 'lib/fatty/progress.rb', line 31

def style
  @style
end

#terminalObject (readonly)

Returns the value of attribute terminal.



31
32
33
# File 'lib/fatty/progress.rb', line 31

def terminal
  @terminal
end

#totalObject (readonly)

Returns the value of attribute total.



31
32
33
# File 'lib/fatty/progress.rb', line 31

def total
  @total
end

#widthObject (readonly)

Returns the value of attribute width.



31
32
33
# File 'lib/fatty/progress.rb', line 31

def width
  @width
end

Instance Method Details

#clearObject



93
94
95
96
# File 'lib/fatty/progress.rb', line 93

def clear
  clear_status
  self
end

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fatty/progress.rb', line 77

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

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/fatty/progress.rb', line 66

def update(current: nil, indicator: nil, render: false)
  @current = normalize_current(current) unless current.nil?
  if style == :spinner
    advance_spinner
  else
    append_indicator(indicator)
  end
  refresh(render:)
  self
end