Sha256: eb3bddf5c3eb714d1d6312f06a40d7538ba3682d014bf5f0953fc336bd661749

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module Reviewer
  class Output
    # Simple class for streamlining the output of 'tokens' representing a style and content
    #
    # @author [garrettdimon]
    #
    class Token
      ESC = "\e["

      attr_accessor :style, :content

      def initialize(style, content)
        @style = style
        @content = content
      end

      def to_s
        [
          style_string,
          content,
          reset_string
        ].join
      end

      private

      def style_string
        "#{ESC}#{weight};#{color}m"
      end

      def reset_string
        "#{ESC}0m"
      end

      def weight_key
        style_components[0]
      end

      def color_key
        style_components[1]
      end

      def weight
        {
          default: 0,
          bold: 1,
          light: 2,
          italic: 3
        }.fetch(weight_key)
      end

      def color
        {
          black: 30,
          red: 31,
          green: 32,
          yellow: 33,
          blue: 34,
          magenta: 35,
          cyan: 36,
          gray: 37,
          default: 39
        }.fetch(color_key)
      end

      def style_components
        {
          success_bold: %i[bold green],
          success: %i[default green],
          success_light: %i[light green],
          error: %i[bold red],
          failure: %i[default red],
          warning: %i[bold yellow],
          warning_light: %i[light yellow],
          source: %i[italic default],
          bold: %i[default default],
          default: %i[default default],
          muted: %i[light gray]
        }.fetch(style)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reviewer-0.1.5 lib/reviewer/output/token.rb