Sha256: b9433ca40d2d364b5413d74dc0adae8ef62008be2050f579e4e7c4cbb4203dfe

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Backup
  class Logger
    class Console
      class Options
        ##
        # Disables all console output.
        #
        # This may also be set on the command line using +--quiet+.
        #
        # If +--no-quiet+ is used on the command line, console output
        # will be enabled and any setting here will be ignored.
        #
        # @param [Boolean, nil]
        # @return [Boolean, nil] Default: +false+
        attr_reader :quiet

        def initialize
          @quiet = false
        end

        def enabled?
          !quiet
        end

        def quiet=(val)
          @quiet = val unless quiet.nil?
        end
      end

      COLORS = {
        info: "\e[32m%s\e[0m", # green
        warn: "\e[33m%s\e[0m", # yellow
        error: "\e[31m%s\e[0m" # red
      }.freeze

      def initialize(_options = nil)
        $stdout.sync = $stderr.sync = true
      end

      def log(message)
        io = message.level == :info ? $stdout : $stderr
        lines = message.formatted_lines
        lines.map! { |line| COLORS[message.level] % line } if io.tty?
        io.puts lines
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backupii-0.1.0.pre.alpha.2 lib/backup/logger/console.rb
backupii-0.1.0.pre.alpha.1 lib/backup/logger/console.rb