Module: Xcode::TerminalOutput

Constant Summary collapse

LEVELS =
[
  :error,
  :warning,
  :notice,
  :info,
  :debug
]
@@colour_enabled =
true
@@log_level =
:info

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



25
26
27
# File 'lib/xcode/terminal_output.rb', line 25

def self.included(base)
  @@colour_supported = terminal_supports_colors?
end

.log_level=(level) ⇒ Object



20
21
22
23
# File 'lib/xcode/terminal_output.rb', line 20

def self.log_level=(level)
  raise "Unknown log level #{level}, should be one of #{LEVELS.join(', ')}" unless LEVELS.include? level
  @@log_level = level
end

.terminal_supports_colors?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/xcode/terminal_output.rb', line 103

def self.terminal_supports_colors?
  # No colors unless we are being run via a TTY
  return false unless $stdout.isatty

  # Check if the terminal supports colors
  colors = `tput colors 2> /dev/null`.chomp
  if $?.exitstatus == 0
    colors.to_i >= 8
  else
    false
  end
end

Instance Method Details

#color_output=(color_output) ⇒ Object



29
30
31
# File 'lib/xcode/terminal_output.rb', line 29

def color_output= color_output
  @@colour_enabled = color_output
end

#color_output?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/xcode/terminal_output.rb', line 33

def color_output?
  @@colour_supported and @@colour_enabled
end

#format_lhs(left, right, terminator = ":") ⇒ Object



57
58
59
60
# File 'lib/xcode/terminal_output.rb', line 57

def format_lhs(left, right, terminator=":")
  # "#{left.to_s.ljust(10)} #{right.rjust(6)}#{terminator} "
  "#{right.to_s.rjust(7)}#{terminator} "
end

#log_levelObject



16
17
18
# File 'lib/xcode/terminal_output.rb', line 16

def log_level
  @@log_level
end


98
99
100
101
# File 'lib/xcode/terminal_output.rb', line 98

def print(text, color = :default)
  color_params = color_output? ? color : {}
  super(text.colorize(color_params))
end

Print an IO input interaction



40
41
42
43
# File 'lib/xcode/terminal_output.rb', line 40

def print_input message, level=:debug
  return if LEVELS.index(level) > LEVELS.index(@@log_level)
  puts format_lhs("", "", "<") + message, :default
end

Print an IO output interaction



47
48
49
50
# File 'lib/xcode/terminal_output.rb', line 47

def print_output message, level=:debug
  return if LEVELS.index(level) > LEVELS.index(@@log_level)
  puts format_lhs("", "", ">") + message, :default
end


52
53
54
55
# File 'lib/xcode/terminal_output.rb', line 52

def print_system message, level=:debug
  return if LEVELS.index(level) > LEVELS.index(@@log_level)
  puts format_lhs("", "", "!") + message, :green
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/xcode/terminal_output.rb', line 62

def print_task(task, message, level=:info, cr=true)
  return if LEVELS.index(level) > LEVELS.index(@@log_level)

  level_str = ""
  case level
  when :error
    level_str = "ERROR"
    color = :red
  when :warning
    level_str = "WARN"
    color = :yellow
  when :notice
    level_str = "NOTICE"
    color = :green
  when :info
    level_str = ""
    color = :blue
  else
    color = :default
  end

  print format_lhs(task, level_str), color
  print message, (level==:warning or level==:error or level==:notice) ? color : :default

  if block_given?
    yield
  end

  print "\n" if cr
end

#puts(text, color = :default) ⇒ Object



93
94
95
96
# File 'lib/xcode/terminal_output.rb', line 93

def puts(text, color = :default)
  color_params = color_output? ? color : {}
  super(text.colorize(color_params))
end