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.

ansi_escapes.rb 3.6 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. module Cucumber
  3. module Gherkin
  4. module Formatter
  5. # Defines aliases for ANSI coloured output. Default colours can be overridden by defining
  6. # a <tt>GHERKIN_COLORS</tt> variable in your shell, very much like how you can
  7. # tweak the familiar POSIX command <tt>ls</tt> with
  8. # $LSCOLORS: http://linux-sxs.org/housekeeping/lscolors.html
  9. #
  10. # The colours that you can change are:
  11. #
  12. # <tt>undefined</tt>:: defaults to <tt>yellow</tt>
  13. # <tt>pending</tt>:: defaults to <tt>yellow</tt>
  14. # <tt>pending_arg</tt>:: defaults to <tt>yellow,bold</tt>
  15. # <tt>executing</tt>:: defaults to <tt>grey</tt>
  16. # <tt>executing_arg</tt>:: defaults to <tt>grey,bold</tt>
  17. # <tt>failed</tt>:: defaults to <tt>red</tt>
  18. # <tt>failed_arg</tt>:: defaults to <tt>red,bold</tt>
  19. # <tt>passed</tt>:: defaults to <tt>green</tt>
  20. # <tt>passed_arg</tt>:: defaults to <tt>green,bold</tt>
  21. # <tt>outline</tt>:: defaults to <tt>cyan</tt>
  22. # <tt>outline_arg</tt>:: defaults to <tt>cyan,bold</tt>
  23. # <tt>skipped</tt>:: defaults to <tt>cyan</tt>
  24. # <tt>skipped_arg</tt>:: defaults to <tt>cyan,bold</tt>
  25. # <tt>comment</tt>:: defaults to <tt>grey</tt>
  26. # <tt>tag</tt>:: defaults to <tt>cyan</tt>
  27. #
  28. # For instance, if your shell has a black background and a green font (like the
  29. # "Homebrew" settings for OS X' Terminal.app), you may want to override passed
  30. # steps to be white instead of green. Examples:
  31. #
  32. # export GHERKIN_COLORS="passed=white"
  33. # export GHERKIN_COLORS="passed=white,bold:passed_arg=white,bold,underline"
  34. #
  35. # (If you're on Windows, use SET instead of export).
  36. # To see what colours and effects are available, just run this in your shell:
  37. #
  38. # ruby -e "require 'rubygems'; require 'term/ansicolor'; puts Term::ANSIColor.attributes"
  39. #
  40. # Although not listed, you can also use <tt>grey</tt>
  41. module AnsiEscapes
  42. COLORS = {
  43. 'black' => "\e[30m",
  44. 'red' => "\e[31m",
  45. 'green' => "\e[32m",
  46. 'yellow' => "\e[33m",
  47. 'blue' => "\e[34m",
  48. 'magenta' => "\e[35m",
  49. 'cyan' => "\e[36m",
  50. 'white' => "\e[37m",
  51. 'grey' => "\e[90m",
  52. 'bold' => "\e[1m"
  53. }.freeze
  54. ALIASES = Hash.new do |h, k|
  55. "#{h[Regexp.last_match(1)]},bold" if k.to_s =~ /(.*)_arg/
  56. end.merge(
  57. 'undefined' => 'yellow',
  58. 'pending' => 'yellow',
  59. 'executing' => 'grey',
  60. 'failed' => 'red',
  61. 'passed' => 'green',
  62. 'outline' => 'cyan',
  63. 'skipped' => 'cyan',
  64. 'comments' => 'grey',
  65. 'tag' => 'cyan'
  66. )
  67. if ENV['GHERKIN_COLORS'] # Example: export GHERKIN_COLORS="passed=red:failed=yellow"
  68. ENV['GHERKIN_COLORS'].split(':').each do |pair|
  69. a = pair.split('=')
  70. ALIASES[a[0]] = a[1]
  71. end
  72. end
  73. ALIASES.each_key do |key|
  74. define_method(key) do
  75. ALIASES[key].split(',').map { |color| COLORS[color] }.join('')
  76. end
  77. define_method("#{key}_arg") do
  78. ALIASES["#{key}_arg"].split(',').map { |color| COLORS[color] }.join('')
  79. end
  80. end
  81. def reset
  82. "\e[0m"
  83. end
  84. def up(n)
  85. "\e[#{n}A"
  86. end
  87. end
  88. end
  89. end
  90. end

No Description

Contributors (1)