Sha256: ce0fffd9ce8420c74194775d4db399f28b5bd3a2d50c54a569b98c8e08b38f00

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require "pastel"
require "tty-prompt"

module CobraCommander
  module Executor
    # Runs an interactive output printer
    class InteractivePrinter
      pastel = Pastel.new
      SUCCESS = "#{pastel.green('āœ”')} %s"
      ERROR = "#{pastel.red('āœ–')} %s"
      BYE = pastel.decorate("\n\nšŸ‘‹ Bye!", :white, :on_black, :bold).freeze

      def self.run(execution, output)
        new(execution).run(output)
      end

      def initialize(execution)
        @prompt = TTY::Prompt.new
        @execution = execution
      end

      def run(output)
        selected = nil
        loop do
          selected = @prompt.select("Print output?", options, default: options.key(selected))
          output.puts selected.fulfilled? ? selected.value : selected.reason
        end
      rescue TTY::Reader::InputInterrupt
        output.puts BYE
      end

    private

      def options
        @options ||= @execution.sort { |*args| sort_results(*args.flatten) }
                               .reduce({}) do |options, (job, result)|
          template = result.rejected? ? ERROR : SUCCESS
          options.merge format(template, job.to_s) => result
        end
      end

      def sort_results(job_a, result_a, job_b, result_b)
        if result_a.rejected? == result_b.rejected?
          job_a.to_s <=> job_b.to_s
        else
          result_b.rejected? ? 1 : -1
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cobra_commander-1.0.1 lib/cobra_commander/executor/interactive_printer.rb
cobra_commander-1.0.0 lib/cobra_commander/executor/interactive_printer.rb