command_kit 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +15 -0
  3. data/.rubocop.yml +138 -0
  4. data/ChangeLog.md +29 -0
  5. data/Gemfile +3 -0
  6. data/README.md +141 -121
  7. data/Rakefile +3 -2
  8. data/command_kit.gemspec +4 -4
  9. data/examples/command.rb +1 -1
  10. data/gemspec.yml +7 -0
  11. data/lib/command_kit/arguments.rb +1 -1
  12. data/lib/command_kit/colors.rb +221 -45
  13. data/lib/command_kit/command.rb +1 -1
  14. data/lib/command_kit/commands.rb +4 -4
  15. data/lib/command_kit/help/man.rb +4 -25
  16. data/lib/command_kit/inflector.rb +47 -17
  17. data/lib/command_kit/main.rb +7 -9
  18. data/lib/command_kit/man.rb +44 -0
  19. data/lib/command_kit/open_app.rb +69 -0
  20. data/lib/command_kit/options/option.rb +1 -6
  21. data/lib/command_kit/options/parser.rb +15 -17
  22. data/lib/command_kit/options.rb +2 -2
  23. data/lib/command_kit/os/linux.rb +157 -0
  24. data/lib/command_kit/os.rb +159 -11
  25. data/lib/command_kit/package_manager.rb +200 -0
  26. data/lib/command_kit/pager.rb +46 -4
  27. data/lib/command_kit/printing/indent.rb +2 -2
  28. data/lib/command_kit/printing.rb +1 -1
  29. data/lib/command_kit/sudo.rb +40 -0
  30. data/lib/command_kit/terminal.rb +5 -0
  31. data/lib/command_kit/version.rb +1 -1
  32. data/spec/arguments/argument_spec.rb +1 -1
  33. data/spec/colors_spec.rb +256 -0
  34. data/spec/commands_spec.rb +1 -1
  35. data/spec/exception_handler_spec.rb +1 -1
  36. data/spec/help/man_spec.rb +0 -32
  37. data/spec/inflector_spec.rb +70 -8
  38. data/spec/man_spec.rb +46 -0
  39. data/spec/open_app_spec.rb +85 -0
  40. data/spec/options/option_spec.rb +2 -2
  41. data/spec/os/linux_spec.rb +154 -0
  42. data/spec/os_spec.rb +200 -13
  43. data/spec/package_manager_spec.rb +806 -0
  44. data/spec/pager_spec.rb +71 -6
  45. data/spec/sudo_spec.rb +51 -0
  46. data/spec/terminal_spec.rb +30 -0
  47. data/spec/usage_spec.rb +1 -1
  48. metadata +19 -4
data/Rakefile CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'rubygems'
4
2
 
5
3
  begin
@@ -21,3 +19,6 @@ task :default => :spec
21
19
  require 'yard'
22
20
  YARD::Rake::YardocTask.new
23
21
  task :doc => :yard
22
+
23
+ require 'rubocop/rake_task'
24
+ RuboCop::RakeTask.new
data/command_kit.gemspec CHANGED
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
1
  require 'yaml'
4
2
 
5
3
  Gem::Specification.new do |gem|
@@ -20,11 +18,13 @@ Gem::Specification.new do |gem|
20
18
  gem.authors = Array(gemspec['authors'])
21
19
  gem.email = gemspec['email']
22
20
  gem.homepage = gemspec['homepage']
21
+ gem.metadata = gemspec['metadata'] if gemspec['metadata']
23
22
 
24
23
  glob = lambda { |patterns| gem.files & Dir[*patterns] }
25
24
 
26
- gem.files = `git ls-files`.split($/)
27
- gem.files = glob[gemspec['files']] if gemspec['files']
25
+ gem.files = if gemspec['files'] then glob[gemspec['files']]
26
+ else `git ls-files`.split($/)
27
+ end
28
28
 
29
29
  gem.executables = gemspec.fetch('executables') do
30
30
  glob['bin/*'].map { |path| File.basename(path) }
data/examples/command.rb CHANGED
@@ -16,7 +16,7 @@ class Command < CommandKit::Command
16
16
 
17
17
  option :output, value: {
18
18
  type: String,
19
- usage: 'FILE',
19
+ usage: 'FILE'
20
20
  },
21
21
  short: '-o',
22
22
  desc: "Optional output file"
data/gemspec.yml CHANGED
@@ -9,6 +9,13 @@ authors: Postmodern
9
9
  email: postmodern.mod3@gmail.com
10
10
  homepage: https://github.com/postmodern/command_kit.rb#readme
11
11
 
12
+ metadata:
13
+ documentation_uri: https://rubydoc.info/gems/command_kit
14
+ source_code_uri: https://github.com/postmodern/command_kit.rb
15
+ bug_tracker_uri: https://github.com/postmodern/command_kit.rb/issues
16
+ changelog_uri: https://github.com/postmodern/command_kit.rb/blob/main/ChangeLog.md
17
+
18
+
12
19
  required_ruby_version: ">= 2.7.0"
13
20
 
14
21
  development_dependencies:
@@ -186,7 +186,7 @@ module CommandKit
186
186
  puts
187
187
  puts 'Arguments:'
188
188
 
189
- self.class.arguments.each_value do |arg|
189
+ arguments.each_value do |arg|
190
190
  puts " #{arg.usage.ljust(33)}#{arg.desc}"
191
191
  end
192
192
  end
@@ -26,7 +26,7 @@ module CommandKit
26
26
  #
27
27
  # ## Alternatives
28
28
  #
29
- # * [ansi](http://rubyworks.github.io/ansi/)
29
+ # * [ansi](https://rubyworks.github.io/ansi/)
30
30
  # * [colorize](https://github.com/fazibear/colorize#readme)
31
31
  #
32
32
  # @see https://en.wikipedia.org/wiki/ANSI_escape_code
@@ -67,7 +67,7 @@ module CommandKit
67
67
  # ANSI color code for blue
68
68
  BLUE = "\e[34m"
69
69
 
70
- # ANSI color code for megenta
70
+ # ANSI color code for magenta
71
71
  MAGENTA = "\e[35m"
72
72
 
73
73
  # ANSI color code for cyan
@@ -79,6 +79,51 @@ module CommandKit
79
79
  # ANSI color for the default foreground color
80
80
  RESET_COLOR = "\e[39m"
81
81
 
82
+ # ANSI color code for background color black
83
+ #
84
+ # @since 0.2.0
85
+ ON_BLACK = "\e[40m"
86
+
87
+ # ANSI color code for background color red
88
+ #
89
+ # @since 0.2.0
90
+ ON_RED = "\e[41m"
91
+
92
+ # ANSI color code for background color green
93
+ #
94
+ # @since 0.2.0
95
+ ON_GREEN = "\e[42m"
96
+
97
+ # ANSI color code for background color yellow
98
+ #
99
+ # @since 0.2.0
100
+ ON_YELLOW = "\e[43m"
101
+
102
+ # ANSI color code for background color blue
103
+ #
104
+ # @since 0.2.0
105
+ ON_BLUE = "\e[44m"
106
+
107
+ # ANSI color code for background color megenta
108
+ #
109
+ # @since 0.2.0
110
+ ON_MAGENTA = "\e[45m"
111
+
112
+ # ANSI color code for background color cyan
113
+ #
114
+ # @since 0.2.0
115
+ ON_CYAN = "\e[46m"
116
+
117
+ # ANSI color code for background color white
118
+ #
119
+ # @since 0.2.0
120
+ ON_WHITE = "\e[47m"
121
+
122
+ # ANSI color for the default background color
123
+ #
124
+ # @since 0.2.0
125
+ RESET_BG = "\e[49m"
126
+
82
127
  module_function
83
128
 
84
129
  #
@@ -273,71 +318,202 @@ module CommandKit
273
318
  else WHITE
274
319
  end
275
320
  end
276
- end
277
321
 
278
- #
279
- # Dummy module with the same interface as {ANSI}, but for when ANSI is not
280
- # supported.
281
- #
282
- module PlainText
283
- RESET = \
284
- CLEAR = \
285
- BOLD = \
286
- RESET_INTENSITY = \
287
- BLACK = \
288
- RED = \
289
- GREEN = \
290
- YELLOW = \
291
- BLUE = \
292
- MAGENTA = \
293
- CYAN = \
294
- WHITE = \
295
- RESET_COLOR = ''
296
-
297
- module_function
322
+ #
323
+ # Sets the background color to black.
324
+ #
325
+ # @param [String, nil] string
326
+ # An optional string.
327
+ #
328
+ # @return [String, ON_BLACK]
329
+ # The colorized string or just {ON_BLACK} if no arguments were given.
330
+ #
331
+ # @see ON_BLACK
332
+ #
333
+ # @api public
334
+ #
335
+ # @since 0.2.0
336
+ #
337
+ def on_black(string=nil)
338
+ if string then "#{ON_BLACK}#{string}#{RESET_BG}"
339
+ else ON_BLACK
340
+ end
341
+ end
298
342
 
299
- def reset
300
- RESET
343
+ #
344
+ # Sets the background color to red.
345
+ #
346
+ # @param [String, nil] string
347
+ # An optional string.
348
+ #
349
+ # @return [String, ON_RED]
350
+ # The colorized string or just {ON_RED} if no arguments were given.
351
+ #
352
+ # @see ON_RED
353
+ #
354
+ # @api public
355
+ #
356
+ # @since 0.2.0
357
+ #
358
+ def on_red(string=nil)
359
+ if string then "#{ON_RED}#{string}#{RESET_BG}"
360
+ else ON_RED
361
+ end
301
362
  end
302
363
 
303
- def clear
304
- reset
364
+ #
365
+ # Sets the background color to green.
366
+ #
367
+ # @param [String, nil] string
368
+ # An optional string.
369
+ #
370
+ # @return [String, ON_GREEN]
371
+ # The colorized string or just {ON_GREEN} if no arguments were given.
372
+ #
373
+ # @see ON_GREEN
374
+ #
375
+ # @api public
376
+ #
377
+ # @since 0.2.0
378
+ #
379
+ def on_green(string=nil)
380
+ if string then "#{ON_GREEN}#{string}#{RESET_BG}"
381
+ else ON_GREEN
382
+ end
305
383
  end
306
384
 
307
- def bold(string=nil)
308
- string || ''
385
+ #
386
+ # Sets the background color to yellow.
387
+ #
388
+ # @param [String, nil] string
389
+ # An optional string.
390
+ #
391
+ # @return [String, ON_YELLOW]
392
+ # The colorized string or just {ON_YELLOW} if no arguments were given.
393
+ #
394
+ # @see ON_YELLOW
395
+ #
396
+ # @api public
397
+ #
398
+ # @since 0.2.0
399
+ #
400
+ def on_yellow(string=nil)
401
+ if string then "#{ON_YELLOW}#{string}#{RESET_BG}"
402
+ else ON_YELLOW
403
+ end
309
404
  end
310
405
 
311
- def black(string=nil)
312
- string || ''
406
+ #
407
+ # Sets the background color to blue.
408
+ #
409
+ # @param [String, nil] string
410
+ # An optional string.
411
+ #
412
+ # @return [String, ON_BLUE]
413
+ # The colorized string or just {ON_BLUE} if no arguments were given.
414
+ #
415
+ # @see ON_BLUE
416
+ #
417
+ # @api public
418
+ #
419
+ # @since 0.2.0
420
+ #
421
+ def on_blue(string=nil)
422
+ if string then "#{ON_BLUE}#{string}#{RESET_BG}"
423
+ else ON_BLUE
424
+ end
313
425
  end
314
426
 
315
- def red(string=nil)
316
- string || ''
427
+ #
428
+ # Sets the background color to magenta.
429
+ #
430
+ # @param [String, nil] string
431
+ # An optional string.
432
+ #
433
+ # @return [String, ON_MAGENTA]
434
+ # The colorized string or just {ON_MAGENTA} if no arguments were given.
435
+ #
436
+ # @see ON_MAGENTA
437
+ #
438
+ # @api public
439
+ #
440
+ # @since 0.2.0
441
+ #
442
+ def on_magenta(string=nil)
443
+ if string then "#{ON_MAGENTA}#{string}#{RESET_BG}"
444
+ else ON_MAGENTA
445
+ end
317
446
  end
318
447
 
319
- def green(string=nil)
320
- string || ''
448
+ #
449
+ # Sets the background color to cyan.
450
+ #
451
+ # @param [String, nil] string
452
+ # An optional string.
453
+ #
454
+ # @return [String, ON_CYAN]
455
+ # The colorized string or just {ON_CYAN} if no arguments were given.
456
+ #
457
+ # @see ON_CYAN
458
+ #
459
+ # @api public
460
+ #
461
+ # @since 0.2.0
462
+ #
463
+ def on_cyan(string=nil)
464
+ if string then "#{ON_CYAN}#{string}#{RESET_BG}"
465
+ else ON_CYAN
466
+ end
321
467
  end
322
468
 
323
- def yellow(string=nil)
324
- string || ''
469
+ #
470
+ # Sets the background color to white.
471
+ #
472
+ # @param [String, nil] string
473
+ # An optional string.
474
+ #
475
+ # @return [String, ON_WHITE]
476
+ # The colorized string or just {ON_WHITE} if no arguments were given.
477
+ #
478
+ # @see ON_WHITE
479
+ #
480
+ # @api public
481
+ #
482
+ # @since 0.2.0
483
+ #
484
+ def on_white(string=nil)
485
+ if string then "#{ON_WHITE}#{string}#{RESET_BG}"
486
+ else ON_WHITE
487
+ end
325
488
  end
489
+ end
326
490
 
327
- def blue(string=nil)
328
- string || ''
491
+ #
492
+ # Dummy module with the same interface as {ANSI}, but for when ANSI is not
493
+ # supported.
494
+ #
495
+ module PlainText
496
+ ANSI.constants(false).each do |name|
497
+ const_set(name,'')
329
498
  end
330
499
 
331
- def magenta(string=nil)
332
- string || ''
500
+ module_function
501
+
502
+ def reset
503
+ RESET
333
504
  end
334
505
 
335
- def cyan(string=nil)
336
- string || ''
506
+ def clear
507
+ reset
337
508
  end
338
509
 
339
- def white(string=nil)
340
- string || ''
510
+ [
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
513
+ ].each do |name|
514
+ define_method(name) do |string=nil|
515
+ string || ''
516
+ end
341
517
  end
342
518
  end
343
519
 
@@ -56,7 +56,7 @@ module CommandKit
56
56
  # end
57
57
  # end
58
58
  #
59
- # ### initialize and using ivars
59
+ # ### initialize and using instance variables
60
60
  #
61
61
  # option :verbose, short: '-v', desc: "Increase verbose level" do
62
62
  # @verbose += 1
@@ -81,10 +81,10 @@ module CommandKit
81
81
  #
82
82
  def commands
83
83
  @commands ||= if superclass.kind_of?(ClassMethods)
84
- superclass.commands.dup
85
- else
86
- {}
87
- end
84
+ superclass.commands.dup
85
+ else
86
+ {}
87
+ end
88
88
  end
89
89
 
90
90
  #
@@ -3,6 +3,7 @@
3
3
  require 'command_kit/command_name'
4
4
  require 'command_kit/help'
5
5
  require 'command_kit/stdio'
6
+ require 'command_kit/man'
6
7
 
7
8
  module CommandKit
8
9
  module Help
@@ -23,6 +24,7 @@ module CommandKit
23
24
  include CommandName
24
25
  include Help
25
26
  include Stdio
27
+ include CommandKit::Man
26
28
 
27
29
  #
28
30
  # @api private
@@ -96,29 +98,6 @@ module CommandKit
96
98
  end
97
99
  end
98
100
 
99
- #
100
- # Displays the given man page.
101
- #
102
- # @param [String] page
103
- # The man page file name.
104
- #
105
- # @param [Integer, String, nil] section
106
- # The optional section number to specify.
107
- #
108
- # @return [Boolean, nil]
109
- # Specifies whether the `man` command was successful or not.
110
- # Returns `nil` when the `man` command is not installed.
111
- #
112
- # @api public
113
- #
114
- def man(page, section: nil)
115
- if section
116
- system('man',section.to_s,page.to_s)
117
- else
118
- system('man',page.to_s)
119
- end
120
- end
121
-
122
101
  #
123
102
  # Provides help information by showing one of the man pages within
124
103
  # {ClassMethods#man_dir .man_dir}.
@@ -153,8 +132,8 @@ module CommandKit
153
132
  # {ClassMethods#man_dir .man_dir} does not have a value.
154
133
  #
155
134
  # @note
156
- # if `TERM` is `dumb` or `$stdout` is not a TTY, fallsback to printing
157
- # the usual `--help` output.
135
+ # if `TERM` is `dumb` or `$stdout` is not a TTY, will fall back to
136
+ # printing the usual `--help` output.
158
137
  #
159
138
  # @api public
160
139
  #