Sha256: f3c840daaa0e27349dbff1e1781bef360672c24166e365c3a000951091f8a62b

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module Orchestration
  COLOR_MAP = {
    failure: %i[red bright],
    error: %i[red],
    ready: %i[green],
    create: %i[green],
    update: %i[yellow],
    backup: %i[blue],
    status: %i[blue],
    setup: %i[blue],
    input: %i[red],
    skip: %i[yellow bright],
    waiting: %i[yellow],
    config: %i[cyan]
  }.freeze

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

    def write(desc, message = nil, 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, newline: 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

      "#{Paint[desc.to_s.rjust(15), *color]} #{message}"
    end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
orchestration-0.6.5 lib/orchestration/terminal.rb
orchestration-0.6.4 lib/orchestration/terminal.rb
orchestration-0.6.3 lib/orchestration/terminal.rb
orchestration-0.6.2 lib/orchestration/terminal.rb
orchestration-0.6.1 lib/orchestration/terminal.rb
orchestration-0.6.0 lib/orchestration/terminal.rb