Sha256: 00336523930a9a130bc0fea2df79c706bc737b6cc5a4a1d69e7a483be1760077

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require 'awesome_print'

module Uffizzi
  module UI
    class Shell
      attr_accessor :output_format

      PRETTY_JSON = 'pretty-json'
      REGULAR_JSON = 'json'
      GITHUB_ACTION = 'github-action'

      def initialize
        @shell = Thor::Shell::Basic.new
      end

      def say(message)
        formatted_message = case output_format
                            when PRETTY_JSON
                              format_to_pretty_json(message)
                            when REGULAR_JSON
                              format_to_json(message)
                            when GITHUB_ACTION
                              format_to_github_action(message)
                            else
                              message
        end
        @shell.say(formatted_message)
      end

      def print_in_columns(messages)
        @shell.print_in_columns(messages)
      end

      def print_table(table_data)
        @shell.print_table(table_data)
      end

      def ask(message, *args)
        answer = @shell.ask(message, *args)
        options = args.last.is_a?(Hash) ? args.pop : {}
        say("\n") unless options.fetch(:echo, true)
        answer
      end

      def last_message
        @shell.send(:stdout).string.strip
      end

      def disable_stdout
        $stdout = StringIO.new
      end

      def enable_stdout
        $stdout = IO.new(1, 'w')
      end

      private

      def format_to_json(data)
        data.to_json
      end

      def format_to_pretty_json(data)
        JSON.pretty_generate(data)
      end

      def format_to_github_action(data)
        return '' unless data.is_a?(Hash)

        data.reduce('') { |acc, (key, value)| "#{acc}::set-output name=#{key}::#{value}\n" }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
uffizzi-cli-1.0.5 lib/uffizzi/shell.rb
uffizzi-cli-1.0.4 lib/uffizzi/shell.rb
uffizzi-cli-1.0.3 lib/uffizzi/shell.rb
uffizzi-cli-1.0.2 lib/uffizzi/shell.rb
uffizzi-cli-1.0.1 lib/uffizzi/shell.rb