tty-command 0.8.0 → 0.8.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
  SHA1:
3
- metadata.gz: 0da26e779e4608d8ea38cf75801e0afa682dfd14
4
- data.tar.gz: 1a8f798ca2de05727cd9217a20314a9730763cd9
3
+ metadata.gz: 9c645ef5e1ef832ee5503018bc7aab93cdd71c17
4
+ data.tar.gz: 99b9fffd1e85616b3e36d4d0002684611a3a4dc0
5
5
  SHA512:
6
- metadata.gz: 6c966cec07e3ded53c89a294291e4ae9b74cdc7c0c81fb9a5843cd26bb0d7846fe14d57ae4b44756f8b5567fe40163bc27d7e33f2fedecb9826e97892002b9ba
7
- data.tar.gz: d098129b2cda2da46cab16cc33f1086f7f1aa163b6c6d6a4e4d2a62103ba9e626704bfc2f275e7e6583cbeab3ee5a9352de48135d214b491caaeae8f550e000d
6
+ metadata.gz: 16830dc6e8c5ac16616e4d14981924f18128800e1ddcae4cca6c1432c1649424a2dd758221a678267b1a76963c8c1aeebf49ff6da7a0e736d5dcb8abbc68dca5
7
+ data.tar.gz: 6ac45eeef2482b4f59ff9a5ec2486d2953b01bb1d7c9b6a99a373ecae7d89a3cf26f4aae464dbc984e893f898c34f6e4af88bc12de3a947b3f586459cb5ed918
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.8.1] - 2018-05-20
4
+
5
+ ### Changed
6
+ * Change ProcessRunner#write_stream to handle all writing logic
7
+
3
8
  ## [v0.8.0] - 2018-04-22
4
9
 
5
10
  ### Added
@@ -110,6 +115,7 @@
110
115
 
111
116
  * Initial implementation and release
112
117
 
118
+ [v0.8.1]: https://github.com/piotrmurach/tty-command/compare/v0.8.0...v0.8.1
113
119
  [v0.8.0]: https://github.com/piotrmurach/tty-command/compare/v0.7.0...v0.8.0
114
120
  [v0.7.0]: https://github.com/piotrmurach/tty-command/compare/v0.6.0...v0.7.0
115
121
  [v0.6.0]: https://github.com/piotrmurach/tty-command/compare/v0.5.0...v0.6.0
data/README.md CHANGED
@@ -194,14 +194,28 @@ cmd = TTY::Command.new(color: true)
194
194
 
195
195
  When using printers you can switch off coloring by using `:color` option set to `false`.
196
196
 
197
- #### 2.3.2 Uuid
197
+ #### 2.3.2 UUID
198
198
 
199
- By default, when logging is enabled, each log entry is prefixed by specific command run uuid number. This number can be switched off using the `:uuid` option:
199
+ By default, when logging is enabled and `pretty` printer is used, each log entry is prefixed by specific command run uuid number. This number can be switched off using the `:uuid` option at initialization:
200
200
 
201
201
  ```ruby
202
202
  cmd = TTY::Command.new(uuid: false)
203
203
  cmd.run('rm -R all_my_files')
204
- # => rm -r all_my_files
204
+ # =>
205
+ # Running rm -r all_my_files
206
+ # ...
207
+ # Finished in 6 seconds with exit status 0 (successful)
208
+ ```
209
+
210
+ or individually per command run:
211
+
212
+ ```rub
213
+ cmd = TTY::Command.new
214
+ cmd.run("echo hello", uuid: false)
215
+ # =>
216
+ # Running echo hello
217
+ # hello
218
+ # Finished in 0.003 seconds with exit status 0 (successful)
205
219
  ```
206
220
 
207
221
  #### 2.3.3 Only output on error
@@ -581,11 +595,11 @@ cmd.run(:ls, '-1').each("\t") { ... }
581
595
 
582
596
  ### 3.4 Custom printer
583
597
 
584
- If the built-in printers do not meet your requirements you can create your own. At the very minimum you need to specify the `write` method that will be called during the lifecycle of command execution:
598
+ If the built-in printers do not meet your requirements you can create your own. At the very minimum you need to specify the `write` method that will be called during the lifecycle of command execution. The `write` accepts two arguments, first the currently run command instance and second the message to be printed:
585
599
 
586
600
  ```ruby
587
601
  CustomPrinter < TTY::Command::Printers::Abstract
588
- def write(message)
602
+ def write(cmd, message)
589
603
  puts message
590
604
  end
591
605
  end
@@ -595,6 +609,7 @@ printer = CustomPrinter
595
609
  cmd = TTY::Command.new(printer: printer)
596
610
  ```
597
611
 
612
+ Please see [lib/tty/command/printers/abstract.rb](https://github.com/piotrmurach/tty-command/blob/master/lib/tty/command/printers/abstract.rb) for a full set of methods that you can override.
598
613
 
599
614
  ## 4. Example
600
615
 
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ require 'tty-command'
4
+
5
+ cmd = TTY::Command.new
6
+
7
+ path = File.expand_path("../spec/fixtures/infinite_input", __dir__)
8
+
9
+ range = 1..Float::INFINITY
10
+ infinite_input = range.lazy.map { |x| x }.first(10_000).join("\n")
11
+
12
+ cmd.run(path, input: infinite_input, timeout: 2)
@@ -11,24 +11,29 @@ module TTY
11
11
  class Pretty < Abstract
12
12
  TIME_FORMAT = "%5.3f %s".freeze
13
13
 
14
+ def initialize(*)
15
+ super
16
+ @uuid = options.fetch(:uuid) { true }
17
+ end
18
+
14
19
  def print_command_start(cmd, *args)
15
20
  message = ["Running #{decorate(cmd.to_command, :yellow, :bold)}"]
16
21
  message << args.map(&:chomp).join(' ') unless args.empty?
17
- write(cmd, message.join, cmd.uuid)
22
+ write(cmd, message.join)
18
23
  end
19
24
 
20
25
  def print_command_out_data(cmd, *args)
21
26
  message = args.map(&:chomp).join(' ')
22
- write(cmd, "\t#{message}", cmd.uuid, out_data)
27
+ write(cmd, "\t#{message}", out_data)
23
28
  end
24
29
 
25
30
  def print_command_err_data(cmd, *args)
26
31
  message = args.map(&:chomp).join(' ')
27
- write(cmd, "\t" + decorate(message, :red), cmd.uuid, err_data)
32
+ write(cmd, "\t" + decorate(message, :red), err_data)
28
33
  end
29
34
 
30
35
  def print_command_exit(cmd, status, runtime, *args)
31
- unless !cmd.only_output_on_error || status.zero?
36
+ if cmd.only_output_on_error && !status.zero?
32
37
  output << out_data
33
38
  output << err_data
34
39
  end
@@ -37,17 +42,18 @@ module TTY
37
42
  message = ["Finished in #{runtime}"]
38
43
  message << " with exit status #{status}" if status
39
44
  message << " (#{success_or_failure(status)})"
40
- write(cmd, message.join, cmd.uuid)
45
+ write(cmd, message.join)
41
46
  end
42
47
 
43
48
  # Write message out to output
44
49
  #
45
50
  # @api private
46
- def write(cmd, message, uuid = nil, data = nil)
47
- uuid_needed = options.fetch(:uuid) { true }
51
+ def write(cmd, message, data = nil)
52
+ cmd_set_uuid = cmd.options.fetch(:uuid, true)
53
+ uuid_needed = cmd.options[:uuid].nil? ? @uuid : cmd_set_uuid
48
54
  out = []
49
55
  if uuid_needed
50
- out << "[#{decorate(uuid, :green)}] " unless uuid.nil?
56
+ out << "[#{decorate(cmd.uuid, :green)}] " unless cmd.uuid.nil?
51
57
  end
52
58
  out << "#{message}\n"
53
59
  target = (cmd.only_output_on_error && !data.nil?) ? data : output
@@ -44,17 +44,7 @@ module TTY
44
44
 
45
45
  pid, stdin, stdout, stderr = ChildProcess.spawn(cmd)
46
46
 
47
- # no input to write, close child's stdin pipe
48
- stdin.close if (@input.nil? || @input.empty?) && !stdin.nil?
49
-
50
- writers = [@input && stdin].compact
51
-
52
- while writers.any?
53
- ready = IO.select(nil, writers, writers, @timeout)
54
- raise TimeoutExceeded if ready.nil?
55
-
56
- write_stream(ready[1], writers)
57
- end
47
+ write_stream(stdin, @input)
58
48
 
59
49
  stdout_data, stderr_data = read_streams(stdout, stderr)
60
50
 
@@ -84,7 +74,7 @@ module TTY
84
74
  private
85
75
 
86
76
  # The buffer size for reading stdout and stderr
87
- BUFSIZE = 3 * 1024
77
+ BUFSIZE = 16 * 1024
88
78
 
89
79
  # @api private
90
80
  def handle_timeout(runtime)
@@ -97,28 +87,35 @@ module TTY
97
87
  # Write the input to the process stdin
98
88
  #
99
89
  # @api private
100
- def write_stream(ready_writers, writers)
90
+ def write_stream(stream, input)
101
91
  start = Time.now
102
- ready_writers.each do |fd|
103
- begin
104
- err = nil
105
- size = fd.write(@input)
106
- @input = @input.byteslice(size..-1)
107
- rescue IO::WaitWritable
108
- rescue Errno::EPIPE => err
109
- # The pipe closed before all input written
110
- # Probably process exited prematurely
111
- fd.close
112
- writers.delete(fd)
113
- end
114
- if err || @input.bytesize == 0
115
- fd.close
116
- writers.delete(fd)
117
- end
92
+ writers = [input && stream].compact
118
93
 
119
- # control total time spent writing
120
- runtime = Time.now - start
121
- handle_timeout(runtime)
94
+ while writers.any?
95
+ ready = IO.select(nil, writers, writers, @timeout)
96
+ raise TimeoutExceeded if ready.nil?
97
+
98
+ ready[1].each do |writer|
99
+ begin
100
+ err = nil
101
+ size = writer.write(@input)
102
+ input = input.byteslice(size..-1)
103
+ rescue IO::WaitWritable
104
+ rescue Errno::EPIPE => err
105
+ # The pipe closed before all input written
106
+ # Probably process exited prematurely
107
+ writer.close
108
+ writers.delete(writer)
109
+ end
110
+ if err || input.bytesize == 0
111
+ writer.close
112
+ writers.delete(writer)
113
+ end
114
+
115
+ # control total time spent writing
116
+ runtime = Time.now - start
117
+ handle_timeout(runtime)
118
+ end
122
119
  end
123
120
  end
124
121
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Command
5
- VERSION = '0.8.0'
5
+ VERSION = '0.8.1'
6
6
  end # Command
7
7
  end # TTY
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-command
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-22 00:00:00.000000000 Z
11
+ date: 2018-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -107,6 +107,7 @@ files:
107
107
  - examples/stdin_input.rb
108
108
  - examples/threaded.rb
109
109
  - examples/timeout.rb
110
+ - examples/timeout_input.rb
110
111
  - examples/wait.rb
111
112
  - lib/tty-command.rb
112
113
  - lib/tty/command.rb