Sha256: 26d2d1ae24583914567076815dc2a0764516cc7d52fda95f578134306a73e0b5

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module CobraCommander
  module Executor
    # Runs an interactive output printer
    class OutputPrompt
      def self.run(pool, output = $stdout)
        new(pool.jobs).run(output)
      end

      pastel = Pastel.new
      ICONS = {
        nil => pastel.dim("ā¦»"),
        success: pastel.green("āœ”"),
        skip: pastel.yellow("ā†·"),
        error: pastel.red("āœ–"),
      }.freeze
      CANCELLED = pastel.dim("(cancelled)")
      BYE = pastel.bold("\n\nšŸ‘‹ Bye!")

      def initialize(jobs)
        @jobs = jobs
        @prompt = TTY::Prompt.new(symbols: { cross: " " })
      end

      def options
        @options ||= @jobs.map do |job|
          {
            name: format_name(job),
            value: job,
            disabled: !job.resolved? && CANCELLED,
          }
        end
      end

      def run(output)
        return unless @jobs.any?(&:resolved?)

        selected = nil
        output.puts
        loop do
          selected = @prompt.select("Print output?", options, default: format_name(selected))
          output.puts nil, selected.output, nil
        rescue TTY::Reader::InputInterrupt
          output.puts BYE
          break
        end
      end

    private

      def format_name(job)
        return unless job

        "#{ICONS[job.status]} #{job.name}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cobra_commander-1.1.0 lib/cobra_commander/executor/output_prompt.rb