module Bcome::LoadingBar::Handler

Public Instance Methods

cursor() click to toggle source
# File lib/objects/loading_bar/handler.rb, line 16
def cursor
  ::TTY::Cursor
end
do_signal(signal) click to toggle source
# File lib/objects/loading_bar/handler.rb, line 51
def do_signal(signal)
  ::Process.kill(signal, @pid)
end
fork_process() click to toggle source
# File lib/objects/loading_bar/handler.rb, line 36
def fork_process
  @pid = fork do
    Signal.trap(::Bcome::LoadingBar::Indicator::Base::SIGNAL_SUCCESS) do
      @indicator.increment_success
    end

    Signal.trap(::Bcome::LoadingBar::Indicator::Base::SIGNAL_FAILURE) do
      @indicator.increment_failure
    end

    @indicator.indicate
  end
  ::Bcome::LoadingBar::PidBucket.instance << @pid
end
signal_failure() click to toggle source
# File lib/objects/loading_bar/handler.rb, line 73
def signal_failure
  do_signal(::Bcome::LoadingBar::Indicator::Base::SIGNAL_FAILURE)
  # Keep parent indicator in sync (see #signal_stop)
  @indicator.increment_failure
end
signal_stop() click to toggle source
# File lib/objects/loading_bar/handler.rb, line 55
def signal_stop
  # Stop the loader
  do_signal(::Bcome::LoadingBar::Indicator::Base::SIGNAL_STOP)

  # De-register its pid
  ::Bcome::LoadingBar::PidBucket.instance - @pid

  # Show the PARENT process indicator, which has been kept in sync with the forked process. We do this due to race condition where the forked indicator
  # does not gets final status before it is killed, so that it looks as if it has not completed
  @indicator.show
end
signal_success() click to toggle source
# File lib/objects/loading_bar/handler.rb, line 67
def signal_success
  do_signal(::Bcome::LoadingBar::Indicator::Base::SIGNAL_SUCCESS)
  # Keep parent indicator in sync (see #signal_stop)
  @indicator.increment_success
end
start_indicator(config) click to toggle source
# File lib/objects/loading_bar/handler.rb, line 6
def start_indicator(config)
  klass = config[:type] == :progress ? ::Bcome::LoadingBar::Indicator::Progress : ::Bcome::LoadingBar::Indicator::Basic
  @indicator = klass.new(config)
  fork_process
end
stop_indicator() click to toggle source
# File lib/objects/loading_bar/handler.rb, line 12
def stop_indicator
  signal_stop
end
wrap_indicator(config, &block) click to toggle source
# File lib/objects/loading_bar/handler.rb, line 20
def wrap_indicator(config, &block)
  begin
    start_indicator(config)
    cursor.invisible do
      block.call
    end
  rescue IRB::Abort
    stop_indicator
    raise ::Bcome::Exception::Generic, 'Interrupt'
  rescue StandardError => e
    stop_indicator
    raise ::Bcome::Exception::Generic, e.message if config[:type] == :basic
  end
  stop_indicator
end