Class: Starlined::Animation

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Animation.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/starlined/animation.rb', line 17

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 aliases
  @aliases = []
  @alias_pointer = 0
  @alias_timer = 0
  @alias_semaphore = Mutex.new

  # semáforo para pasos
  @steps_semaphore = Mutex.new

  # líneas visuales que ocupa la última impresión
  @last_lines = 1

  @running = false
  @thread = nil
end

Class Attribute Details

.pending_clear_linesObject

Returns the value of attribute pending_clear_lines.



14
15
16
# File 'lib/starlined/animation.rb', line 14

def pending_clear_lines
  @pending_clear_lines
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



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

def aliases
  @aliases
end

#current_stepsObject (readonly)

Returns the value of attribute current_steps.



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

def current_steps
  @current_steps
end

#last_linesObject (readonly)

Returns the value of attribute last_lines.



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

def last_lines
  @last_lines
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#stepsObject (readonly)

Returns the value of attribute steps.



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

def steps
  @steps
end

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#add_alias(aka) ⇒ Object



64
65
66
67
68
# File 'lib/starlined/animation.rb', line 64

def add_alias(aka)
  return if aka.nil?

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

#elapsed_timeObject



80
81
82
# File 'lib/starlined/animation.rb', line 80

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

#increment_stepObject



58
59
60
61
62
# File 'lib/starlined/animation.rb', line 58

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

#remove_alias(aka) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/starlined/animation.rb', line 70

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



43
44
45
46
47
48
# File 'lib/starlined/animation.rb', line 43

def start
  return if @running

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

#stopObject



50
51
52
53
54
55
56
# File 'lib/starlined/animation.rb', line 50

def stop
  return unless @running

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