mister_bin 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f6758cdef54004ba6fc9f83f4b73f318213919800393c072f00d6b21daef46b
4
- data.tar.gz: 665a7f2759ac11bf3f7850cfbc2319cded0009f4776806f821c01b1b77691f26
3
+ metadata.gz: f9ead1a61f1fa10257fdada263fe53831d50eb347cb72511b9a84380f22c1fa0
4
+ data.tar.gz: 98d279539bbcefe16e28a29d8106fbac79613ce6eb9a8adf80359a7204c5933c
5
5
  SHA512:
6
- metadata.gz: 1c493f1c9e7e104be1c95d86baa4c3a806ae90272e592938787d17ae363b82a442265f7984ff32ba4708bc764124563a534b029f15180ba852430cc8d0b35e63
7
- data.tar.gz: 600cda0b6a7faaa6b98a32eb970cfcc19e725dcf4402eb741d29ce036568c062e64a07602b580d1330191e87fe99b9a4c816fd6ceb8356c78d264336b2eb5fc3
6
+ metadata.gz: c43175939978e46c952ca8d89b02111e8a5f6c001d1c973e6c18bed8da09cc81d2aa3fbb186896bf94bc0d7459d050ecac7972aefa899803f18282b9740110e0
7
+ data.tar.gz: 977f09d4e624c8870c0011a22d4cf6143751246b21b0dcdc19e81a225c6400abdb0e8e6fff4bb82d1c9a1c5c91cc05341c8a242d7e564017f5dbaf6661f08fa0
data/README.md CHANGED
@@ -239,4 +239,3 @@ end
239
239
 
240
240
 
241
241
  [1]: http://docopt.org/
242
-
@@ -9,13 +9,17 @@ module MisterBin
9
9
  end
10
10
 
11
11
  def run(argv=[])
12
- script = Script.new file
13
- script.build_docopt
14
12
  script.execute argv
15
13
  end
16
14
 
17
- # def argv
18
- # command.split ' '
19
- # end
15
+ def metadata
16
+ script.evaluate
17
+ end
18
+
19
+ private
20
+
21
+ def script
22
+ @script ||= Script.new file
23
+ end
20
24
  end
21
25
  end
@@ -1,8 +1,13 @@
1
+ require 'forwardable'
2
+
1
3
  module MisterBin
2
4
 
3
5
  # This class handles listing and finding command files
4
6
  class Commands
7
+ extend Forwardable
8
+
5
9
  attr_reader :basename, :basedir, :isolate
10
+ def_delegators :all, :[], :<<, :each, :size, :keys, :values, :empty?
6
11
 
7
12
  def initialize(basename, basedir='.', isolate: false)
8
13
  @basename = basename
@@ -14,12 +19,8 @@ module MisterBin
14
19
  @all ||= all!
15
20
  end
16
21
 
17
- def names
18
- all.keys.sort
19
- end
20
-
21
22
  def find(command, subcommand=nil)
22
- if subcommand and names.include? "#{command} #{subcommand}"
23
+ if subcommand and keys.include? "#{command} #{subcommand}"
23
24
  all["#{command} #{subcommand}"]
24
25
  else
25
26
  all["#{command}"]
@@ -38,7 +39,7 @@ module MisterBin
38
39
  end
39
40
  result[command] = Command.new command, file
40
41
  end
41
- result
42
+ result.sort.to_h
42
43
  end
43
44
 
44
45
  def files
@@ -1,4 +1,3 @@
1
- require 'singleton'
2
1
  require 'colsole'
3
2
 
4
3
  module MisterBin
@@ -6,17 +5,12 @@ module MisterBin
6
5
  # This singleton class is responsible for generating a text string ready to be used
7
6
  # by Docopt.
8
7
  class DocoptMaker
9
- include Singleton
10
8
  include Colsole
11
9
 
12
10
  attr_reader :usages, :options, :examples, :params
13
11
  attr_accessor :help, :version
14
12
 
15
13
  def initialize
16
- reset
17
- end
18
-
19
- def reset
20
14
  @usages = []
21
15
  @options = []
22
16
  @params = []
@@ -40,18 +40,31 @@ module MisterBin
40
40
  end
41
41
 
42
42
  def show_subs
43
- if commands.all.empty?
44
- puts "No subcommands found"
43
+ if commands.empty?
44
+ say "No subcommands found"
45
45
  else
46
- say "#{header}\n" if header
47
- puts "Commands:"
48
- commands.names.each { |command| puts " #{name} #{command}" }
49
- say "\n#{footer}" if footer
46
+ show_subs!
50
47
  end
51
48
 
52
49
  return 1
53
50
  end
54
51
 
52
+ def show_subs!
53
+ longest_key = commands.keys.max_by(&:size).size
54
+ max_summary_size = terminal_width - longest_key - 6
55
+
56
+ say "#{header}\n" if header
57
+
58
+ say "Commands:"
59
+ commands.each do |key, command|
60
+ summary = command.metadata[:summary] || ''
61
+ summary = summary[0..max_summary_size].strip
62
+ say " !bldgrn!#{key.ljust longest_key} !txtrst!#{summary}"
63
+ end
64
+
65
+ say "\n#{footer}" if footer
66
+ end
67
+
55
68
  def commands
56
69
  @commands ||= Commands.new name, basedir, isolate: isolate
57
70
  end
@@ -13,14 +13,9 @@ module MisterBin
13
13
  @file = file
14
14
  end
15
15
 
16
- def build_docopt
17
- DocoptMaker.instance.reset
18
- instance_eval script
19
- docopt
20
- end
21
-
22
16
  def execute(argv=[])
23
- args = Docopt.docopt docopt, version: DocoptMaker.instance.version, argv: argv
17
+ build_docopt
18
+ args = Docopt.docopt docopt, version: maker.version, argv: argv
24
19
  exitcode = action_block.call args if action_block
25
20
  exitcode.is_a?(Numeric) ? exitcode : 0
26
21
  rescue Docopt::Exit => e
@@ -28,34 +23,43 @@ module MisterBin
28
23
  1
29
24
  end
30
25
 
26
+ def evaluate
27
+ instance_eval script
28
+ metadata
29
+ end
30
+
31
31
  def docopt
32
- DocoptMaker.instance.docopt
32
+ maker.docopt
33
+ end
34
+
35
+ def metadata
36
+ { summary: help, version: version }
33
37
  end
34
38
 
35
39
  # DSL
36
40
 
37
- def help(text)
38
- DocoptMaker.instance.help = text
41
+ def help(text=nil)
42
+ text ? maker.help = text : maker.help
43
+ end
44
+
45
+ def version(text=nil)
46
+ text ? maker.version = text : maker.version
39
47
  end
40
48
 
41
49
  def usage(text)
42
- DocoptMaker.instance.usages << text
50
+ maker.usages << text
43
51
  end
44
52
 
45
53
  def option(flags, text)
46
- DocoptMaker.instance.options << [flags, text]
54
+ maker.options << [flags, text]
47
55
  end
48
56
 
49
57
  def param(param, text)
50
- DocoptMaker.instance.params << [param, text]
58
+ maker.params << [param, text]
51
59
  end
52
60
 
53
61
  def example(text)
54
- DocoptMaker.instance.examples << text
55
- end
56
-
57
- def version(text)
58
- DocoptMaker.instance.version = text
62
+ maker.examples << text
59
63
  end
60
64
 
61
65
  def action(&block)
@@ -64,6 +68,16 @@ module MisterBin
64
68
 
65
69
  private
66
70
 
71
+ def build_docopt
72
+ @maker = nil
73
+ instance_eval script
74
+ docopt
75
+ end
76
+
77
+ def maker
78
+ @maker ||= DocoptMaker.new
79
+ end
80
+
67
81
  def script
68
82
  @script ||= File.read file
69
83
  end
@@ -1,3 +1,3 @@
1
1
  module MisterBin
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mister_bin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-19 00:00:00.000000000 Z
11
+ date: 2018-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole