dry-stack 0.1.55 → 0.1.57

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31af448879164bcafc2849deddb1a57ac32c879d8802c778a011ba63a042aac8
4
- data.tar.gz: ea33f69f427432a01a44eee65bbfe47252abbc3ead5c41f81efe4dbd66119b45
3
+ metadata.gz: 31df24a38beb3fafd6a02537928ab12544183eeb4edeffb8fcdac2033010af3d
4
+ data.tar.gz: 0b3137a155bca51c5464a442f76600517c87e47dcb4234dd7495eac16a2d2cfd
5
5
  SHA512:
6
- metadata.gz: f1ceae1fc14e54826ca71d95499e994114f02729aa3701e899a720fd6266167458e9b99626103a9a9bee23a0d8c69633ae8a49741225f9c9a65678b325c1cc0e
7
- data.tar.gz: 8e01abf90ecd9706ec7eba012adfe55eb73e5682563c1b5e6e55ac3d5bd7d65a3a52a6fd2b9b2c36d5ca52389d2f5fdab4d1e5aa84c70e93dd6cf0c2b293c885
6
+ metadata.gz: c27e4576b6f0a01d9fa79124e80447f9b908bef0eca50c595cd466fd2311b52c438b6efe5ab0e82f2f14e153a256cafa08a7178490368bb78970254e989a7a5e
7
+ data.tar.gz: 12aff94e708e1364c0b758c240fbeed909c6582565f9a3939bfcd1c871ab892d03bd253307432eb681a0abad2e79cfd33d58fb77f25ee0995843585b470a8c85
data/bin/rparallel CHANGED
@@ -10,6 +10,15 @@ Task = Struct.new(:number, :command, :started_at, :ended_at, :exit_code, keyword
10
10
  end
11
11
 
12
12
  class RParallel
13
+ COLORS = {
14
+ bold: "\e[1m",
15
+ dim: "\e[2m",
16
+ green: "\e[32m",
17
+ red: "\e[31m",
18
+ cyan: "\e[36m",
19
+ reset: "\e[0m"
20
+ }.freeze
21
+
13
22
  def self.run(argv, stdin: $stdin, stdout: $stdout, stderr: $stderr)
14
23
  return usage(stdout) if argv == ["--help"] || argv == ["-h"]
15
24
 
@@ -21,20 +30,22 @@ class RParallel
21
30
  commands = stdin.each_line.map(&:strip).reject(&:empty?)
22
31
  tasks = commands.each_with_index.map { |command, index| Task.new(number: index + 1, command:) }
23
32
 
24
- tasks.map { |task| Thread.new { run_task(task, stdout, stderr) } }.each(&:join)
33
+ color = color_enabled?
34
+ output_mutex = Mutex.new
35
+ tasks.map { |task| Thread.new { run_task(task, stdout, stderr, output_mutex, color) } }.each(&:join)
25
36
  print_report(tasks, stdout)
26
37
 
27
38
  tasks.all? { _1.exit_code.zero? } ? 0 : 1
28
39
  end
29
40
 
30
- def self.run_task(task, stdout, stderr)
41
+ def self.run_task(task, stdout, stderr, output_mutex, color)
31
42
  task.started_at = monotonic_time
32
43
 
33
44
  Open3.popen3("sh", "-c", task.command) do |child_stdin, child_stdout, child_stderr, wait_thread|
34
45
  child_stdin.close
35
46
  readers = [
36
- Thread.new { IO.copy_stream(child_stdout, stdout) },
37
- Thread.new { IO.copy_stream(child_stderr, stderr) }
47
+ Thread.new { stream_output(child_stdout, stdout, task.number, output_mutex) },
48
+ Thread.new { stream_output(child_stderr, stderr, task.number, output_mutex, color: :red, enabled: color) }
38
49
  ]
39
50
  readers.each(&:join)
40
51
  task.exit_code = wait_thread.value.exitstatus || 1
@@ -46,26 +57,61 @@ class RParallel
46
57
  task.ended_at = monotonic_time
47
58
  end
48
59
 
60
+ def self.stream_output(source, target, job_number, output_mutex, color: nil, enabled: false)
61
+ source.each_line do |line|
62
+ output = "[Job #{job_number}] #{line.chomp}"
63
+ output_mutex.synchronize { target.puts color ? colorize(output, color, enabled) : output }
64
+ end
65
+ end
66
+
49
67
  def self.print_report(tasks, stdout)
50
68
  rows = tasks.map do |task|
51
69
  [task.number.to_s, task.status, task.exit_code.to_s, format("%.3fs", task.duration), task.command]
52
70
  end
53
- widths = column_widths([["#", "status", "exit", "duration", "command"], *rows])
71
+ header = ["#", "status", "exit", "duration", "command"]
72
+ widths = column_widths([header, *rows])
73
+ color = color_enabled?
54
74
 
55
75
  stdout.puts
56
- stdout.puts "rparallel report"
57
- stdout.puts format_row(["#", "status", "exit", "duration", "command"], widths)
58
- rows.each { stdout.puts format_row(_1, widths) }
76
+ stdout.puts colorize("rparallel report", :bold, color)
77
+ stdout.puts colorize(border(widths), :dim, color)
78
+ stdout.puts format_row(header, widths, color:, header: true)
79
+ stdout.puts colorize(border(widths), :dim, color)
80
+ rows.each { stdout.puts format_row(_1, widths, color:) }
81
+ stdout.puts colorize(border(widths), :dim, color)
59
82
  end
60
83
 
61
84
  def self.column_widths(rows)
62
85
  rows.transpose.map { _1.map(&:length).max }
63
86
  end
64
87
 
65
- def self.format_row(row, widths)
66
- row.each_with_index.map do |value, index|
67
- index == row.length - 1 ? value : value.ljust(widths[index])
68
- end.join(" ")
88
+ def self.format_row(row, widths, color:, header: false)
89
+ cells = row.each_with_index.map do |value, index|
90
+ padded = value.ljust(widths[index])
91
+ header ? colorize(padded, :cyan, color) : colorize_report_cell(padded, row, index, color)
92
+ end
93
+
94
+ "| #{cells.join(' | ')} |"
95
+ end
96
+
97
+ def self.border(widths)
98
+ "+-#{widths.map { '-' * _1 }.join('-+-')}-+"
99
+ end
100
+
101
+ def self.colorize_report_cell(value, row, index, color)
102
+ return colorize(value, row[1] == "ok" ? :green : :red, color) if [1, 2].include?(index)
103
+
104
+ value
105
+ end
106
+
107
+ def self.colorize(value, color_name, enabled)
108
+ return value unless enabled
109
+
110
+ "#{COLORS.fetch(color_name)}#{value}#{COLORS.fetch(:reset)}"
111
+ end
112
+
113
+ def self.color_enabled?
114
+ ENV["NO_COLOR"].nil? && ENV["RPARALLEL_NO_COLOR"].nil?
69
115
  end
70
116
 
71
117
  def self.usage(stdout)
@@ -77,7 +123,8 @@ class RParallel
77
123
 
78
124
  def self.monotonic_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
79
125
 
80
- private_class_method :run_task, :print_report, :column_widths, :format_row, :usage, :monotonic_time
126
+ private_class_method :run_task, :stream_output, :print_report, :column_widths, :format_row, :border,
127
+ :colorize_report_cell, :colorize, :color_enabled?, :usage, :monotonic_time
81
128
  end
82
129
 
83
130
  exit RParallel.run(ARGV)
@@ -415,18 +415,31 @@ module Dry
415
415
  (@after_blocks ||=[]) << block
416
416
  end
417
417
 
418
+ def normalize_service_hook_args(name, opts)
419
+ if name.is_a?(Hash) && opts.empty?
420
+ opts = name
421
+ name = nil
422
+ end
423
+
424
+ opts = opts.dup
425
+ except = [opts.delete(:except)].flatten.compact
426
+ [[name].flatten.compact, opts, except]
427
+ end
428
+
418
429
  def BeforeService(name = nil, opts = {}, &block)
419
- (@before_blocks ||=[]).push names: [name].flatten.compact, except: opts[:except],
430
+ names, opts, except = normalize_service_hook_args(name, opts)
431
+ (@before_blocks ||=[]).push names:, except:,
420
432
  block: ->(s_name) {
421
433
  _ServiceImplementation s_name, opts, &block
422
434
  }
423
435
  end
424
436
 
425
437
  def AfterService(name = nil, opts = {}, &block)
438
+ names, opts, except = normalize_service_hook_args(name, opts)
426
439
  After do
427
- names = [name || @services.keys].flatten
428
- names -= [opts[:except]].flatten if opts.key? :except
429
- names.each do |s_name|
440
+ hook_names = names.empty? ? @services.keys : names
441
+ hook_names -= except
442
+ hook_names.each do |s_name|
430
443
  _ServiceImplementation s_name, opts, &block
431
444
  end
432
445
  end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  class Stack
3
- VERSION = '0.1.55'
3
+ VERSION = '0.1.57'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-stack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.55
4
+ version: 0.1.57
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artyom B
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-05-12 00:00:00.000000000 Z
11
+ date: 2026-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake