Module: Kennel::Console

Defined in:
lib/kennel/console.rb

Defined Under Namespace

Classes: TeeIO

Constant Summary collapse

COLORS =
{ red: 31, green: 32, yellow: 33, cyan: 36, magenta: 35, default: 0 }.freeze

Class Method Summary collapse

Class Method Details

.ask?(question) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'lib/kennel/console.rb', line 22

def ask?(question)
  Kennel.err.printf color(:red, "#{question} -  press 'y' to continue: ", force: true)
  begin
    Kennel.in.gets.chomp == "y"
  rescue Interrupt # do not show a backtrace if user decides to Ctrl+C here
    Kennel.err.print "\n"
    exit 1
  end
end

.capture_stderrObject



47
48
49
50
51
52
53
54
# File 'lib/kennel/console.rb', line 47

def capture_stderr
  old = Kennel.err
  Kennel.err = StringIO.new
  yield
  Kennel.err.string
ensure
  Kennel.err = old
end

.capture_stdoutObject



38
39
40
41
42
43
44
45
# File 'lib/kennel/console.rb', line 38

def capture_stdout
  old = Kennel.out
  Kennel.out = StringIO.new
  yield
  Kennel.out.string
ensure
  Kennel.out = old
end

.color(color, text, force: false) ⇒ Object



32
33
34
35
36
# File 'lib/kennel/console.rb', line 32

def color(color, text, force: false)
  return text unless force || Kennel.out.tty?

  "\e[#{COLORS.fetch(color)}m#{text}\e[0m"
end

.tee_outputObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kennel/console.rb', line 56

def tee_output
  old_stdout = Kennel.out
  old_stderr = Kennel.err
  capture = StringIO.new
  Kennel.out = TeeIO.new([capture, Kennel.out])
  Kennel.err = TeeIO.new([capture, Kennel.err])
  yield
  capture.string
ensure
  Kennel.out = old_stdout
  Kennel.err = old_stderr
end

.tty?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/kennel/console.rb', line 18

def tty?
  !ENV["CI"] && (Kennel.in.tty? || Kennel.err.tty?)
end