Class: ProgressBar

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

Overview

Displays a progress bar for a given set of stages.

Constant Summary collapse

STAGES =

All stages Kamal goes through.

[
  'Log into image registry',
  'Build and push app image',
  'Acquiring the deploy lock',
  'Ensure Traefik is running',
  'Detect stale containers',
  'Start container',
  'Prune old containers and images',
  'Releasing the deploy lock',
  'Finished all'
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ ProgressBar

Initializes the progress bar with the given window.

Parameters:

  • window (Curses::Window)


24
25
26
27
28
29
# File 'lib/kamalx.rb', line 24

def initialize(window)
  @window = window
  @progress = 0
  @total_steps = STAGES.size
  @blink_state = true
end

Instance Method Details

#drawObject

Draws the progress bar based on the current progress.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kamalx.rb', line 45

def draw
  @window.clear
  width = @window.maxx - 2
  progress_width = [(width * (@progress.to_f / @total_steps)).to_i, 1].max

  # Draw progress bar
  @window.setpos(0, 0)
  @window.attron(Curses.color_pair(6)) do # Use green color
    progress_bar = '=' * (progress_width - 1)
    cursor = @blink_state ? '>' : ' '
    progress_bar += @progress == @total_steps ? '>' : cursor
    @window.addstr("[#{progress_bar}#{' ' * [width - progress_width, 0].max}]")
  end

  # Draw stage information
  stage_info = "Current Stage: #{STAGES[@progress - 1]}"
  @window.setpos(1, (width - stage_info.length) / 2)
  @window.attron(Curses::A_BOLD)
  @window.addstr('Current Stage:')
  @window.attroff(Curses::A_BOLD)
  @window.addstr(" #{STAGES[@progress - 1]}")

  @window.refresh
  @blink_state = !@blink_state
end

#finishObject

Finishes the progress bar by setting it to the last stage.



72
73
74
75
# File 'lib/kamalx.rb', line 72

def finish
  @progress = @total_steps
  draw
end

#update(line) ⇒ Object

Updates the progress bar with the given line.

Parameters:

  • line (String)


34
35
36
37
38
39
40
41
42
# File 'lib/kamalx.rb', line 34

def update(line)
  STAGES.each_with_index do |stage, index|
    next unless line.include?(stage)

    @progress = index + 1
    draw
    break
  end
end