Sha256: 91769431b1141f012b27a44dc35919fc5022f370fcf7e074d9c347fbe9d771e9

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require_relative 'ansi'

module WhirledPeas
  module Utils
    class FormattedString
      def self.blank
        new('')
      end

      attr_reader :formatting

      def initialize(raw, formatting=nil)
        @raw = raw
        @formatting = [*formatting]
      end

      def length
        raw.length
      end

      def each_char(&block)
        raw.chars.each(&block)
      end

      def blank?
        raw.empty?
      end

      def hash
        [raw, formatting].hash
      end

      def ==(other)
        case other
        when self.class
          hash == other.hash
        when String
          formatting.empty? && raw == other
        else
          false
        end
      end

      def to_s
        if raw.empty? || formatting.length == 0
          raw
        else
          start_formatting = formatting.map { |code| Ansi.esc_seq(code) }.join
          "#{start_formatting}#{raw}#{Ansi.clear}"
        end
      end

      def inspect
        if raw.empty? || formatting.length == 0
          raw.inspect
        else
          "<#{formatting.join(', ')}>#{raw}<0>".inspect
        end
      end

      private

      attr_reader :raw
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
whirled_peas-0.13.0 lib/whirled_peas/utils/formatted_string.rb
whirled_peas-0.12.0 lib/whirled_peas/utils/formatted_string.rb