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(config, 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
90
# File 'lib/megatest/output.rb', line 73

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

  @config = config
  @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



136
137
138
# File 'lib/megatest/output.rb', line 136

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

#blue(text) ⇒ Object



156
157
158
# File 'lib/megatest/output.rb', line 156

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

#colored(text) ⇒ Object



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

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

#colors?Boolean

Returns:

  • (Boolean)


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

def colors?
  @colors
end

#cyan(text) ⇒ Object



164
165
166
# File 'lib/megatest/output.rb', line 164

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

#error(message) ⇒ Object



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

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

#green(text) ⇒ Object



148
149
150
# File 'lib/megatest/output.rb', line 148

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

#grey(text) ⇒ Object



168
169
170
# File 'lib/megatest/output.rb', line 168

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

#indent(text, depth: 2) ⇒ Object



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

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

#magenta(text) ⇒ Object



160
161
162
# File 'lib/megatest/output.rb', line 160

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


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

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

#puts(*args) ⇒ Object



140
141
142
# File 'lib/megatest/output.rb', line 140

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

#red(text) ⇒ Object



144
145
146
# File 'lib/megatest/output.rb', line 144

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

#step(title, open: false) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/megatest/output.rb', line 123

def step(title, open: false)
  case @config.output_profile
  when :buildkite
    if open
      puts("+++ #{title}")
    else
      puts("--- #{title}")
    end
  else
    puts("* #{title}")
  end
end

#warning(message) ⇒ Object



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

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

#yellow(text) ⇒ Object



152
153
154
# File 'lib/megatest/output.rb', line 152

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