honeybadger 5.0.2 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +713 -701
  3. data/LICENSE +19 -19
  4. data/README.md +57 -57
  5. data/TROUBLESHOOTING.md +3 -3
  6. data/bin/honeybadger +5 -5
  7. data/lib/honeybadger/agent.rb +488 -488
  8. data/lib/honeybadger/backend/base.rb +116 -116
  9. data/lib/honeybadger/backend/debug.rb +22 -22
  10. data/lib/honeybadger/backend/null.rb +29 -29
  11. data/lib/honeybadger/backend/server.rb +62 -62
  12. data/lib/honeybadger/backend/test.rb +46 -46
  13. data/lib/honeybadger/backend.rb +27 -27
  14. data/lib/honeybadger/backtrace.rb +181 -181
  15. data/lib/honeybadger/breadcrumbs/active_support.rb +119 -119
  16. data/lib/honeybadger/breadcrumbs/breadcrumb.rb +53 -53
  17. data/lib/honeybadger/breadcrumbs/collector.rb +82 -82
  18. data/lib/honeybadger/breadcrumbs/logging.rb +51 -51
  19. data/lib/honeybadger/breadcrumbs/ring_buffer.rb +44 -44
  20. data/lib/honeybadger/breadcrumbs.rb +8 -8
  21. data/lib/honeybadger/cli/deploy.rb +43 -43
  22. data/lib/honeybadger/cli/exec.rb +143 -143
  23. data/lib/honeybadger/cli/helpers.rb +28 -28
  24. data/lib/honeybadger/cli/heroku.rb +129 -129
  25. data/lib/honeybadger/cli/install.rb +101 -101
  26. data/lib/honeybadger/cli/main.rb +237 -237
  27. data/lib/honeybadger/cli/notify.rb +67 -67
  28. data/lib/honeybadger/cli/test.rb +267 -267
  29. data/lib/honeybadger/cli.rb +14 -14
  30. data/lib/honeybadger/config/defaults.rb +336 -333
  31. data/lib/honeybadger/config/env.rb +42 -42
  32. data/lib/honeybadger/config/ruby.rb +146 -146
  33. data/lib/honeybadger/config/yaml.rb +76 -76
  34. data/lib/honeybadger/config.rb +413 -413
  35. data/lib/honeybadger/const.rb +20 -20
  36. data/lib/honeybadger/context_manager.rb +55 -55
  37. data/lib/honeybadger/conversions.rb +16 -16
  38. data/lib/honeybadger/init/rails.rb +38 -38
  39. data/lib/honeybadger/init/rake.rb +66 -66
  40. data/lib/honeybadger/init/ruby.rb +11 -11
  41. data/lib/honeybadger/init/sinatra.rb +51 -51
  42. data/lib/honeybadger/logging.rb +177 -177
  43. data/lib/honeybadger/notice.rb +579 -568
  44. data/lib/honeybadger/plugin.rb +210 -210
  45. data/lib/honeybadger/plugins/breadcrumbs.rb +111 -111
  46. data/lib/honeybadger/plugins/delayed_job/plugin.rb +56 -56
  47. data/lib/honeybadger/plugins/delayed_job.rb +22 -22
  48. data/lib/honeybadger/plugins/faktory.rb +52 -52
  49. data/lib/honeybadger/plugins/lambda.rb +71 -71
  50. data/lib/honeybadger/plugins/local_variables.rb +44 -44
  51. data/lib/honeybadger/plugins/passenger.rb +23 -23
  52. data/lib/honeybadger/plugins/rails.rb +72 -63
  53. data/lib/honeybadger/plugins/resque.rb +72 -72
  54. data/lib/honeybadger/plugins/shoryuken.rb +52 -52
  55. data/lib/honeybadger/plugins/sidekiq.rb +71 -62
  56. data/lib/honeybadger/plugins/sucker_punch.rb +18 -18
  57. data/lib/honeybadger/plugins/thor.rb +32 -32
  58. data/lib/honeybadger/plugins/warden.rb +19 -19
  59. data/lib/honeybadger/rack/error_notifier.rb +92 -92
  60. data/lib/honeybadger/rack/user_feedback.rb +88 -88
  61. data/lib/honeybadger/rack/user_informer.rb +45 -45
  62. data/lib/honeybadger/ruby.rb +2 -2
  63. data/lib/honeybadger/singleton.rb +103 -103
  64. data/lib/honeybadger/tasks.rb +22 -22
  65. data/lib/honeybadger/templates/feedback_form.erb +84 -84
  66. data/lib/honeybadger/util/http.rb +92 -92
  67. data/lib/honeybadger/util/lambda.rb +32 -32
  68. data/lib/honeybadger/util/request_hash.rb +73 -73
  69. data/lib/honeybadger/util/request_payload.rb +41 -41
  70. data/lib/honeybadger/util/revision.rb +39 -39
  71. data/lib/honeybadger/util/sanitizer.rb +214 -214
  72. data/lib/honeybadger/util/sql.rb +34 -34
  73. data/lib/honeybadger/util/stats.rb +50 -50
  74. data/lib/honeybadger/version.rb +4 -4
  75. data/lib/honeybadger/worker.rb +253 -253
  76. data/lib/honeybadger.rb +11 -11
  77. data/resources/ca-bundle.crt +3376 -3376
  78. data/vendor/capistrano-honeybadger/lib/capistrano/honeybadger.rb +5 -5
  79. data/vendor/capistrano-honeybadger/lib/capistrano/tasks/deploy.cap +89 -89
  80. data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano/legacy.rb +47 -47
  81. data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano.rb +2 -2
  82. data/vendor/cli/inifile.rb +628 -628
  83. data/vendor/cli/thor/actions/create_file.rb +103 -103
  84. data/vendor/cli/thor/actions/create_link.rb +59 -59
  85. data/vendor/cli/thor/actions/directory.rb +118 -118
  86. data/vendor/cli/thor/actions/empty_directory.rb +135 -135
  87. data/vendor/cli/thor/actions/file_manipulation.rb +316 -316
  88. data/vendor/cli/thor/actions/inject_into_file.rb +107 -107
  89. data/vendor/cli/thor/actions.rb +319 -319
  90. data/vendor/cli/thor/base.rb +656 -656
  91. data/vendor/cli/thor/command.rb +133 -133
  92. data/vendor/cli/thor/core_ext/hash_with_indifferent_access.rb +77 -77
  93. data/vendor/cli/thor/core_ext/io_binary_read.rb +10 -10
  94. data/vendor/cli/thor/core_ext/ordered_hash.rb +98 -98
  95. data/vendor/cli/thor/error.rb +32 -32
  96. data/vendor/cli/thor/group.rb +281 -281
  97. data/vendor/cli/thor/invocation.rb +178 -178
  98. data/vendor/cli/thor/line_editor/basic.rb +35 -35
  99. data/vendor/cli/thor/line_editor/readline.rb +88 -88
  100. data/vendor/cli/thor/line_editor.rb +17 -17
  101. data/vendor/cli/thor/parser/argument.rb +73 -73
  102. data/vendor/cli/thor/parser/arguments.rb +175 -175
  103. data/vendor/cli/thor/parser/option.rb +125 -125
  104. data/vendor/cli/thor/parser/options.rb +218 -218
  105. data/vendor/cli/thor/parser.rb +4 -4
  106. data/vendor/cli/thor/rake_compat.rb +71 -71
  107. data/vendor/cli/thor/runner.rb +322 -322
  108. data/vendor/cli/thor/shell/basic.rb +421 -421
  109. data/vendor/cli/thor/shell/color.rb +149 -149
  110. data/vendor/cli/thor/shell/html.rb +126 -126
  111. data/vendor/cli/thor/shell.rb +81 -81
  112. data/vendor/cli/thor/util.rb +267 -267
  113. data/vendor/cli/thor/version.rb +3 -3
  114. data/vendor/cli/thor.rb +484 -484
  115. metadata +10 -5
@@ -1,421 +1,421 @@
1
- require "tempfile"
2
- require "io/console" if RUBY_VERSION > "1.9.2"
3
-
4
- class Thor
5
- module Shell
6
- class Basic # rubocop:disable ClassLength
7
- attr_accessor :base
8
- attr_reader :padding
9
-
10
- # Initialize base, mute and padding to nil.
11
- #
12
- def initialize #:nodoc:
13
- @base, @mute, @padding, @always_force = nil, false, 0, false
14
- end
15
-
16
- # Mute everything that's inside given block
17
- #
18
- def mute
19
- @mute = true
20
- yield
21
- ensure
22
- @mute = false
23
- end
24
-
25
- # Check if base is muted
26
- #
27
- def mute? # rubocop:disable TrivialAccessors
28
- @mute
29
- end
30
-
31
- # Sets the output padding, not allowing less than zero values.
32
- #
33
- def padding=(value)
34
- @padding = [0, value].max
35
- end
36
-
37
- # Asks something to the user and receives a response.
38
- #
39
- # If asked to limit the correct responses, you can pass in an
40
- # array of acceptable answers. If one of those is not supplied,
41
- # they will be shown a message stating that one of those answers
42
- # must be given and re-asked the question.
43
- #
44
- # If asking for sensitive information, the :echo option can be set
45
- # to false to mask user input from $stdin.
46
- #
47
- # If the required input is a path, then set the path option to
48
- # true. This will enable tab completion for file paths relative
49
- # to the current working directory on systems that support
50
- # Readline.
51
- #
52
- # ==== Example
53
- # ask("What is your name?")
54
- #
55
- # ask("What is your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])
56
- #
57
- # ask("What is your password?", :echo => false)
58
- #
59
- # ask("Where should the file be saved?", :path => true)
60
- #
61
- def ask(statement, *args)
62
- options = args.last.is_a?(Hash) ? args.pop : {}
63
- color = args.first
64
-
65
- if options[:limited_to]
66
- ask_filtered(statement, color, options)
67
- else
68
- ask_simply(statement, color, options)
69
- end
70
- end
71
-
72
- # Say (print) something to the user. If the sentence ends with a whitespace
73
- # or tab character, a new line is not appended (print + flush). Otherwise
74
- # are passed straight to puts (behavior got from Highline).
75
- #
76
- # ==== Example
77
- # say("I know you knew that.")
78
- #
79
- def say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/))
80
- buffer = prepare_message(message, *color)
81
- buffer << "\n" if force_new_line && !message.to_s.end_with?("\n")
82
-
83
- stdout.print(buffer)
84
- stdout.flush
85
- end
86
-
87
- # Say a status with the given color and appends the message. Since this
88
- # method is used frequently by actions, it allows nil or false to be given
89
- # in log_status, avoiding the message from being shown. If a Symbol is
90
- # given in log_status, it's used as the color.
91
- #
92
- def say_status(status, message, log_status = true)
93
- return if quiet? || log_status == false
94
- spaces = " " * (padding + 1)
95
- color = log_status.is_a?(Symbol) ? log_status : :green
96
-
97
- status = status.to_s.rjust(12)
98
- status = set_color status, color, true if color
99
-
100
- buffer = "#{status}#{spaces}#{message}"
101
- buffer << "\n" unless buffer.end_with?("\n")
102
-
103
- stdout.print(buffer)
104
- stdout.flush
105
- end
106
-
107
- # Make a question the to user and returns true if the user replies "y" or
108
- # "yes".
109
- #
110
- def yes?(statement, color = nil)
111
- !!(ask(statement, color, :add_to_history => false) =~ is?(:yes))
112
- end
113
-
114
- # Make a question the to user and returns true if the user replies "n" or
115
- # "no".
116
- #
117
- def no?(statement, color = nil)
118
- !!(ask(statement, color, :add_to_history => false) =~ is?(:no))
119
- end
120
-
121
- # Prints values in columns
122
- #
123
- # ==== Parameters
124
- # Array[String, String, ...]
125
- #
126
- def print_in_columns(array)
127
- return if array.empty?
128
- colwidth = (array.map { |el| el.to_s.size }.max || 0) + 2
129
- array.each_with_index do |value, index|
130
- # Don't output trailing spaces when printing the last column
131
- if ((((index + 1) % (terminal_width / colwidth))).zero? && !index.zero?) || index + 1 == array.length
132
- stdout.puts value
133
- else
134
- stdout.printf("%-#{colwidth}s", value)
135
- end
136
- end
137
- end
138
-
139
- # Prints a table.
140
- #
141
- # ==== Parameters
142
- # Array[Array[String, String, ...]]
143
- #
144
- # ==== Options
145
- # indent<Integer>:: Indent the first column by indent value.
146
- # colwidth<Integer>:: Force the first column to colwidth spaces wide.
147
- #
148
- def print_table(array, options = {}) # rubocop:disable MethodLength
149
- return if array.empty?
150
-
151
- formats, indent, colwidth = [], options[:indent].to_i, options[:colwidth]
152
- options[:truncate] = terminal_width if options[:truncate] == true
153
-
154
- formats << "%-#{colwidth + 2}s" if colwidth
155
- start = colwidth ? 1 : 0
156
-
157
- colcount = array.max { |a, b| a.size <=> b.size }.size
158
-
159
- maximas = []
160
-
161
- start.upto(colcount - 1) do |index|
162
- maxima = array.map { |row| row[index] ? row[index].to_s.size : 0 }.max
163
- maximas << maxima
164
- if index == colcount - 1
165
- # Don't output 2 trailing spaces when printing the last column
166
- formats << "%-s"
167
- else
168
- formats << "%-#{maxima + 2}s"
169
- end
170
- end
171
-
172
- formats[0] = formats[0].insert(0, " " * indent)
173
- formats << "%s"
174
-
175
- array.each do |row|
176
- sentence = ""
177
-
178
- row.each_with_index do |column, index|
179
- maxima = maximas[index]
180
-
181
- if column.is_a?(Numeric)
182
- if index == row.size - 1
183
- # Don't output 2 trailing spaces when printing the last column
184
- f = "%#{maxima}s"
185
- else
186
- f = "%#{maxima}s "
187
- end
188
- else
189
- f = formats[index]
190
- end
191
- sentence << f % column.to_s
192
- end
193
-
194
- sentence = truncate(sentence, options[:truncate]) if options[:truncate]
195
- stdout.puts sentence
196
- end
197
- end
198
-
199
- # Prints a long string, word-wrapping the text to the current width of the
200
- # terminal display. Ideal for printing heredocs.
201
- #
202
- # ==== Parameters
203
- # String
204
- #
205
- # ==== Options
206
- # indent<Integer>:: Indent each line of the printed paragraph by indent value.
207
- #
208
- def print_wrapped(message, options = {})
209
- indent = options[:indent] || 0
210
- width = terminal_width - indent
211
- paras = message.split("\n\n")
212
-
213
- paras.map! do |unwrapped|
214
- unwrapped.strip.gsub(/\n/, " ").squeeze(" ").gsub(/.{1,#{width}}(?:\s|\Z)/) { ($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n") }
215
- end
216
-
217
- paras.each do |para|
218
- para.split("\n").each do |line|
219
- stdout.puts line.insert(0, " " * indent)
220
- end
221
- stdout.puts unless para == paras.last
222
- end
223
- end
224
-
225
- # Deals with file collision and returns true if the file should be
226
- # overwritten and false otherwise. If a block is given, it uses the block
227
- # response as the content for the diff.
228
- #
229
- # ==== Parameters
230
- # destination<String>:: the destination file to solve conflicts
231
- # block<Proc>:: an optional block that returns the value to be used in diff
232
- #
233
- def file_collision(destination) # rubocop:disable MethodLength
234
- return true if @always_force
235
- options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
236
-
237
- loop do
238
- answer = ask(
239
- %[Overwrite #{destination}? (enter "h" for help) #{options}],
240
- :add_to_history => false
241
- )
242
-
243
- case answer
244
- when is?(:yes), is?(:force), ""
245
- return true
246
- when is?(:no), is?(:skip)
247
- return false
248
- when is?(:always)
249
- return @always_force = true
250
- when is?(:quit)
251
- say "Aborting..."
252
- fail SystemExit
253
- when is?(:diff)
254
- show_diff(destination, yield) if block_given?
255
- say "Retrying..."
256
- else
257
- say file_collision_help
258
- end
259
- end
260
- end
261
-
262
- # This code was copied from Rake, available under MIT-LICENSE
263
- # Copyright (c) 2003, 2004 Jim Weirich
264
- def terminal_width
265
- if ENV["THOR_COLUMNS"]
266
- result = ENV["THOR_COLUMNS"].to_i
267
- else
268
- result = unix? ? dynamic_width : 80
269
- end
270
- result < 10 ? 80 : result
271
- rescue
272
- 80
273
- end
274
-
275
- # Called if something goes wrong during the execution. This is used by Thor
276
- # internally and should not be used inside your scripts. If something went
277
- # wrong, you can always raise an exception. If you raise a Thor::Error, it
278
- # will be rescued and wrapped in the method below.
279
- #
280
- def error(statement)
281
- stderr.puts statement
282
- end
283
-
284
- # Apply color to the given string with optional bold. Disabled in the
285
- # Thor::Shell::Basic class.
286
- #
287
- def set_color(string, *args) #:nodoc:
288
- string
289
- end
290
-
291
- protected
292
-
293
- def prepare_message(message, *color)
294
- spaces = " " * padding
295
- spaces + set_color(message.to_s, *color)
296
- end
297
-
298
- def can_display_colors?
299
- false
300
- end
301
-
302
- def lookup_color(color)
303
- return color unless color.is_a?(Symbol)
304
- self.class.const_get(color.to_s.upcase)
305
- end
306
-
307
- def stdout
308
- $stdout
309
- end
310
-
311
- def stderr
312
- $stderr
313
- end
314
-
315
- def is?(value) #:nodoc:
316
- value = value.to_s
317
-
318
- if value.size == 1
319
- /\A#{value}\z/i
320
- else
321
- /\A(#{value}|#{value[0, 1]})\z/i
322
- end
323
- end
324
-
325
- def file_collision_help #:nodoc:
326
- <<-HELP
327
- Y - yes, overwrite
328
- n - no, do not overwrite
329
- a - all, overwrite this and all others
330
- q - quit, abort
331
- d - diff, show the differences between the old and the new
332
- h - help, show this help
333
- HELP
334
- end
335
-
336
- def show_diff(destination, content) #:nodoc:
337
- diff_cmd = ENV["THOR_DIFF"] || ENV["RAILS_DIFF"] || "diff -u"
338
-
339
- Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
340
- temp.write content
341
- temp.rewind
342
- system %(#{diff_cmd} "#{destination}" "#{temp.path}")
343
- end
344
- end
345
-
346
- def quiet? #:nodoc:
347
- mute? || (base && base.options[:quiet])
348
- end
349
-
350
- # Calculate the dynamic width of the terminal
351
- def dynamic_width
352
- @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
353
- end
354
-
355
- def dynamic_width_stty
356
- %x(stty size 2>/dev/null).split[1].to_i
357
- end
358
-
359
- def dynamic_width_tput
360
- %x(tput cols 2>/dev/null).to_i
361
- end
362
-
363
- def unix?
364
- RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
365
- end
366
-
367
- def truncate(string, width)
368
- as_unicode do
369
- chars = string.chars.to_a
370
- if chars.length <= width
371
- chars.join
372
- else
373
- ( chars[0, width - 3].join) + "..."
374
- end
375
- end
376
- end
377
-
378
- if "".respond_to?(:encode)
379
- def as_unicode
380
- yield
381
- end
382
- else
383
- def as_unicode
384
- old, $KCODE = $KCODE, "U"
385
- yield
386
- ensure
387
- $KCODE = old
388
- end
389
- end
390
-
391
- def ask_simply(statement, color, options)
392
- default = options[:default]
393
- message = [statement, ("(#{default})" if default), nil].uniq.join(" ")
394
- message = prepare_message(message, color)
395
- result = Thor::LineEditor.readline(message, options)
396
-
397
- return unless result
398
-
399
- result.strip!
400
-
401
- if default && result == ""
402
- default
403
- else
404
- result
405
- end
406
- end
407
-
408
- def ask_filtered(statement, color, options)
409
- answer_set = options[:limited_to]
410
- correct_answer = nil
411
- until correct_answer
412
- answers = answer_set.join(", ")
413
- answer = ask_simply("#{statement} [#{answers}]", color, options)
414
- correct_answer = answer_set.include?(answer) ? answer : nil
415
- say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
416
- end
417
- correct_answer
418
- end
419
- end
420
- end
421
- end
1
+ require "tempfile"
2
+ require "io/console" if RUBY_VERSION > "1.9.2"
3
+
4
+ class Thor
5
+ module Shell
6
+ class Basic # rubocop:disable ClassLength
7
+ attr_accessor :base
8
+ attr_reader :padding
9
+
10
+ # Initialize base, mute and padding to nil.
11
+ #
12
+ def initialize #:nodoc:
13
+ @base, @mute, @padding, @always_force = nil, false, 0, false
14
+ end
15
+
16
+ # Mute everything that's inside given block
17
+ #
18
+ def mute
19
+ @mute = true
20
+ yield
21
+ ensure
22
+ @mute = false
23
+ end
24
+
25
+ # Check if base is muted
26
+ #
27
+ def mute? # rubocop:disable TrivialAccessors
28
+ @mute
29
+ end
30
+
31
+ # Sets the output padding, not allowing less than zero values.
32
+ #
33
+ def padding=(value)
34
+ @padding = [0, value].max
35
+ end
36
+
37
+ # Asks something to the user and receives a response.
38
+ #
39
+ # If asked to limit the correct responses, you can pass in an
40
+ # array of acceptable answers. If one of those is not supplied,
41
+ # they will be shown a message stating that one of those answers
42
+ # must be given and re-asked the question.
43
+ #
44
+ # If asking for sensitive information, the :echo option can be set
45
+ # to false to mask user input from $stdin.
46
+ #
47
+ # If the required input is a path, then set the path option to
48
+ # true. This will enable tab completion for file paths relative
49
+ # to the current working directory on systems that support
50
+ # Readline.
51
+ #
52
+ # ==== Example
53
+ # ask("What is your name?")
54
+ #
55
+ # ask("What is your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])
56
+ #
57
+ # ask("What is your password?", :echo => false)
58
+ #
59
+ # ask("Where should the file be saved?", :path => true)
60
+ #
61
+ def ask(statement, *args)
62
+ options = args.last.is_a?(Hash) ? args.pop : {}
63
+ color = args.first
64
+
65
+ if options[:limited_to]
66
+ ask_filtered(statement, color, options)
67
+ else
68
+ ask_simply(statement, color, options)
69
+ end
70
+ end
71
+
72
+ # Say (print) something to the user. If the sentence ends with a whitespace
73
+ # or tab character, a new line is not appended (print + flush). Otherwise
74
+ # are passed straight to puts (behavior got from Highline).
75
+ #
76
+ # ==== Example
77
+ # say("I know you knew that.")
78
+ #
79
+ def say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/))
80
+ buffer = prepare_message(message, *color)
81
+ buffer << "\n" if force_new_line && !message.to_s.end_with?("\n")
82
+
83
+ stdout.print(buffer)
84
+ stdout.flush
85
+ end
86
+
87
+ # Say a status with the given color and appends the message. Since this
88
+ # method is used frequently by actions, it allows nil or false to be given
89
+ # in log_status, avoiding the message from being shown. If a Symbol is
90
+ # given in log_status, it's used as the color.
91
+ #
92
+ def say_status(status, message, log_status = true)
93
+ return if quiet? || log_status == false
94
+ spaces = " " * (padding + 1)
95
+ color = log_status.is_a?(Symbol) ? log_status : :green
96
+
97
+ status = status.to_s.rjust(12)
98
+ status = set_color status, color, true if color
99
+
100
+ buffer = "#{status}#{spaces}#{message}"
101
+ buffer << "\n" unless buffer.end_with?("\n")
102
+
103
+ stdout.print(buffer)
104
+ stdout.flush
105
+ end
106
+
107
+ # Make a question the to user and returns true if the user replies "y" or
108
+ # "yes".
109
+ #
110
+ def yes?(statement, color = nil)
111
+ !!(ask(statement, color, :add_to_history => false) =~ is?(:yes))
112
+ end
113
+
114
+ # Make a question the to user and returns true if the user replies "n" or
115
+ # "no".
116
+ #
117
+ def no?(statement, color = nil)
118
+ !!(ask(statement, color, :add_to_history => false) =~ is?(:no))
119
+ end
120
+
121
+ # Prints values in columns
122
+ #
123
+ # ==== Parameters
124
+ # Array[String, String, ...]
125
+ #
126
+ def print_in_columns(array)
127
+ return if array.empty?
128
+ colwidth = (array.map { |el| el.to_s.size }.max || 0) + 2
129
+ array.each_with_index do |value, index|
130
+ # Don't output trailing spaces when printing the last column
131
+ if ((((index + 1) % (terminal_width / colwidth))).zero? && !index.zero?) || index + 1 == array.length
132
+ stdout.puts value
133
+ else
134
+ stdout.printf("%-#{colwidth}s", value)
135
+ end
136
+ end
137
+ end
138
+
139
+ # Prints a table.
140
+ #
141
+ # ==== Parameters
142
+ # Array[Array[String, String, ...]]
143
+ #
144
+ # ==== Options
145
+ # indent<Integer>:: Indent the first column by indent value.
146
+ # colwidth<Integer>:: Force the first column to colwidth spaces wide.
147
+ #
148
+ def print_table(array, options = {}) # rubocop:disable MethodLength
149
+ return if array.empty?
150
+
151
+ formats, indent, colwidth = [], options[:indent].to_i, options[:colwidth]
152
+ options[:truncate] = terminal_width if options[:truncate] == true
153
+
154
+ formats << "%-#{colwidth + 2}s" if colwidth
155
+ start = colwidth ? 1 : 0
156
+
157
+ colcount = array.max { |a, b| a.size <=> b.size }.size
158
+
159
+ maximas = []
160
+
161
+ start.upto(colcount - 1) do |index|
162
+ maxima = array.map { |row| row[index] ? row[index].to_s.size : 0 }.max
163
+ maximas << maxima
164
+ if index == colcount - 1
165
+ # Don't output 2 trailing spaces when printing the last column
166
+ formats << "%-s"
167
+ else
168
+ formats << "%-#{maxima + 2}s"
169
+ end
170
+ end
171
+
172
+ formats[0] = formats[0].insert(0, " " * indent)
173
+ formats << "%s"
174
+
175
+ array.each do |row|
176
+ sentence = ""
177
+
178
+ row.each_with_index do |column, index|
179
+ maxima = maximas[index]
180
+
181
+ if column.is_a?(Numeric)
182
+ if index == row.size - 1
183
+ # Don't output 2 trailing spaces when printing the last column
184
+ f = "%#{maxima}s"
185
+ else
186
+ f = "%#{maxima}s "
187
+ end
188
+ else
189
+ f = formats[index]
190
+ end
191
+ sentence << f % column.to_s
192
+ end
193
+
194
+ sentence = truncate(sentence, options[:truncate]) if options[:truncate]
195
+ stdout.puts sentence
196
+ end
197
+ end
198
+
199
+ # Prints a long string, word-wrapping the text to the current width of the
200
+ # terminal display. Ideal for printing heredocs.
201
+ #
202
+ # ==== Parameters
203
+ # String
204
+ #
205
+ # ==== Options
206
+ # indent<Integer>:: Indent each line of the printed paragraph by indent value.
207
+ #
208
+ def print_wrapped(message, options = {})
209
+ indent = options[:indent] || 0
210
+ width = terminal_width - indent
211
+ paras = message.split("\n\n")
212
+
213
+ paras.map! do |unwrapped|
214
+ unwrapped.strip.gsub(/\n/, " ").squeeze(" ").gsub(/.{1,#{width}}(?:\s|\Z)/) { ($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n") }
215
+ end
216
+
217
+ paras.each do |para|
218
+ para.split("\n").each do |line|
219
+ stdout.puts line.insert(0, " " * indent)
220
+ end
221
+ stdout.puts unless para == paras.last
222
+ end
223
+ end
224
+
225
+ # Deals with file collision and returns true if the file should be
226
+ # overwritten and false otherwise. If a block is given, it uses the block
227
+ # response as the content for the diff.
228
+ #
229
+ # ==== Parameters
230
+ # destination<String>:: the destination file to solve conflicts
231
+ # block<Proc>:: an optional block that returns the value to be used in diff
232
+ #
233
+ def file_collision(destination) # rubocop:disable MethodLength
234
+ return true if @always_force
235
+ options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
236
+
237
+ loop do
238
+ answer = ask(
239
+ %[Overwrite #{destination}? (enter "h" for help) #{options}],
240
+ :add_to_history => false
241
+ )
242
+
243
+ case answer
244
+ when is?(:yes), is?(:force), ""
245
+ return true
246
+ when is?(:no), is?(:skip)
247
+ return false
248
+ when is?(:always)
249
+ return @always_force = true
250
+ when is?(:quit)
251
+ say "Aborting..."
252
+ fail SystemExit
253
+ when is?(:diff)
254
+ show_diff(destination, yield) if block_given?
255
+ say "Retrying..."
256
+ else
257
+ say file_collision_help
258
+ end
259
+ end
260
+ end
261
+
262
+ # This code was copied from Rake, available under MIT-LICENSE
263
+ # Copyright (c) 2003, 2004 Jim Weirich
264
+ def terminal_width
265
+ if ENV["THOR_COLUMNS"]
266
+ result = ENV["THOR_COLUMNS"].to_i
267
+ else
268
+ result = unix? ? dynamic_width : 80
269
+ end
270
+ result < 10 ? 80 : result
271
+ rescue
272
+ 80
273
+ end
274
+
275
+ # Called if something goes wrong during the execution. This is used by Thor
276
+ # internally and should not be used inside your scripts. If something went
277
+ # wrong, you can always raise an exception. If you raise a Thor::Error, it
278
+ # will be rescued and wrapped in the method below.
279
+ #
280
+ def error(statement)
281
+ stderr.puts statement
282
+ end
283
+
284
+ # Apply color to the given string with optional bold. Disabled in the
285
+ # Thor::Shell::Basic class.
286
+ #
287
+ def set_color(string, *args) #:nodoc:
288
+ string
289
+ end
290
+
291
+ protected
292
+
293
+ def prepare_message(message, *color)
294
+ spaces = " " * padding
295
+ spaces + set_color(message.to_s, *color)
296
+ end
297
+
298
+ def can_display_colors?
299
+ false
300
+ end
301
+
302
+ def lookup_color(color)
303
+ return color unless color.is_a?(Symbol)
304
+ self.class.const_get(color.to_s.upcase)
305
+ end
306
+
307
+ def stdout
308
+ $stdout
309
+ end
310
+
311
+ def stderr
312
+ $stderr
313
+ end
314
+
315
+ def is?(value) #:nodoc:
316
+ value = value.to_s
317
+
318
+ if value.size == 1
319
+ /\A#{value}\z/i
320
+ else
321
+ /\A(#{value}|#{value[0, 1]})\z/i
322
+ end
323
+ end
324
+
325
+ def file_collision_help #:nodoc:
326
+ <<-HELP
327
+ Y - yes, overwrite
328
+ n - no, do not overwrite
329
+ a - all, overwrite this and all others
330
+ q - quit, abort
331
+ d - diff, show the differences between the old and the new
332
+ h - help, show this help
333
+ HELP
334
+ end
335
+
336
+ def show_diff(destination, content) #:nodoc:
337
+ diff_cmd = ENV["THOR_DIFF"] || ENV["RAILS_DIFF"] || "diff -u"
338
+
339
+ Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
340
+ temp.write content
341
+ temp.rewind
342
+ system %(#{diff_cmd} "#{destination}" "#{temp.path}")
343
+ end
344
+ end
345
+
346
+ def quiet? #:nodoc:
347
+ mute? || (base && base.options[:quiet])
348
+ end
349
+
350
+ # Calculate the dynamic width of the terminal
351
+ def dynamic_width
352
+ @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
353
+ end
354
+
355
+ def dynamic_width_stty
356
+ %x(stty size 2>/dev/null).split[1].to_i
357
+ end
358
+
359
+ def dynamic_width_tput
360
+ %x(tput cols 2>/dev/null).to_i
361
+ end
362
+
363
+ def unix?
364
+ RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
365
+ end
366
+
367
+ def truncate(string, width)
368
+ as_unicode do
369
+ chars = string.chars.to_a
370
+ if chars.length <= width
371
+ chars.join
372
+ else
373
+ ( chars[0, width - 3].join) + "..."
374
+ end
375
+ end
376
+ end
377
+
378
+ if "".respond_to?(:encode)
379
+ def as_unicode
380
+ yield
381
+ end
382
+ else
383
+ def as_unicode
384
+ old, $KCODE = $KCODE, "U"
385
+ yield
386
+ ensure
387
+ $KCODE = old
388
+ end
389
+ end
390
+
391
+ def ask_simply(statement, color, options)
392
+ default = options[:default]
393
+ message = [statement, ("(#{default})" if default), nil].uniq.join(" ")
394
+ message = prepare_message(message, color)
395
+ result = Thor::LineEditor.readline(message, options)
396
+
397
+ return unless result
398
+
399
+ result.strip!
400
+
401
+ if default && result == ""
402
+ default
403
+ else
404
+ result
405
+ end
406
+ end
407
+
408
+ def ask_filtered(statement, color, options)
409
+ answer_set = options[:limited_to]
410
+ correct_answer = nil
411
+ until correct_answer
412
+ answers = answer_set.join(", ")
413
+ answer = ask_simply("#{statement} [#{answers}]", color, options)
414
+ correct_answer = answer_set.include?(answer) ? answer : nil
415
+ say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
416
+ end
417
+ correct_answer
418
+ end
419
+ end
420
+ end
421
+ end