Class: Starlined::Runner
- Inherits:
-
Object
- Object
- Starlined::Runner
- Defined in:
- lib/starlined/runner.rb
Instance Attribute Summary collapse
-
#animation ⇒ Object
readonly
Returns the value of attribute animation.
Instance Method Summary collapse
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
-
#run(command, print_err: true, aka: nil, no_count: false) ⇒ Object
Función que se encarga de ejecutar comandos y mostrar animaciones por pantalla.
- #start(message, steps: 0) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ Runner
Returns a new instance of Runner.
10 11 12 13 14 |
# File 'lib/starlined/runner.rb', line 10 def initialize @run_instances = 0 @run_semaphore = Mutex.new @animation = nil end |
Instance Attribute Details
#animation ⇒ Object (readonly)
Returns the value of attribute animation.
8 9 10 |
# File 'lib/starlined/runner.rb', line 8 def animation @animation end |
Instance Method Details
#run(command, print_err: true, aka: nil, no_count: false) ⇒ Object
Función que se encarga de ejecutar comandos y mostrar animaciones por pantalla. Utiliza un hilo separado para las animaciones y comprueba el exitcode de los callbacks para detectar errores. Si se ejecutan múltiples instancias de manera simultánea (como cuando se ejecutan comandos en paralelo), se utiliza un semáforo para controlar que no se creen más de un hilo de animación. Si se ejecuta un comando que requiere permisos de sudo, detecta si necesita contraseña y para la animación según corresponda.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/starlined/runner.rb', line 35 def run(command, print_err: true, aka: nil, no_count: false) handle_sudo if !!(command =~ /^sudo/) @run_semaphore.synchronize do @animation&.add_alias(aka) unless aka.nil? @animation.start if @run_instances.zero? && @animation @run_instances += 1 end result = -> { Open3.capture3(command) }.call @run_semaphore.synchronize do @run_instances -= 1 @animation&.increment_step unless no_count @animation&.remove_alias(aka) unless aka.nil? @animation.stop if @run_instances.zero? && @animation end handle_error(result, print_err) unless result.last.success? result end |
#start(message, steps: 0) ⇒ Object
16 17 18 19 20 |
# File 'lib/starlined/runner.rb', line 16 def start(, steps: 0) stop_animation if @animation @animation = Animation.new(, steps) @animation end |
#stop ⇒ Object
22 23 24 25 26 27 |
# File 'lib/starlined/runner.rb', line 22 def stop return unless @animation Messages.success(@animation., @animation.elapsed_time) stop_animation end |