模块:RSpec::Core::Formatters::ConsoleCodes

定义在
lib/rspec/core/formatters/console_codes.rb

概述

ConsoleCodes 提供了使用 ANSI 代码(例如颜色和粗体)格式化控制台输出的辅助方法。

类方法摘要 折叠

类方法细节

.console_code_for(code_or_symbol) ⇒Fixnum

获取提供的符号的正确代码,或检查代码是否有效。默认值为白色 (37)。

参数

  • code_or_symbol (Symbol, Fixnum)

    要检查的符号或代码

返回值

  • (Fixnum)

    控制台代码

47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/core/formatters/console_codes.rb', line 47
def console_code_for(code_or_symbol)
  if (config_method = config_colors_to_methods[code_or_symbol])
    console_code_for RSpec.configuration.__send__(config_method)
  elsif VT100_CODE_VALUES.key?(code_or_symbol)
    code_or_symbol
  else
    VT100_CODES.fetch(code_or_symbol) do
      console_code_for(:white)
    end
  end
end

.wrap(text, code_or_symbol) ⇒String

使用提供的代码将一段文本包装在 ANSI 代码中。仅当 RSpec.configuration.color_enabled? 返回 true 时才应用控制代码。

参数

  • text (String)

    要包装的文本

  • code_or_symbol (Symbol, Fixnum)

    所需的控制代码

返回值

  • (String)

    包装后的文本

66
67
68
69
70
71
72
# File 'lib/rspec/core/formatters/console_codes.rb', line 66
def wrap(text, code_or_symbol)
  if RSpec.configuration.color_enabled?
    "\e[#{console_code_for(code_or_symbol)}m#{text}\e[0m"
  else
    text
  end
end