Class: Starlined::Animation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = 'Booting up', steps = 0) ⇒ Animation

Returns a new instance of Animation.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/starlined/animation.rb', line 10

def initialize(message = 'Booting up', steps = 0)
  @message = message
  @start_time = Time.now
  @steps = steps
  @current_steps = 0

  # variables de animación
  @pos = [0, 1, 2]
  @move = 1

  # manejo de alias
  @aliases = []
  @alias_pointer = 0
  @alias_timer = 0
  @alias_semaphore = Mutex.new

  @steps_semaphore = Mutex.new

  @running = false
  @thread = nil
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



8
9
10
# File 'lib/starlined/animation.rb', line 8

def aliases
  @aliases
end

#current_stepsObject (readonly)

Returns the value of attribute current_steps.



7
8
9
# File 'lib/starlined/animation.rb', line 7

def current_steps
  @current_steps
end

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/starlined/animation.rb', line 7

def message
  @message
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



7
8
9
# File 'lib/starlined/animation.rb', line 7

def start_time
  @start_time
end

#stepsObject (readonly)

Returns the value of attribute steps.



7
8
9
# File 'lib/starlined/animation.rb', line 7

def steps
  @steps
end

#threadObject (readonly)

Returns the value of attribute thread.



7
8
9
# File 'lib/starlined/animation.rb', line 7

def thread
  @thread
end

Instance Method Details

#add_alias(aka) ⇒ Object



53
54
55
56
57
# File 'lib/starlined/animation.rb', line 53

def add_alias(aka)
  return if aka.nil?

  @alias_semaphore.synchronize { @aliases.push(aka) }
end

#elapsed_timeObject



69
70
71
# File 'lib/starlined/animation.rb', line 69

def elapsed_time
  (Time.now - @start_time).round(2).to_s
end

#increment_stepObject



47
48
49
50
51
# File 'lib/starlined/animation.rb', line 47

def increment_step
  @steps_semaphore.synchronize do
    @current_steps += 1 if @current_steps < @steps
  end
end

#remove_alias(aka) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/starlined/animation.rb', line 59

def remove_alias(aka)
  return if aka.nil?

  @alias_semaphore.synchronize do
    @aliases.delete(aka)
    # ajustar el puntero si se eliminó un elemento antes de la posición actual
    @alias_pointer = 0 if @alias_pointer >= @aliases.length && !@aliases.empty?
  end
end

#startObject



32
33
34
35
36
37
# File 'lib/starlined/animation.rb', line 32

def start
  return if @running

  @running = true
  @thread = Thread.new { animation_loop }
end

#stopObject



39
40
41
42
43
44
45
# File 'lib/starlined/animation.rb', line 39

def stop
  return unless @running

  @running = false
  @thread&.kill
  @thread = nil
end