Class: Megatest::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/megatest/output.rb

Defined Under Namespace

Modules: ANSIColors, NoColors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, colors: nil) ⇒ Output

Returns a new instance of Output.

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/megatest/output.rb', line 73

def initialize(io, colors: nil)
  raise ArgumentError, "don't nest outputs" if io.is_a?(Output)

  @io = io
  colors = io.tty? if colors.nil?
  case colors
  when true
    @colors = true
    @color = ANSIColors
  when false
    @colors = false
    @color = NoColors
  else
    @color = colors
    @colors = @color != NoColors
  end
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



71
72
73
# File 'lib/megatest/output.rb', line 71

def color
  @color
end

Instance Method Details

#<<(str) ⇒ Object



122
123
124
# File 'lib/megatest/output.rb', line 122

def <<(str)
  @io << str
end

#blue(text) ⇒ Object



142
143
144
# File 'lib/megatest/output.rb', line 142

def blue(text)
  @color.blue(text)
end

#colored(text) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/megatest/output.rb', line 102

def colored(text)
  if @colors
    text
  else
    ANSIColors.strip(text)
  end
end

#colors?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/megatest/output.rb', line 91

def colors?
  @colors
end

#cyan(text) ⇒ Object



150
151
152
# File 'lib/megatest/output.rb', line 150

def cyan(text)
  @color.cyan(text)
end

#error(message) ⇒ Object



114
115
116
# File 'lib/megatest/output.rb', line 114

def error(message)
  puts(red(message))
end

#green(text) ⇒ Object



134
135
136
# File 'lib/megatest/output.rb', line 134

def green(text)
  @color.green(text)
end

#grey(text) ⇒ Object



154
155
156
# File 'lib/megatest/output.rb', line 154

def grey(text)
  @color.grey(text)
end

#indent(text, depth: 2) ⇒ Object



95
96
97
98
99
100
# File 'lib/megatest/output.rb', line 95

def indent(text, depth: 2)
  prefix = " " * depth
  lines = text.lines
  lines.map! { |l| "#{prefix}#{l}" }
  lines.join
end

#magenta(text) ⇒ Object



146
147
148
# File 'lib/megatest/output.rb', line 146

def magenta(text)
  @color.magenta(text)
end


118
119
120
# File 'lib/megatest/output.rb', line 118

def print(*args)
  @io.print(*args)
end

#puts(*args) ⇒ Object



126
127
128
# File 'lib/megatest/output.rb', line 126

def puts(*args)
  @io.puts(*args)
end

#red(text) ⇒ Object



130
131
132
# File 'lib/megatest/output.rb', line 130

def red(text)
  @color.red(text)
end

#warning(message) ⇒ Object



110
111
112
# File 'lib/megatest/output.rb', line 110

def warning(message)
  puts(yellow(message))
end

#yellow(text) ⇒ Object



138
139
140
# File 'lib/megatest/output.rb', line 138

def yellow(text)
  @color.yellow(text)
end