run_tasks 1.6.0 → 1.7.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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/src/run.rb +57 -12
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fa8127bba38224f6ba4c779edae6b71f8f54a00
4
- data.tar.gz: f279a93debf06b7085b0d95358e7f9f1fcb34780
3
+ metadata.gz: 4d28b64a797694474cb625f1359e5ba0cf58316c
4
+ data.tar.gz: a5de99dc39b841f4a215818410803a777b558dea
5
5
  SHA512:
6
- metadata.gz: 86273c44aac9a5d1b447f75f5d606dcd0e14ca365455748adcf7b27a522953b0986fbfa68e768c2cd0d3b7f93f8a2d44bc6736bc1fd29933b0d317d4486870c0
7
- data.tar.gz: f8638842342a3a39744d2ff6f24b6007138d97c9ce28929fabb47754fd2bd9124619209d370614e5e7d6770f28346b80c4bcc2f69d8e532176f859fbc5c32194
6
+ metadata.gz: 31fa21481a39c615caa2663f9673717250a2b0ba12c52998d73ea6cbeaf69d4e1797fc0173c765f78aa0df46fbd1f7d6f6ad9d807eb2aa25c5f3b3663fd88cb8
7
+ data.tar.gz: 585078fa341b255f561951ed5ed9056d73c2d2e79c27d3c6a9b9bf6583facecb7f3184ed6d82799db9491d6381cb324a4996b1b8cabac9db562ad73b9535e6c7
data/src/run.rb CHANGED
@@ -33,24 +33,30 @@ HOMEPAGE = GEM&.homepage
33
33
  def task(name, help = nil, &block)
34
34
  if !name.is_a?(Symbol)
35
35
  puts
36
- puts "First task parameter must be a symbol".red
36
+ puts "'name' parameter must be a symbol".red
37
37
  exit 6
38
38
  end
39
- if !help.nil? && !help.is_a?(String)
40
- puts
41
- puts "Second task parameter must be a string".red
42
- exit 6
43
- end
44
- if help.nil?
39
+ if !help.nil?
40
+ if !help.is_a?(String)
41
+ puts
42
+ puts "'help' parameter must be a string".red
43
+ exit 6
44
+ end
45
+ else
46
+ # Load comments directly above the task as help verbatim.
45
47
  caller = caller_locations[0]
46
- line = File.readlines(caller.absolute_path)[caller.lineno - 2]
47
- match = /^\s*#\s*(?<comment>.+?)\s*$/.match(line)
48
- help = match[:comment] if !match.nil?
48
+ lines = File.readlines(caller.absolute_path)
49
+ help = (0..(caller.lineno - 2)).to_a.reverse.reduce([]) do |comments, lineno|
50
+ match = /^\s*#\s*(?<comment>.+?)\s*$/.match(lines[lineno])
51
+ break comments if match.nil?
52
+ comments << match[:comment]
53
+ comments
54
+ end.reverse
49
55
  end
50
56
  @tasks.store(
51
57
  name,
52
58
  {
53
- :help => help.nil? ? "" : Markdown::Engine.new(help).to_ansi,
59
+ :help => help.is_a?(String) ? [help] : help,
54
60
  :block => block
55
61
  }
56
62
  )
@@ -92,6 +98,34 @@ def are_you_sure?(text = "Are you sure?")
92
98
  exit 9 unless answer == "y"
93
99
  end
94
100
 
101
+ def menu(text, choices)
102
+ labels = nil
103
+ values = nil
104
+ choice = nil
105
+
106
+ if choices.is_a?(Array)
107
+ labels = choices
108
+ values = choices
109
+ elsif choices.is_a?(Hash)
110
+ labels = choices.keys
111
+ values = choices.values
112
+ else
113
+ puts "menu() 'choices' parameter must be an Array or an Hash".red
114
+ exit 10
115
+ end
116
+
117
+ loop do
118
+ labels.each_with_index do |label, index|
119
+ puts "#{index + 1}. #{label}"
120
+ end
121
+ puts text
122
+ choice = STDIN.gets.chomp.to_i
123
+ break if !values[choice - 1].nil?
124
+ end
125
+
126
+ values[choice - 1]
127
+ end
128
+
95
129
  # @param uri [String]
96
130
  def require_remote(uri)
97
131
  cache_path = "/tmp/run_cache_#{Digest::MD5.hexdigest(uri)}"
@@ -149,7 +183,18 @@ if ARGV.size == 0 || (ARGV.size == 1 && ARGV[0] == "help")
149
183
  end
150
184
  # Display each task and their help.
151
185
  @tasks.sort.to_h.each do |name, task|
152
- puts " #{name}".yellow + (" " * (max_size - name.size + 4)) + task[:help]
186
+ if task[:help].size == 0
187
+ puts " #{name}".yellow
188
+ next
189
+ end
190
+ task[:help].each_with_index do |help, index|
191
+ help = Markdown::Engine.new(help).to_ansi
192
+ if index == 0
193
+ puts " #{name}".yellow + (" " * (max_size - name.size + 4)) + help
194
+ next
195
+ end
196
+ puts (" " * (max_size + 5)) + help
197
+ end
153
198
  end
154
199
  exit
155
200
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: run_tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aurélien Delogu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-27 00:00:00.000000000 Z
11
+ date: 2023-07-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: aurelien.delogu@gmail.com