1
+ require_relative "column_printer"
2
+ require_relative "table_printer"
3
+ require_relative "wrapped_printer"
4
+
1
5
class Thor
2
6
module Shell
3
7
class Basic
4
- DEFAULT_TERMINAL_WIDTH = 80
5
-
6
8
attr_accessor :base
7
9
attr_reader :padding
8
10
@@ -161,16 +163,8 @@ def no?(statement, color = nil)
161
163
# Array[String, String, ...]
162
164
#
163
165
def print_in_columns ( array )
164
- return if array . empty?
165
- colwidth = ( array . map { |el | el . to_s . size } . max || 0 ) + 2
166
- array . each_with_index do |value , index |
167
- # Don't output trailing spaces when printing the last column
168
- if ( ( ( ( index + 1 ) % ( terminal_width / colwidth ) ) ) . zero? && !index . zero? ) || index + 1 == array . length
169
- stdout . puts value
170
- else
171
- stdout . printf ( "%-#{ colwidth } s" , value )
172
- end
173
- end
166
+ printer = ColumnPrinter . new ( stdout )
167
+ printer . print ( array )
174
168
end
175
169
176
170
# Prints a table.
@@ -183,56 +177,8 @@ def print_in_columns(array)
183
177
# colwidth<Integer>:: Force the first column to colwidth spaces wide.
184
178
#
185
179
def print_table ( array , options = { } ) # rubocop:disable Metrics/MethodLength
186
- return if array . empty?
187
-
188
- formats = [ ]
189
- indent = options [ :indent ] . to_i
190
- colwidth = options [ :colwidth ]
191
- options [ :truncate ] = terminal_width if options [ :truncate ] == true
192
-
193
- formats << "%-#{ colwidth + 2 } s" . dup if colwidth
194
- start = colwidth ? 1 : 0
195
-
196
- colcount = array . max { |a , b | a . size <=> b . size } . size
197
-
198
- maximas = [ ]
199
-
200
- start . upto ( colcount - 1 ) do |index |
201
- maxima = array . map { |row | row [ index ] ? row [ index ] . to_s . size : 0 } . max
202
- maximas << maxima
203
- formats << if index == colcount - 1
204
- # Don't output 2 trailing spaces when printing the last column
205
- "%-s" . dup
206
- else
207
- "%-#{ maxima + 2 } s" . dup
208
- end
209
- end
210
-
211
- formats [ 0 ] = formats [ 0 ] . insert ( 0 , " " * indent )
212
- formats << "%s"
213
-
214
- array . each do |row |
215
- sentence = "" . dup
216
-
217
- row . each_with_index do |column , index |
218
- maxima = maximas [ index ]
219
-
220
- f = if column . is_a? ( Numeric )
221
- if index == row . size - 1
222
- # Don't output 2 trailing spaces when printing the last column
223
- "%#{ maxima } s"
224
- else
225
- "%#{ maxima } s "
226
- end
227
- else
228
- formats [ index ]
229
- end
230
- sentence << f % column . to_s
231
- end
232
-
233
- sentence = truncate ( sentence , options [ :truncate ] ) if options [ :truncate ]
234
- stdout . puts sentence
235
- end
180
+ printer = TablePrinter . new ( stdout , options )
181
+ printer . print ( array )
236
182
end
237
183
238
184
# Prints a long string, word-wrapping the text to the current width of the
@@ -245,33 +191,8 @@ def print_table(array, options = {}) # rubocop:disable Metrics/MethodLength
245
191
# indent<Integer>:: Indent each line of the printed paragraph by indent value.
246
192
#
247
193
def print_wrapped ( message , options = { } )
248
- indent = options [ :indent ] || 0
249
- width = terminal_width - indent
250
- paras = message . split ( "\n \n " )
251
-
252
- paras . map! do |unwrapped |
253
- words = unwrapped . split ( " " )
254
- counter = words . first . length
255
- words . inject do |memo , word |
256
- word = word . gsub ( /\n \005 / , "\n " ) . gsub ( /\005 / , "\n " )
257
- counter = 0 if word . include? "\n "
258
- if ( counter + word . length + 1 ) < width
259
- memo = "#{ memo } #{ word } "
260
- counter += ( word . length + 1 )
261
- else
262
- memo = "#{ memo } \n #{ word } "
263
- counter = word . length
264
- end
265
- memo
266
- end
267
- end . compact!
268
-
269
- paras . each do |para |
270
- para . split ( "\n " ) . each do |line |
271
- stdout . puts line . insert ( 0 , " " * indent )
272
- end
273
- stdout . puts unless para == paras . last
274
- end
194
+ printer = WrappedPrinter . new ( stdout , options )
195
+ printer . print ( message )
275
196
end
276
197
277
198
# Deals with file collision and returns true if the file should be
@@ -321,19 +242,6 @@ def file_collision(destination)
321
242
end
322
243
end
323
244
324
- # This code was copied from Rake, available under MIT-LICENSE
325
- # Copyright (c) 2003, 2004 Jim Weirich
326
- def terminal_width
327
- result = if ENV [ "THOR_COLUMNS" ]
328
- ENV [ "THOR_COLUMNS" ] . to_i
329
- else
330
- unix? ? dynamic_width : DEFAULT_TERMINAL_WIDTH
331
- end
332
- result < 10 ? DEFAULT_TERMINAL_WIDTH : result
333
- rescue
334
- DEFAULT_TERMINAL_WIDTH
335
- end
336
-
337
245
# Called if something goes wrong during the execution. This is used by Thor
338
246
# internally and should not be used inside your scripts. If something went
339
247
# wrong, you can always raise an exception. If you raise a Thor::Error, it
@@ -416,46 +324,8 @@ def quiet? #:nodoc:
416
324
mute? || ( base && base . options [ :quiet ] )
417
325
end
418
326
419
- # Calculate the dynamic width of the terminal
420
- def dynamic_width
421
- @dynamic_width ||= ( dynamic_width_stty . nonzero? || dynamic_width_tput )
422
- end
423
-
424
- def dynamic_width_stty
425
- `stty size 2>/dev/null` . split [ 1 ] . to_i
426
- end
427
-
428
- def dynamic_width_tput
429
- `tput cols 2>/dev/null` . to_i
430
- end
431
-
432
327
def unix?
433
- RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris)/i
434
- end
435
-
436
- def truncate ( string , width )
437
- as_unicode do
438
- chars = string . chars . to_a
439
- if chars . length <= width
440
- chars . join
441
- else
442
- chars [ 0 , width - 3 ] . join + "..."
443
- end
444
- end
445
- end
446
-
447
- if "" . respond_to? ( :encode )
448
- def as_unicode
449
- yield
450
- end
451
- else
452
- def as_unicode
453
- old = $KCODE
454
- $KCODE = "U"
455
- yield
456
- ensure
457
- $KCODE = old
458
- end
328
+ Terminal . unix?
459
329
end
460
330
461
331
def ask_simply ( statement , color , options )
0 commit comments