dry-stack 0.1.55 → 0.1.56

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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/rparallel +60 -13
  3. data/lib/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31af448879164bcafc2849deddb1a57ac32c879d8802c778a011ba63a042aac8
4
- data.tar.gz: ea33f69f427432a01a44eee65bbfe47252abbc3ead5c41f81efe4dbd66119b45
3
+ metadata.gz: f703dca5d6c72d78f7634b294ba41b739f19b5ee87c0177301ce0d8fc98e7148
4
+ data.tar.gz: 7f70554b5b5b421926ce36dc75e1fec8086d98a2df2f51c9b8763441ad869057
5
5
  SHA512:
6
- metadata.gz: f1ceae1fc14e54826ca71d95499e994114f02729aa3701e899a720fd6266167458e9b99626103a9a9bee23a0d8c69633ae8a49741225f9c9a65678b325c1cc0e
7
- data.tar.gz: 8e01abf90ecd9706ec7eba012adfe55eb73e5682563c1b5e6e55ac3d5bd7d65a3a52a6fd2b9b2c36d5ca52389d2f5fdab4d1e5aa84c70e93dd6cf0c2b293c885
6
+ metadata.gz: f8f8588139024f0a15c9ac97b103a545a5d0d997d7e6af7b4db07f9574bc242c25c9a2f4a3ff8caf0c6a4b01a4d7e70910a7eb59f6a561edc735ed9297850fd6
7
+ data.tar.gz: 37e9151d6f369f35eda3286c418758292c515526106c3c3e04b27c5e391ebc5908482675a0b9a80fe20db7bd6968f5e4e5566bf2b27b1304357f218924def989
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)
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.56'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.56
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artyom B