Sha256: a9faea52a334ab6c1f0aaf8f23fec8b6174a8705b517951609fbf29a6e6b2df4

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8

module Backup
  module 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
      }

      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

4 entries across 4 versions & 1 rubygems

Version Path
backup-3.1.3 lib/backup/logger/console.rb
backup-3.1.2 lib/backup/logger/console.rb
backup-3.1.1 lib/backup/logger/console.rb
backup-3.1.0 lib/backup/logger/console.rb