Sha256: 5c30f409e07bd1081d42aa388237425e1a98a174e53bbe9b8289928b777fda11

Contents?: true

Size: 1.57 KB

Versions: 30

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

module Inch
  module Utils
    class UI
      attr_reader :out, :err

      def initialize(stdout = $stdout, stderr = $stderr)
        @out, @err = stdout, stderr
      end

      def debug(msg)
        return unless ENV['DEBUG']
        msg.to_s.lines.each do |line|
          trace edged :dark, line.gsub(/\n$/, '').dark
        end
      end

      def sub(msg = '')
        color = @current_header_color || :white
        trace __edged(color, msg)
      end

      def edged(color, msg, edge = '┃ ')
        trace __edged(color, msg, edge)
      end

      # Writes the given +text+ to out
      #
      # @param text [String]
      # @return [void]
      def trace(text = '')
        @current_header_color = nil if text.to_s.empty?
        out.puts text
      end

      # Writes the given +text+ to err
      #
      # @param text [String]
      # @return [void]
      def warn(text = '')
        err.puts text
      end

      def header(text, color, bg_color = nil)
        @current_header_color = color
        trace __header(text, color, bg_color)
        trace unless use_color?
      end

      # @return [Boolean] true if the UI uses coloring
      def use_color?
        Term::ANSIColor.coloring?
      end

      private

      def __edged(color, msg, edge = '┃ ')
        edge.color(color) + msg
      end

      def __header(text, color, bg_color = nil)
        bg_color ||= "intense_#{color}"
        bar = " #{text}".ljust(CLI::COLUMNS - 1)
                .on_color(bg_color).color(:color16)
        '#'.color(color).on_color(color) + bar
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
inch-0.7.1 lib/inch/utils/ui.rb
inch-0.7.0 lib/inch/utils/ui.rb
inch-0.6.4 lib/inch/utils/ui.rb
inch-0.6.3 lib/inch/utils/ui.rb
inch-0.6.2 lib/inch/utils/ui.rb
inch-0.6.1 lib/inch/utils/ui.rb
inch-0.6.0 lib/inch/utils/ui.rb
inch-0.6.0.rc6 lib/inch/utils/ui.rb
inch-0.6.0.rc5 lib/inch/utils/ui.rb
inch-0.6.0.rc4 lib/inch/utils/ui.rb
inch-0.6.0.rc3 lib/inch/utils/ui.rb
inch-0.6.0.rc2 lib/inch/utils/ui.rb
inch-0.6.0.rc1 lib/inch/utils/ui.rb
inch-0.5.10 lib/inch/utils/ui.rb
inch-0.5.9 lib/inch/utils/ui.rb
inch-0.5.8 lib/inch/utils/ui.rb
inch-0.5.7 lib/inch/utils/ui.rb
inch-0.5.6 lib/inch/utils/ui.rb
inch-0.5.5 lib/inch/utils/ui.rb
inch-0.5.4 lib/inch/utils/ui.rb