Sha256: 02e5fbe48d8c8c2024f3e68584ff46971633200af528ff635ee3264b820834ae

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Orchestration
  COLOR_MAP = {
    failure: :light_red,
    error: :red,
    waiting: :yellow,
    ready: :green,
    create: :green,
    update: :yellow,
    status: :blue,
    setup: :blue,
    input: :red,
    skip: :light_yellow
  }.freeze

  class Terminal
    def initialize(settings)
      @settings = settings
    end

    def write(desc, message, color_name = nil, newline = true)
      output = newline ? "#{message}\n" : message.to_s
      STDOUT.print colorize(desc, output, color_name)
      STDOUT.flush
    end

    def read(message, default = nil)
      write(:input, prompt(message, default), nil, false)
      result = STDIN.gets.chomp.strip
      return default if result.empty?

      result
    end

    def ask_setting(setting, default = nil)
      return unless @settings.get(setting).nil?

      write(:setup, t("settings.#{setting}.description"))
      prompt = t("settings.#{setting}.prompt")
      @settings.set(setting, read(prompt, default))
    end

    private

    def prompt(message, default)
      return "(#{message}): " if default.nil?

      "(#{message}) [#{t('default')}: #{default}]: "
    end

    def colorize(desc, message, color_name)
      color = if color_name.nil?
                COLOR_MAP.fetch(desc)
              else
                COLOR_MAP.fetch(color_name)
              end
      desc
        .to_s
        .rjust(15)
        .colorize(mode: :default, color: color)
        .concat(' ' + message)
    end

    def t(key)
      I18n.t("orchestration.#{key}")
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
orchestration-0.3.17 lib/orchestration/terminal.rb
orchestration-0.3.16 lib/orchestration/terminal.rb
orchestration-0.3.15 lib/orchestration/terminal.rb
orchestration-0.3.14 lib/orchestration/terminal.rb
orchestration-0.3.13 lib/orchestration/terminal.rb
orchestration-0.3.12 lib/orchestration/terminal.rb
orchestration-0.3.11 lib/orchestration/terminal.rb
orchestration-0.3.10 lib/orchestration/terminal.rb
orchestration-0.3.9 lib/orchestration/terminal.rb