class Utils::ProbeServer

def clear

def clear
  system "clear"
end

def cmd(job)

def cmd(job)
  call = []
  if ENV.key?('BUNDLE_GEMFILE') and bundle = `which bundle`.full?(:chomp)
    call << bundle << 'exec'
  end
  call.push($0, *job)
  #output_message "Executing #{call.inspect} now.", type: :info
  call
end

def env

def env
w(self, ENV)

def help

def help
  docs      = doc_annotations.sort_by(&:first)
  docs_size = docs.map { |a| a.first.size }.max
  format = '%-20s %-3s %s'
  output_message [
      (format % %w[ command sho description ]).on_color(20).white
    ] << docs.map { |cmd, doc|
      shortcut = shortcut_of(cmd) and shortcut = "(#{shortcut})"
      format % [ cmd, shortcut, doc ]
    }
end

def history_clear

def history_clear
  @history = []
  true
end

def history_list

def history_list
  output_message @history
end

def initialize(uri)

def initialize(uri)
  @uri        = uri
  @history    = [].freeze
  @jobs_queue = Queue.new
  @current_job_id = 0
  Thread.new { work_loop }
end

def inspect

def inspect
  "#<Probe #queue=#{@jobs_queue.size}>"
end

def job_enqueue(job_args)

def job_enqueue(job_args)
  job = Job.new(self, job_args)
  output_message " → #{job.inspect} enqueued.", type: :info
  @jobs_queue.push job
end

def job_repeat(job_id = @history.last)

def job_repeat(job_id = @history.last)
  Job === job_id and job_id = job_id.id
  if old_job = @history.find { |job| job.id == job_id }
    job_enqueue old_job.args
    true
  else
    false
  end
end

def next_job_id

def next_job_id
  @current_job_id += 1
end

def output_message(msg, type: nil)

def output_message(msg, type: nil)
  msg.respond_to?(:to_a) and msg = msg.to_a * "\n"
  msg =
    case type
    when :success
      msg.on_color(22).white
    when :info
      msg.on_color(20).white
    when :warn
      msg.on_color(94).white
    when :failure
      msg.on_color(124).blink.white
    else
      msg
    end
  STDOUT.puts msg
  STDOUT.flush
  self
end

def print(*msg)

def print(*msg)
  if msg.first !~ /^irb: warn: can't alias / # shut your god d*mn wh*re mouth
    super
  end
end

def run_job(job)

def run_job(job)
  output_message " → #{job.inspect} now running.", type: :info
  system *cmd(job.args)
  message = " → #{job.inspect} was just run"
  if $?.success?
    job.ok = true
    message << " successfully."
    output_message message, type: :success
  else
    job.ok = false
    message << " and failed with exit status #{$?.exitstatus}!"
    output_message message, type: :failure
  end
  @history += [ @job.freeze ]
  @history.freeze
  @job = nil
end

def shutdown

def shutdown
  output_message "Server was shutdown down – HARD!", type: :warn
  exit 23
end

def start

def start
  output_message "Starting probe server listening to #{@uri.inspect}.", type: :info
  DRb.start_service(@uri, self)
  begin
    DRb.thread.join
  rescue Interrupt
    ARGV.clear << '-f'
    output_message %{\nEntering interactive mode.}, type: :info
    help
    begin
      old, $VERBOSE = $VERBOSE, nil
      examine(self)
    ensure
      $VERBOSE = old
    end
    output_message "Quitting interactive mode, but still listening to #{@uri.inspect}.", type: :info
    retry
  end
end

def work_loop

def work_loop
  loop do
    @job = @jobs_queue.shift
    run_job @job
  end
end