Class: Chamomile::Progress
- Inherits:
-
Object
- Object
- Chamomile::Progress
- 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
-
#damping ⇒ Object
readonly
Returns the value of attribute damping.
-
#empty_char ⇒ Object
Returns the value of attribute empty_char.
-
#empty_color ⇒ Object
Returns the value of attribute empty_color.
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#full_char ⇒ Object
Returns the value of attribute full_char.
-
#full_color ⇒ Object
Returns the value of attribute full_color.
-
#gradient ⇒ Object
Returns the value of attribute gradient.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#percent ⇒ Object
readonly
Returns the value of attribute percent.
-
#percentage_format ⇒ Object
Returns the value of attribute percentage_format.
-
#show_percentage ⇒ Object
Returns the value of attribute show_percentage.
-
#width ⇒ Object
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #animating? ⇒ Boolean
- #decr_percent(v) ⇒ Object
- #handle(msg) ⇒ Object (also: #update)
- #incr_percent(v) ⇒ Object
-
#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
constructor
A new instance of Progress.
- #set_percent(p) ⇒ Object
- #set_spring_options(frequency, damping) ⇒ Object
- #view ⇒ Object
- #view_as(percent) ⇒ Object
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
#damping ⇒ Object (readonly)
Returns the value of attribute damping.
34 35 36 |
# File 'lib/chamomile/components/progress.rb', line 34 def damping @damping end |
#empty_char ⇒ Object
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_color ⇒ Object
Returns the value of attribute empty_color.
35 36 37 |
# File 'lib/chamomile/components/progress.rb', line 35 def empty_color @empty_color end |
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
34 35 36 |
# File 'lib/chamomile/components/progress.rb', line 34 def frequency @frequency end |
#full_char ⇒ Object
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_color ⇒ Object
Returns the value of attribute full_color.
35 36 37 |
# File 'lib/chamomile/components/progress.rb', line 35 def full_color @full_color end |
#gradient ⇒ Object
Returns the value of attribute gradient.
35 36 37 |
# File 'lib/chamomile/components/progress.rb', line 35 def gradient @gradient end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
34 35 36 |
# File 'lib/chamomile/components/progress.rb', line 34 def id @id end |
#percent ⇒ Object (readonly)
Returns the value of attribute percent.
34 35 36 |
# File 'lib/chamomile/components/progress.rb', line 34 def percent @percent end |
#percentage_format ⇒ Object
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_percentage ⇒ Object
Returns the value of attribute show_percentage.
35 36 37 |
# File 'lib/chamomile/components/progress.rb', line 35 def show_percentage @show_percentage end |
#width ⇒ Object
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_id ⇒ Object
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
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 (frequency, damping) @frequency = frequency.to_f @damping = damping.to_f end |
#view ⇒ Object
106 107 108 |
# File 'lib/chamomile/components/progress.rb', line 106 def view (@percent) end |
#view_as(percent) ⇒ Object
110 111 112 |
# File 'lib/chamomile/components/progress.rb', line 110 def view_as(percent) (percent.to_f.clamp(0.0, 1.0)) end |