Class: Legion::TTY::Components::ProgressPanel

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/tty/components/progress_panel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, total:, output: $stdout) ⇒ ProgressPanel

Returns a new instance of ProgressPanel.



11
12
13
14
15
16
17
# File 'lib/legion/tty/components/progress_panel.rb', line 11

def initialize(title:, total:, output: $stdout)
  @title = title
  @total = total
  @current = 0
  @output = output
  @bar = build_bar
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



9
10
11
# File 'lib/legion/tty/components/progress_panel.rb', line 9

def current
  @current
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/legion/tty/components/progress_panel.rb', line 9

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



9
10
11
# File 'lib/legion/tty/components/progress_panel.rb', line 9

def total
  @total
end

Instance Method Details

#advance(step = 1) ⇒ Object



19
20
21
22
# File 'lib/legion/tty/components/progress_panel.rb', line 19

def advance(step = 1)
  @current = [@current + step, @total].min
  @bar&.advance(step)
end

#finishObject



24
25
26
27
28
# File 'lib/legion/tty/components/progress_panel.rb', line 24

def finish
  remaining = @total - @current
  @bar&.advance(remaining) if remaining.positive?
  @current = @total
end

#finished?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/legion/tty/components/progress_panel.rb', line 30

def finished?
  @current >= @total
end

#percentObject



34
35
36
37
38
# File 'lib/legion/tty/components/progress_panel.rb', line 34

def percent
  return 0 if @total.zero?

  ((@current.to_f / @total) * 100).round(1)
end

#render(width: 80) ⇒ Object



40
41
42
43
44
45
# File 'lib/legion/tty/components/progress_panel.rb', line 40

def render(width: 80)
  pct = percent
  label = Theme.c(:accent, @title)
  bar = build_render_bar(width, pct)
  "#{label} [#{bar}] #{Theme.c(:secondary, "#{pct}%")}"
end