Class: Chamomile::Progress

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

Overview

Animated progress bar with spring-based animation and optional gradient.

Constant Summary collapse

DEFAULT_WIDTH =
40
FULL_CHAR =
"\u2588"
EMPTY_CHAR =
"\u2591"
FREQUENCY =

Spring constants

14.0
DAMPING =
2.2
EPSILON =
0.001
FPS =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: DEFAULT_WIDTH, show_percentage: true, percentage_format: " %3.0f%%", full_char: FULL_CHAR, empty_char: EMPTY_CHAR, full_color: nil, empty_color: nil, gradient: nil, frequency: FREQUENCY, damping: DAMPING) ⇒ Progress

Returns a new instance of Progress.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chamomile/components/progress.rb', line 38

def initialize(width: DEFAULT_WIDTH, show_percentage: true, percentage_format: " %3.0f%%",
               full_char: FULL_CHAR, empty_char: EMPTY_CHAR,
               full_color: nil, empty_color: nil, gradient: nil,
               frequency: FREQUENCY, damping: DAMPING)
  @id = self.class.next_id
  @width = width
  @full_char = full_char
  @empty_char = empty_char
  @show_percentage = show_percentage
  @percentage_format = percentage_format
  @full_color = full_color
  @empty_color = empty_color
  @gradient = gradient
  @frequency = frequency.to_f
  @damping = damping.to_f
  @percent = 0.0
  @target = 0.0
  @velocity = 0.0
  @tag = 0
  @animating = false
end

Instance Attribute Details

#dampingObject (readonly)

Returns the value of attribute damping.



34
35
36
# File 'lib/chamomile/components/progress.rb', line 34

def damping
  @damping
end

#empty_charObject

Returns the value of attribute empty_char.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def empty_char
  @empty_char
end

#empty_colorObject

Returns the value of attribute empty_color.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def empty_color
  @empty_color
end

#frequencyObject (readonly)

Returns the value of attribute frequency.



34
35
36
# File 'lib/chamomile/components/progress.rb', line 34

def frequency
  @frequency
end

#full_charObject

Returns the value of attribute full_char.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def full_char
  @full_char
end

#full_colorObject

Returns the value of attribute full_color.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def full_color
  @full_color
end

#gradientObject

Returns the value of attribute gradient.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def gradient
  @gradient
end

#idObject (readonly)

Returns the value of attribute id.



34
35
36
# File 'lib/chamomile/components/progress.rb', line 34

def id
  @id
end

#percentObject (readonly)

Returns the value of attribute percent.



34
35
36
# File 'lib/chamomile/components/progress.rb', line 34

def percent
  @percent
end

#percentage_formatObject

Returns the value of attribute percentage_format.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def percentage_format
  @percentage_format
end

#show_percentageObject

Returns the value of attribute show_percentage.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def show_percentage
  @show_percentage
end

#widthObject

Returns the value of attribute width.



35
36
37
# File 'lib/chamomile/components/progress.rb', line 35

def width
  @width
end

Class Method Details

.next_idObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/chamomile/components/progress.rb', line 22

def self.next_id
  @id_mutex.synchronize do
    if Process.pid != @id_pid
      @id_pid = Process.pid
      @next_id = 0
      @id_mutex = Mutex.new
    end
    @next_id += 1
    "#{@id_pid}-pg-#{@next_id}"
  end
end

Instance Method Details

#animating?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/chamomile/components/progress.rb', line 78

def animating?
  @animating
end

#decr_percent(v) ⇒ Object



69
70
71
# File 'lib/chamomile/components/progress.rb', line 69

def decr_percent(v)
  set_percent(@target - v)
end

#handle(msg) ⇒ Object Also known as: update



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chamomile/components/progress.rb', line 82

def handle(msg)
  return unless msg.is_a?(ProgressFrameMsg)
  return unless msg.id == @id && msg.tag == @tag

  dt = 1.0 / FPS
  force = (@target - @percent) * @frequency
  @velocity = (@velocity + (force * dt)) * (1.0 / (1.0 + (@damping * dt)))
  @percent += @velocity * dt
  @percent = @percent.clamp(0.0, 1.0)

  if (@percent - @target).abs < EPSILON && @velocity.abs < EPSILON
    @percent = @target
    @velocity = 0.0
    @animating = false
    @tag += 1
    nil
  else
    @tag += 1
    frame_cmd
  end
end

#incr_percent(v) ⇒ Object



65
66
67
# File 'lib/chamomile/components/progress.rb', line 65

def incr_percent(v)
  set_percent(@target + v)
end

#set_percent(p) ⇒ Object



60
61
62
63
# File 'lib/chamomile/components/progress.rb', line 60

def set_percent(p)
  @target = p.to_f.clamp(0.0, 1.0)
  start_animation
end

#set_spring_options(frequency, damping) ⇒ Object



73
74
75
76
# File 'lib/chamomile/components/progress.rb', line 73

def set_spring_options(frequency, damping)
  @frequency = frequency.to_f
  @damping = damping.to_f
end

#viewObject



106
107
108
# File 'lib/chamomile/components/progress.rb', line 106

def view
  render_bar(@percent)
end

#view_as(percent) ⇒ Object



110
111
112
# File 'lib/chamomile/components/progress.rb', line 110

def view_as(percent)
  render_bar(percent.to_f.clamp(0.0, 1.0))
end