command_kit 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -76,8 +76,53 @@ module CommandKit
76
76
  # ANSI color code for white
77
77
  WHITE = "\e[37m"
78
78
 
79
+ # ANSI color code for black
80
+ #
81
+ # @since 0.3.0
82
+ BRIGHT_BLACK = "\e[90m"
83
+
84
+ # ANSI color code for red
85
+ #
86
+ # @since 0.3.0
87
+ BRIGHT_RED = "\e[91m"
88
+
89
+ # ANSI color code for green
90
+ #
91
+ # @since 0.3.0
92
+ BRIGHT_GREEN = "\e[92m"
93
+
94
+ # ANSI color code for yellow
95
+ #
96
+ # @since 0.3.0
97
+ BRIGHT_YELLOW = "\e[93m"
98
+
99
+ # ANSI color code for blue
100
+ #
101
+ # @since 0.3.0
102
+ BRIGHT_BLUE = "\e[94m"
103
+
104
+ # ANSI color code for magenta
105
+ #
106
+ # @since 0.3.0
107
+ BRIGHT_MAGENTA = "\e[95m"
108
+
109
+ # ANSI color code for cyan
110
+ #
111
+ # @since 0.3.0
112
+ BRIGHT_CYAN = "\e[96m"
113
+
114
+ # ANSI color code for white
115
+ #
116
+ # @since 0.3.0
117
+ BRIGHT_WHITE = "\e[97m"
118
+
79
119
  # ANSI color for the default foreground color
80
- RESET_COLOR = "\e[39m"
120
+ #
121
+ # @since 0.3.0
122
+ RESET_FG = "\e[39m"
123
+
124
+ # @see RESET_FG
125
+ RESET_COLOR = RESET_FG
81
126
 
82
127
  # ANSI color code for background color black
83
128
  #
@@ -119,6 +164,46 @@ module CommandKit
119
164
  # @since 0.2.0
120
165
  ON_WHITE = "\e[47m"
121
166
 
167
+ # ANSI color code for background color bright black
168
+ #
169
+ # @since 0.3.0
170
+ ON_BRIGHT_BLACK = "\e[100m"
171
+
172
+ # ANSI color code for background color bright red
173
+ #
174
+ # @since 0.3.0
175
+ ON_BRIGHT_RED = "\e[101m"
176
+
177
+ # ANSI color code for background color bright green
178
+ #
179
+ # @since 0.3.0
180
+ ON_BRIGHT_GREEN = "\e[102m"
181
+
182
+ # ANSI color code for background color bright yellow
183
+ #
184
+ # @since 0.3.0
185
+ ON_BRIGHT_YELLOW = "\e[103m"
186
+
187
+ # ANSI color code for background color bright blue
188
+ #
189
+ # @since 0.3.0
190
+ ON_BRIGHT_BLUE = "\e[104m"
191
+
192
+ # ANSI color code for background color bright magenta
193
+ #
194
+ # @since 0.3.0
195
+ ON_BRIGHT_MAGENTA = "\e[105m"
196
+
197
+ # ANSI color code for background color bright cyan
198
+ #
199
+ # @since 0.3.0
200
+ ON_BRIGHT_CYAN = "\e[106m"
201
+
202
+ # ANSI color code for background color bright white
203
+ #
204
+ # @since 0.3.0
205
+ ON_BRIGHT_WHITE = "\e[107m"
206
+
122
207
  # ANSI color for the default background color
123
208
  #
124
209
  # @since 0.2.0
@@ -126,6 +211,10 @@ module CommandKit
126
211
 
127
212
  module_function
128
213
 
214
+ #
215
+ # @group Styling Methods
216
+ #
217
+
129
218
  #
130
219
  # Resets text formatting.
131
220
  #
@@ -167,6 +256,10 @@ module CommandKit
167
256
  end
168
257
  end
169
258
 
259
+ #
260
+ # @group Foreground Color Methods
261
+ #
262
+
170
263
  #
171
264
  # Sets the text color to black.
172
265
  #
@@ -181,7 +274,7 @@ module CommandKit
181
274
  # @api public
182
275
  #
183
276
  def black(string=nil)
184
- if string then "#{BLACK}#{string}#{RESET_COLOR}"
277
+ if string then "#{BLACK}#{string}#{RESET_FG}"
185
278
  else BLACK
186
279
  end
187
280
  end
@@ -200,7 +293,7 @@ module CommandKit
200
293
  # @api public
201
294
  #
202
295
  def red(string=nil)
203
- if string then "#{RED}#{string}#{RESET_COLOR}"
296
+ if string then "#{RED}#{string}#{RESET_FG}"
204
297
  else RED
205
298
  end
206
299
  end
@@ -219,7 +312,7 @@ module CommandKit
219
312
  # @api public
220
313
  #
221
314
  def green(string=nil)
222
- if string then "#{GREEN}#{string}#{RESET_COLOR}"
315
+ if string then "#{GREEN}#{string}#{RESET_FG}"
223
316
  else GREEN
224
317
  end
225
318
  end
@@ -238,7 +331,7 @@ module CommandKit
238
331
  # @api public
239
332
  #
240
333
  def yellow(string=nil)
241
- if string then "#{YELLOW}#{string}#{RESET_COLOR}"
334
+ if string then "#{YELLOW}#{string}#{RESET_FG}"
242
335
  else YELLOW
243
336
  end
244
337
  end
@@ -257,7 +350,7 @@ module CommandKit
257
350
  # @api public
258
351
  #
259
352
  def blue(string=nil)
260
- if string then "#{BLUE}#{string}#{RESET_COLOR}"
353
+ if string then "#{BLUE}#{string}#{RESET_FG}"
261
354
  else BLUE
262
355
  end
263
356
  end
@@ -276,7 +369,7 @@ module CommandKit
276
369
  # @api public
277
370
  #
278
371
  def magenta(string=nil)
279
- if string then "#{MAGENTA}#{string}#{RESET_COLOR}"
372
+ if string then "#{MAGENTA}#{string}#{RESET_FG}"
280
373
  else MAGENTA
281
374
  end
282
375
  end
@@ -295,7 +388,7 @@ module CommandKit
295
388
  # @api public
296
389
  #
297
390
  def cyan(string=nil)
298
- if string then "#{CYAN}#{string}#{RESET_COLOR}"
391
+ if string then "#{CYAN}#{string}#{RESET_FG}"
299
392
  else CYAN
300
393
  end
301
394
  end
@@ -314,11 +407,201 @@ module CommandKit
314
407
  # @api public
315
408
  #
316
409
  def white(string=nil)
317
- if string then "#{WHITE}#{string}#{RESET_COLOR}"
410
+ if string then "#{WHITE}#{string}#{RESET_FG}"
318
411
  else WHITE
319
412
  end
320
413
  end
321
414
 
415
+ #
416
+ # Sets the text color to bright black (gray).
417
+ #
418
+ # @param [String, nil] string
419
+ # An optional string.
420
+ #
421
+ # @return [String, BRIGHT_BLACK]
422
+ # The colorized string or just {BRIGHT_BLACK} if no arguments were
423
+ # given.
424
+ #
425
+ # @see BRIGHT_BLACK
426
+ #
427
+ # @api public
428
+ #
429
+ # @since 0.3.0
430
+ #
431
+ def bright_black(string=nil)
432
+ if string then "#{BRIGHT_BLACK}#{string}#{RESET_FG}"
433
+ else BRIGHT_BLACK
434
+ end
435
+ end
436
+
437
+ #
438
+ # Sets the text color to gray.
439
+ #
440
+ # @api public
441
+ #
442
+ # @see #bright_black
443
+ #
444
+ # @since 0.3.0
445
+ #
446
+ def gray(string=nil)
447
+ bright_black(string)
448
+ end
449
+
450
+ #
451
+ # Sets the text color to bright red.
452
+ #
453
+ # @param [String, nil] string
454
+ # An optional string.
455
+ #
456
+ # @return [String, BRIGHT_RED]
457
+ # The colorized string or just {BRIGHT_RED} if no arguments were given.
458
+ #
459
+ # @see BRIGHT_RED
460
+ #
461
+ # @api public
462
+ #
463
+ # @since 0.3.0
464
+ #
465
+ def bright_red(string=nil)
466
+ if string then "#{BRIGHT_RED}#{string}#{RESET_FG}"
467
+ else BRIGHT_RED
468
+ end
469
+ end
470
+
471
+ #
472
+ # Sets the text color to bright green.
473
+ #
474
+ # @param [String, nil] string
475
+ # An optional string.
476
+ #
477
+ # @return [String, BRIGHT_GREEN]
478
+ # The colorized string or just {BRIGHT_GREEN} if no arguments were
479
+ # given.
480
+ #
481
+ # @see BRIGHT_GREEN
482
+ #
483
+ # @api public
484
+ #
485
+ # @since 0.3.0
486
+ #
487
+ def bright_green(string=nil)
488
+ if string then "#{BRIGHT_GREEN}#{string}#{RESET_FG}"
489
+ else BRIGHT_GREEN
490
+ end
491
+ end
492
+
493
+ #
494
+ # Sets the text color to bright yellow.
495
+ #
496
+ # @param [String, nil] string
497
+ # An optional string.
498
+ #
499
+ # @return [String, BRIGHT_YELLOW]
500
+ # The colorized string or just {BRIGHT_YELLOW} if no arguments were
501
+ # given.
502
+ #
503
+ # @see BRIGHT_YELLOW
504
+ #
505
+ # @api public
506
+ #
507
+ # @since 0.3.0
508
+ #
509
+ def bright_yellow(string=nil)
510
+ if string then "#{BRIGHT_YELLOW}#{string}#{RESET_FG}"
511
+ else BRIGHT_YELLOW
512
+ end
513
+ end
514
+
515
+ #
516
+ # Sets the text color to bright blue.
517
+ #
518
+ # @param [String, nil] string
519
+ # An optional string.
520
+ #
521
+ # @return [String, BRIGHT_BLUE]
522
+ # The colorized string or just {BRIGHT_BLUE} if no arguments were given.
523
+ #
524
+ # @see BRIGHT_BLUE
525
+ #
526
+ # @api public
527
+ #
528
+ # @since 0.3.0
529
+ #
530
+ def bright_blue(string=nil)
531
+ if string then "#{BRIGHT_BLUE}#{string}#{RESET_FG}"
532
+ else BRIGHT_BLUE
533
+ end
534
+ end
535
+
536
+ #
537
+ # Sets the text color to bright magenta.
538
+ #
539
+ # @param [String, nil] string
540
+ # An optional string.
541
+ #
542
+ # @return [String, BRIGHT_MAGENTA]
543
+ # The colorized string or just {BRIGHT_MAGENTA} if no arguments were
544
+ # given.
545
+ #
546
+ # @see BRIGHT_MAGENTA
547
+ #
548
+ # @api public
549
+ #
550
+ # @since 0.3.0
551
+ #
552
+ def bright_magenta(string=nil)
553
+ if string then "#{BRIGHT_MAGENTA}#{string}#{RESET_FG}"
554
+ else BRIGHT_MAGENTA
555
+ end
556
+ end
557
+
558
+ #
559
+ # Sets the text color to bright cyan.
560
+ #
561
+ # @param [String, nil] string
562
+ # An optional string.
563
+ #
564
+ # @return [String, BRIGHT_CYAN]
565
+ # The colorized string or just {BRIGHT_CYAN} if no arguments were given.
566
+ #
567
+ # @see BRIGHT_CYAN
568
+ #
569
+ # @api public
570
+ #
571
+ # @since 0.3.0
572
+ #
573
+ def bright_cyan(string=nil)
574
+ if string then "#{BRIGHT_CYAN}#{string}#{RESET_FG}"
575
+ else BRIGHT_CYAN
576
+ end
577
+ end
578
+
579
+ #
580
+ # Sets the text color to bright white.
581
+ #
582
+ # @param [String, nil] string
583
+ # An optional string.
584
+ #
585
+ # @return [String, BRIGHT_WHITE]
586
+ # The colorized string or just {BRIGHT_WHITE} if no arguments were
587
+ # given.
588
+ #
589
+ # @see BRIGHT_WHITE
590
+ #
591
+ # @api public
592
+ #
593
+ # @since 0.3.0
594
+ #
595
+ def bright_white(string=nil)
596
+ if string then "#{BRIGHT_WHITE}#{string}#{RESET_FG}"
597
+ else BRIGHT_WHITE
598
+ end
599
+ end
600
+
601
+ #
602
+ # @group Background Color Methods
603
+ #
604
+
322
605
  #
323
606
  # Sets the background color to black.
324
607
  #
@@ -486,6 +769,193 @@ module CommandKit
486
769
  else ON_WHITE
487
770
  end
488
771
  end
772
+
773
+ #
774
+ # Sets the background color to bright black.
775
+ #
776
+ # @param [String, nil] string
777
+ # An optional string.
778
+ #
779
+ # @return [String, ON_BRIGHT_BLACK]
780
+ # The colorized string or just {ON_BRIGHT_BLACK} if no arguments were
781
+ # given.
782
+ #
783
+ # @see ON_BRIGHT_BLACK
784
+ #
785
+ # @api public
786
+ #
787
+ # @since 0.3.0
788
+ #
789
+ def on_bright_black(string=nil)
790
+ if string then "#{ON_BRIGHT_BLACK}#{string}#{RESET_BG}"
791
+ else ON_BRIGHT_BLACK
792
+ end
793
+ end
794
+
795
+ #
796
+ # @see #on_bright_black
797
+ #
798
+ # @api public
799
+ #
800
+ # @since 0.3.0
801
+ #
802
+ def on_gray(string=nil)
803
+ on_bright_black(string)
804
+ end
805
+
806
+ #
807
+ # Sets the background color to bright red.
808
+ #
809
+ # @param [String, nil] string
810
+ # An optional string.
811
+ #
812
+ # @return [String, ON_BRIGHT_RED]
813
+ # The colorized string or just {ON_BRIGHT_RED} if no arguments were
814
+ # given.
815
+ #
816
+ # @see ON_BRIGHT_RED
817
+ #
818
+ # @api public
819
+ #
820
+ # @since 0.3.0
821
+ #
822
+ def on_bright_red(string=nil)
823
+ if string then "#{ON_BRIGHT_RED}#{string}#{RESET_BG}"
824
+ else ON_BRIGHT_RED
825
+ end
826
+ end
827
+
828
+ #
829
+ # Sets the background color to bright green.
830
+ #
831
+ # @param [String, nil] string
832
+ # An optional string.
833
+ #
834
+ # @return [String, ON_BRIGHT_GREEN]
835
+ # The colorized string or just {ON_BRIGHT_GREEN} if no arguments were
836
+ # given.
837
+ #
838
+ # @see ON_BRIGHT_GREEN
839
+ #
840
+ # @api public
841
+ #
842
+ # @since 0.3.0
843
+ #
844
+ def on_bright_green(string=nil)
845
+ if string then "#{ON_BRIGHT_GREEN}#{string}#{RESET_BG}"
846
+ else ON_BRIGHT_GREEN
847
+ end
848
+ end
849
+
850
+ #
851
+ # Sets the background color to bright yellow.
852
+ #
853
+ # @param [String, nil] string
854
+ # An optional string.
855
+ #
856
+ # @return [String, ON_BRIGHT_YELLOW]
857
+ # The colorized string or just {ON_BRIGHT_YELLOW} if no arguments were
858
+ # given.
859
+ #
860
+ # @see ON_BRIGHT_YELLOW
861
+ #
862
+ # @api public
863
+ #
864
+ # @since 0.3.0
865
+ #
866
+ def on_bright_yellow(string=nil)
867
+ if string then "#{ON_BRIGHT_YELLOW}#{string}#{RESET_BG}"
868
+ else ON_BRIGHT_YELLOW
869
+ end
870
+ end
871
+
872
+ #
873
+ # Sets the background color to bright blue.
874
+ #
875
+ # @param [String, nil] string
876
+ # An optional string.
877
+ #
878
+ # @return [String, ON_BRIGHT_BLUE]
879
+ # The colorized string or just {ON_BRIGHT_BLUE} if no arguments were
880
+ # given.
881
+ #
882
+ # @see ON_BRIGHT_BLUE
883
+ #
884
+ # @api public
885
+ #
886
+ # @since 0.3.0
887
+ #
888
+ def on_bright_blue(string=nil)
889
+ if string then "#{ON_BRIGHT_BLUE}#{string}#{RESET_BG}"
890
+ else ON_BRIGHT_BLUE
891
+ end
892
+ end
893
+
894
+ #
895
+ # Sets the background color to bright magenta.
896
+ #
897
+ # @param [String, nil] string
898
+ # An optional string.
899
+ #
900
+ # @return [String, ON_BRIGHT_MAGENTA]
901
+ # The colorized string or just {ON_BRIGHT_MAGENTA} if no arguments were
902
+ # given.
903
+ #
904
+ # @see ON_BRIGHT_MAGENTA
905
+ #
906
+ # @api public
907
+ #
908
+ # @since 0.3.0
909
+ #
910
+ def on_bright_magenta(string=nil)
911
+ if string then "#{ON_BRIGHT_MAGENTA}#{string}#{RESET_BG}"
912
+ else ON_BRIGHT_MAGENTA
913
+ end
914
+ end
915
+
916
+ #
917
+ # Sets the background color to bright cyan.
918
+ #
919
+ # @param [String, nil] string
920
+ # An optional string.
921
+ #
922
+ # @return [String, ON_BRIGHT_CYAN]
923
+ # The colorized string or just {ON_BRIGHT_CYAN} if no arguments were
924
+ # given.
925
+ #
926
+ # @see ON_BRIGHT_CYAN
927
+ #
928
+ # @api public
929
+ #
930
+ # @since 0.3.0
931
+ #
932
+ def on_bright_cyan(string=nil)
933
+ if string then "#{ON_BRIGHT_CYAN}#{string}#{RESET_BG}"
934
+ else ON_BRIGHT_CYAN
935
+ end
936
+ end
937
+
938
+ #
939
+ # Sets the background color to bright white.
940
+ #
941
+ # @param [String, nil] string
942
+ # An optional string.
943
+ #
944
+ # @return [String, ON_BRIGHT_WHITE]
945
+ # The colorized string or just {ON_BRIGHT_WHITE} if no arguments were
946
+ # given.
947
+ #
948
+ # @see ON_BRIGHT_WHITE
949
+ #
950
+ # @api public
951
+ #
952
+ # @since 0.3.0
953
+ #
954
+ def on_bright_white(string=nil)
955
+ if string then "#{ON_BRIGHT_WHITE}#{string}#{RESET_BG}"
956
+ else ON_BRIGHT_WHITE
957
+ end
958
+ end
489
959
  end
490
960
 
491
961
  #
@@ -508,8 +978,11 @@ module CommandKit
508
978
  end
509
979
 
510
980
  [
511
- :bold, :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white,
512
- :on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white
981
+ :bold,
982
+ :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white,
983
+ :bright_black, :gray, :bright_red, :bright_green, :bright_yellow, :bright_blue, :bright_magenta, :bright_cyan, :bright_white,
984
+ :on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white,
985
+ :on_bright_black, :on_gray, :on_bright_red, :on_bright_green, :on_bright_yellow, :on_bright_blue, :on_bright_magenta, :on_bright_cyan, :on_bright_white
513
986
  ].each do |name|
514
987
  define_method(name) do |string=nil|
515
988
  string || ''
@@ -8,8 +8,7 @@ require 'command_kit/options'
8
8
  require 'command_kit/examples'
9
9
  require 'command_kit/description'
10
10
  require 'command_kit/exception_handler'
11
-
12
- require 'fileutils'
11
+ require 'command_kit/file_utils'
13
12
 
14
13
  module CommandKit
15
14
  #
@@ -0,0 +1,46 @@
1
+ require 'fileutils'
2
+ require 'erb'
3
+
4
+ module CommandKit
5
+ #
6
+ # File manipulation related methods.
7
+ #
8
+ # @since 0.3.0
9
+ #
10
+ # @api public
11
+ #
12
+ module FileUtils
13
+ include ::FileUtils
14
+
15
+ #
16
+ # Renders an erb file and optionally writes it out to a destination file.
17
+ #
18
+ # @param [String] source
19
+ # The path to the erb template file.
20
+ #
21
+ # @param [String, nil] dest
22
+ # The path to the destination file.
23
+ #
24
+ # @return [String, nil]
25
+ # If no destination path argument is given, the rendered erb template
26
+ # String will be returned.
27
+ #
28
+ # @example Rendering a ERB template and saving it's output:
29
+ # erb File.join(template_dir,'README.md.erb'), 'README.md'
30
+ #
31
+ # @example Rendering a ERB template and capturing it's output:
32
+ # output = erb(File.join(template_dir,'_partial.erb'))
33
+ #
34
+ def erb(source,dest=nil)
35
+ erb = ERB.new(File.read(source), trim_mode: '-')
36
+ result = erb.result(binding)
37
+
38
+ if dest
39
+ File.write(dest,result)
40
+ return
41
+ else
42
+ return result
43
+ end
44
+ end
45
+ end
46
+ end