Sha256: 502d7d870272df653091bd71dceafc15d464eb1c57ae451636dd93c2e0d7c6f5

Contents?: true

Size: 713 Bytes

Versions: 25

Compression:

Stored size: 713 Bytes

Contents

# frozen_string_literal: true

module Nanoc::CLI
  # A simple ANSI colorizer for strings. When given a string and a list of
  # attributes, it returns a colorized string.
  #
  # @api private
  module ANSIStringColorizer
    # TODO: complete mapping
    MAPPING = {
      bold: "\e[1m",
      red: "\e[31m",
      green: "\e[32m",
      yellow: "\e[33m",
      blue: "\e[34m",
    }.freeze

    # @param [String] s The string to colorize
    #
    # @param [Array] as An array of attributes from `MAPPING` to colorize the
    #   string with
    #
    # @return [String] A string colorized using the given attributes
    def self.c(s, *as)
      as.map { |a| MAPPING[a] }.join('') + s + "\e[0m"
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
nanoc-4.8.19 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.18 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.17 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.16 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.15 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.14 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.13 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.12 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.11 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.10 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.9 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.8 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.7 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.6 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.5 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.4 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.3 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.2 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.1 lib/nanoc/cli/ansi_string_colorizer.rb
nanoc-4.8.0 lib/nanoc/cli/ansi_string_colorizer.rb