/
user_interface.rb
331 lines (293 loc) · 10 KB
/
user_interface.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
require 'cocoapods/user_interface/error_report'
module Pod
# Provides support for UI output. It provides support for nested sections of
# information and for a verbose mode.
#
module UserInterface
require 'colored'
@title_colors = %w( yellow green )
@title_level = 0
@indentation_level = 2
@treat_titles_as_messages = false
@warnings = []
class << self
include Config::Mixin
attr_accessor :indentation_level
attr_accessor :title_level
attr_accessor :warnings
# @return [Bool] Whether the wrapping of the strings to the width of the
# terminal should be disabled.
#
attr_accessor :disable_wrap
alias_method :disable_wrap?, :disable_wrap
# 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.
#
# @todo Refactor to title (for always visible titles like search)
# and sections (titles that represent collapsible sections).
#
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
# In verbose mode it shows the sections and the contents.
# In normal mode it just prints the title.
#
# @return [void]
#
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
# A title opposed to a section is always visible
#
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
# def title(title, verbose_prefix = '', relative_indentation = 2)
# end
# Prints a verbose message taking an optional verbose prefix and
# a relative indentation valid for the UI action in the passed
# block.
#
# @todo Clean interface.
#
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
# 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.
#
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
# Prints an important message to the user.
#
# @param [String] message The message to print.
#
# return [void]
#
def notice(message)
puts("\n[!] #{message}".green)
end
# Returns a string containing relative location of a path from the Podfile.
# The returned path is quoted. If the argument is nil it returns the
# empty string.
#
def path(pathname)
if pathname
from_path = config.podfile_path.dirname if config.podfile_path
from_path ||= Pathname.pwd
path = Pathname(pathname).relative_path_from(from_path)
"`#{path}`"
else
''
end
end
# Prints the textual representation of a given set.
#
def pod(set, mode = :normal)
if mode == :name_and_version
puts_indented "#{set.name} #{set.versions.first.version}"
else
pod = Specification::Set::Presenter.new(set)
title = "\n-> #{pod.name} (#{pod.version})"
if pod.spec.deprecated?
title += " #{pod.deprecation_description}"
colored_title = title.red
else
colored_title = title.green
end
title(colored_title, '', 1) do
puts_indented pod.summary if pod.summary
puts_indented "pod '#{pod.name}', '~> #{pod.version}'"
labeled('Homepage', pod.homepage)
labeled('Source', pod.source_url)
labeled('Versions', pod.versions_by_source)
if mode == :stats
labeled('Authors', pod.authors) if pod.authors =~ /,/
labeled('Author', pod.authors) if pod.authors !~ /,/
labeled('License', pod.license)
labeled('Platform', pod.platform)
labeled('Stars', pod.github_stargazers)
labeled('Forks', pod.github_forks)
end
labeled('Subspecs', pod.subspecs)
end
end
end
# Prints a message with a label.
#
def labeled(label, value, justification = 12)
if value
title = "- #{label}:"
if value.is_a?(Array)
lines = [wrap_string(title, self.indentation_level)]
value.each do |v|
lines << wrap_string("- #{v}", self.indentation_level + 2)
end
puts lines.join("\n")
else
puts wrap_string(title.ljust(justification) + "#{value}", self.indentation_level)
end
end
end
# Prints a message respecting the current indentation level and
# wrapping it to the terminal width if necessary.
#
def puts_indented(message = '')
indented = wrap_string(message, self.indentation_level)
puts(indented)
end
# Prints the stored warnings. This method is intended to be called at the
# end of the execution of the binary.
#
# @return [void]
#
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|
string = "- #{action}"
string = wrap_string(string, 4)
puts(string)
end
end
end
public
# @!group Basic methods
#-----------------------------------------------------------------------#
# prints a message followed by a new line unless config is silent.
#
def puts(message = '')
STDOUT.puts(message) unless config.silent?
end
# prints a message followed by a new line unless config is silent.
#
def print(message)
STDOUT.print(message) unless config.silent?
end
# gets input from $stdin
#
def gets
$stdin.gets
end
# Stores important warning to the user optionally followed by actions
# that the user should take. To print them use {#print_warnings}.
#
# @param [String] message The message to print.
# @param [Array] actions The actions that the user should take.
#
# return [void]
#
def warn(message, actions = [], verbose_only = false)
warnings << { :message => message, :actions => actions, :verbose_only => verbose_only }
end
private
# @!group Helpers
#-----------------------------------------------------------------------#
# @return [String] Wraps a string taking into account the width of the
# terminal and an option indent. Adapted from
# http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
#
# @param [String] txt The string to wrap
#
# @param [String] indent The string to use to indent the result.
#
# @return [String] The formatted string.
#
# @note If CocoaPods is not being run in a terminal or the width of the
# terminal is too small a width of 80 is assumed.
#
def wrap_string(string, indent = 0)
if disable_wrap
(' ' * indent) + string
else
first_space = ' ' * indent
indented = CLAide::Command::Banner::TextWrapper.wrap_with_indent(string, indent, 9999)
first_space + indented
end
end
end
end
UI = UserInterface
#---------------------------------------------------------------------------#
# Redirects cocoapods-core UI.
#
module CoreUI
class << self
def puts(message)
UI.puts message
end
def warn(message)
UI.warn message
end
end
end
end
#---------------------------------------------------------------------------#
module Xcodeproj
# Redirects xcodeproj UI.
#
module UserInterface
def self.puts(message)
::Pod::UI.puts message
end
def self.warn(message)
::Pod::UI.warn message
end
end
end