Module: Pod::UserInterface

Extended by:
Config::Mixin
Defined in:
lib/cocoapods/user_interface.rb,
lib/cocoapods/user_interface/error_report.rb

Overview

Provides support for UI output. It provides support for nested sections of information and for a verbose mode.

Defined Under Namespace

Modules: ErrorReport

Class Attribute Summary collapse

Basic methods collapse

Class Method Summary collapse

Methods included from Config::Mixin

config

Class Attribute Details

.disable_wrapBool Also known as: disable_wrap?

Returns Whether the wrapping of the strings to the width of the terminal should be disabled.

Returns:

  • (Bool)

    Whether the wrapping of the strings to the width of the terminal should be disabled.



29
30
31
# File 'lib/cocoapods/user_interface.rb', line 29

def disable_wrap
  @disable_wrap
end

.indentation_levelObject

Returns the value of attribute indentation_level.



22
23
24
# File 'lib/cocoapods/user_interface.rb', line 22

def indentation_level
  @indentation_level
end

.title_levelObject

Returns the value of attribute title_level.



23
24
25
# File 'lib/cocoapods/user_interface.rb', line 23

def title_level
  @title_level
end

.warningsObject

Returns the value of attribute warnings.



24
25
26
# File 'lib/cocoapods/user_interface.rb', line 24

def warnings
  @warnings
end

Class Method Details

.info(message) ⇒ Object

Prints an info to the user. The info is always displayed. It respects the current indentation level only in verbose mode.

Any title printed in the optional block is treated as a message.



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

def info(message)
  indentation = config.verbose? ? self.indentation_level : 0
  indented = wrap_string(message, " " * indentation)
  puts(indented)

  self.indentation_level += 2
  @treat_titles_as_messages = true
  yield if block_given?
  @treat_titles_as_messages = false
  self.indentation_level -= 2
end

.labeled(label, value) ⇒ Object

Prints a message with a label.



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cocoapods/user_interface.rb', line 186

def labeled(label, value)
  if value
    ''.tap do |t|
      t << "    - #{label}:".ljust(16)
      if value.is_a?(Array)
        separator = "\n  - "
        puts_indented t << separator << value.join(separator)
      else
        puts_indented t << value.to_s << "\n"
      end
    end
  end
end

.message(message, verbose_prefix = '', relative_indentation = 2) ⇒ Object

TODO:

Clean interface.

Prints a verbose message taking an optional verbose prefix and a relative indentation valid for the UI action in the passed block.



108
109
110
111
112
113
114
115
# File 'lib/cocoapods/user_interface.rb', line 108

def message(message, verbose_prefix = '', relative_indentation = 2)
  message = verbose_prefix + message if config.verbose?
  puts_indented message if config.verbose?

  self.indentation_level += relative_indentation
  yield if block_given?
  self.indentation_level -= relative_indentation
end

.notice(message) ⇒ Object

Prints an important message to the user.

return [void]

Parameters:

  • message (String)

    The message to print.



141
142
143
# File 'lib/cocoapods/user_interface.rb', line 141

def notice(message)
  puts("\n[!] #{message}".green)
end

.path(pathname) ⇒ Object

Returns a string containing relative location of a path from the Podfile. The returned path is quoted. If the argument is nit it returns the empty string.



149
150
151
152
153
154
155
156
# File 'lib/cocoapods/user_interface.rb', line 149

def path(pathname)
  if pathname
    path = pathname.relative_path_from(config.podfile_path.dirname || Pathname.pwd)
    "`#{path}`"
  else
    ''
  end
end

.pod(set, mode = :normal, statistics_provider = nil) ⇒ Object

Prints the textual representation of a given set.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cocoapods/user_interface.rb', line 160

def pod(set, mode = :normal, statistics_provider = nil)
  if mode == :name
    puts_indented set.name
  else
    pod = Specification::Set::Presenter.new(set, statistics_provider)
    title("\n-> #{pod.name} (#{pod.version})".green, '', 1) do
      puts_indented pod.summary
      labeled('Homepage', pod.homepage)
      labeled('Source',   pod.source_url)
      labeled('Versions', pod.verions_by_source)
      if mode == :stats
        labeled('Pushed',   pod.github_last_activity)
        labeled('Authors',  pod.authors) if pod.authors =~ /,/
        labeled('Author',   pod.authors) if pod.authors !~ /,/
        labeled('License',  pod.license)
        labeled('Platform', pod.platform)
        labeled('Watchers', pod.github_watchers)
        labeled('Forks',    pod.github_forks)
      end
      labeled('Sub specs', pod.subspecs)
    end
  end
end

prints a message followed by a new line unless config is silent.



238
239
240
# File 'lib/cocoapods/user_interface.rb', line 238

def print(message)
  STDOUT.print(message) unless config.silent?
end

This method returns an undefined value.

Prints the stored warnings. This method is intended to be called at the end of the execution of the binary.



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cocoapods/user_interface.rb', line 213

def print_warnings
  STDOUT.flush
  warnings.each do |warning|
    next if warning[:verbose_only] && !config.verbose?
    STDERR.puts("\n[!] #{warning[:message]}".yellow)
    warning[:actions].each do |action|
      indented = wrap_string(action, "    - ")
      puts(indented)
    end
  end
end

.puts(message = '') ⇒ Object

prints a message followed by a new line unless config is silent.



232
233
234
# File 'lib/cocoapods/user_interface.rb', line 232

def puts(message = '')
  STDOUT.puts(message) unless config.silent?
end

.puts_indented(message = '') ⇒ Object

Prints a message respecting the current indentation level and wrapping it to the terminal width if necessary.



203
204
205
206
# File 'lib/cocoapods/user_interface.rb', line 203

def puts_indented(message = '')
  indented = wrap_string(message, " " * self.indentation_level)
  puts(indented)
end

.section(title, verbose_prefix = '', relative_indentation = 0) ⇒ Object

TODO:

Refactor to title (for always visible titles like search) and sections (titles that represent collapsible sections).

Prints a title taking an optional verbose prefix and a relative indentation valid for the UI action in the passed block.

In verbose mode titles are printed with a color according to their level. In normal mode titles are printed only if they have nesting level smaller than 2.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cocoapods/user_interface.rb', line 43

def section(title, verbose_prefix = '', relative_indentation = 0)
  if config.verbose?
    title(title, verbose_prefix, relative_indentation)
  elsif title_level < 1
    puts title
  end

  self.indentation_level += relative_indentation
  self.title_level += 1
  yield if block_given?
  self.indentation_level -= relative_indentation
  self.title_level -= 1
end

.title(title, verbose_prefix = '', relative_indentation = 2) ⇒ Object

A title opposed to a section is always visible



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cocoapods/user_interface.rb', line 80

def title(title, verbose_prefix = '', relative_indentation = 2)
  if(@treat_titles_as_messages)
    message(title, verbose_prefix)
  else
    title = verbose_prefix + title if config.verbose?
    title = "\n#{title}" if @title_level < 2
    if (color = @title_colors[@title_level])
      title = title.send(color)
    end
    puts "#{title}"
  end

  self.indentation_level += relative_indentation
  self.title_level += 1
  yield if block_given?
  self.indentation_level -= relative_indentation
  self.title_level -= 1
end

.titled_section(title, options = {}) ⇒ void

This method returns an undefined value.

In verbose mode it shows the sections and the contents. In normal mode it just prints the title.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cocoapods/user_interface.rb', line 62

def titled_section(title, options = {})
  relative_indentation = options[:relative_indentation] || 0
  verbose_prefix = options[:verbose_prefix] || ''
  if config.verbose?
    title(title, verbose_prefix, relative_indentation)
  else
    puts title
  end

  self.indentation_level += relative_indentation
  self.title_level += 1
  yield if block_given?
  self.indentation_level -= relative_indentation
  self.title_level -= 1
end

.warn(message, actions = [], verbose_only = false) ⇒ Object

Stores important warning to the user optionally followed by actions that the user should take. To print them use #print_warnings.

return [void]

Parameters:

  • message (String)

    The message to print.

  • actions (Array) (defaults to: [])

    The actions that the user should take.



250
251
252
# File 'lib/cocoapods/user_interface.rb', line 250

def warn(message, actions = [], verbose_only = false)
  warnings << { :message => message, :actions => actions, :verbose_only => verbose_only }
end