You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ansicolor.rb 4.0 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # frozen_string_literal: true
  2. module Cucumber
  3. module Term
  4. # This module allows to colorize text using ANSI escape sequences.
  5. #
  6. # Include the module in your class and use its methods to colorize text.
  7. #
  8. # Example:
  9. #
  10. # require 'cucumber/term/ansicolor'
  11. #
  12. # class MyFormatter
  13. # include Cucumber::Term::ANSIColor
  14. #
  15. # def initialize(config)
  16. # $stdout.puts yellow("Initializing formatter")
  17. # $stdout.puts green("Coloring is active \o/") if Cucumber::Term::ANSIColor.coloring?
  18. # $stdout.puts grey("Feature path:") + blue(bold(config.feature_dirs))
  19. # end
  20. # end
  21. #
  22. # To see what colours and effects are available, just run this in your shell:
  23. #
  24. # ruby -e "require 'rubygems'; require 'cucumber/term/ansicolor'; puts Cucumber::Term::ANSIColor.attributes"
  25. #
  26. module ANSIColor
  27. # :stopdoc:
  28. ATTRIBUTES = [
  29. [:clear, 0],
  30. [:reset, 0], # synonym for :clear
  31. [:bold, 1],
  32. [:dark, 2],
  33. [:italic, 3], # not widely implemented
  34. [:underline, 4],
  35. [:underscore, 4], # synonym for :underline
  36. [:blink, 5],
  37. [:rapid_blink, 6], # not widely implemented
  38. [:negative, 7], # no reverse because of String#reverse
  39. [:concealed, 8],
  40. [:strikethrough, 9], # not widely implemented
  41. [:black, 30],
  42. [:red, 31],
  43. [:green, 32],
  44. [:yellow, 33],
  45. [:blue, 34],
  46. [:magenta, 35],
  47. [:cyan, 36],
  48. [:white, 37],
  49. [:grey, 90],
  50. [:on_black, 40],
  51. [:on_red, 41],
  52. [:on_green, 42],
  53. [:on_yellow, 43],
  54. [:on_blue, 44],
  55. [:on_magenta, 45],
  56. [:on_cyan, 46],
  57. [:on_white, 47]
  58. ].freeze
  59. ATTRIBUTE_NAMES = ATTRIBUTES.transpose.first
  60. # :startdoc:
  61. # Regular expression that is used to scan for ANSI-sequences while
  62. # uncoloring strings.
  63. COLORED_REGEXP = /\e\[(?:[34][0-7]|[0-9])?m/
  64. @coloring = true
  65. class << self
  66. # Turns the coloring on or off globally, so you can easily do
  67. # this for example:
  68. # Cucumber::Term::ANSIColor::coloring = $stdout.isatty
  69. attr_accessor :coloring
  70. # Returns true, if the coloring function of this module
  71. # is switched on, false otherwise.
  72. alias coloring? :coloring
  73. def included(klass)
  74. return unless klass == String
  75. ATTRIBUTES.delete(:clear)
  76. ATTRIBUTE_NAMES.delete(:clear)
  77. end
  78. end
  79. ATTRIBUTES.each do |color_name, color_code|
  80. define_method(color_name) do |text = nil, &block|
  81. if block
  82. colorize(block.call, color_code)
  83. elsif text
  84. colorize(text, color_code)
  85. elsif respond_to?(:to_str)
  86. colorize(to_str, color_code)
  87. else
  88. colorize(nil, color_code) # switch coloration on
  89. end
  90. end
  91. end
  92. # Returns an uncolored version of the string
  93. # ANSI-sequences are stripped from the string.
  94. def uncolored(text = nil)
  95. if block_given?
  96. uncolorize(yield)
  97. elsif text
  98. uncolorize(text)
  99. elsif respond_to?(:to_str)
  100. uncolorize(to_str)
  101. else
  102. ''
  103. end
  104. end
  105. # Returns an array of all Cucumber::Term::ANSIColor attributes as symbols.
  106. def attributes
  107. ATTRIBUTE_NAMES
  108. end
  109. private
  110. def colorize(text, color_code)
  111. return String.new(text || '') unless Cucumber::Term::ANSIColor.coloring?
  112. return "\e[#{color_code}m" unless text
  113. "\e[#{color_code}m#{text}\e[0m"
  114. end
  115. def uncolorize(string)
  116. string.gsub(COLORED_REGEXP, '')
  117. end
  118. end
  119. end
  120. end

No Description

Contributors (1)