justrun 1.0.0 → 1.0.1

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/lib/justrun.rb +60 -11
  3. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ba0830fb9aa41d2dc2f4e73e5f574a798bdaecb
4
- data.tar.gz: a21c4123f097bb74ac3f89dafca0ad7f34cd94b9
3
+ metadata.gz: ec7679a78b2e88636c92105d18866774991c8ce8
4
+ data.tar.gz: 948d3b2b1d3f45e2b43146a15b72791fe37a7917
5
5
  SHA512:
6
- metadata.gz: 66b2bd7010a8bccc8a9784a5243c8a6861c07a9cad70f2ff8c8b33da2fc414f6f21f5fb4e775997cf0891e8c76ed3161393feba3144d51b322f665c18bd76212
7
- data.tar.gz: ec6c0fd746e9d0fa96e447c15f9f7c8b7b61d8b0c94b52de56f3d81cf0436f971e9191ff5b5061b02b15f72ef019dc1ffec485403df3b9922cb82bcc8be5d0ab
6
+ metadata.gz: e48401fce9c94d5377633ad2159ee6029bd2cafefc96d3453d31f5b898cead16ca2c8354fa805699f3dee97dba7881fab78d2434e518a12272665544b28fb097
7
+ data.tar.gz: c175d9e49ec3ac95fbf0a6da508f25920de60480d35dc8a3f806d05d26a6d9bf5cb555e53495f1bf8084860241955711208297ba6abd9f4f2d42d7ee8217bc53
data/lib/justrun.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  require 'open3'
2
2
 
3
- class JustRun
4
-
3
+ #TODO: Run multiple commands at the same time
5
4
 
6
- def self.command(command, init: lambda { |stdin|
7
- stdin.close_write
8
- }, &block)
5
+ class JustRun
6
+ def self.command(command, block_size: 4096*4, init: ->(writer) {}, &block)
9
7
  ret_code = -1
10
8
  Open3.popen3 command do |stdin, stdout, stderr, wait_thr|
11
- init.call stdin
9
+ writer = JustRun::Writer.new stdin
10
+ init.call writer
11
+
12
12
  fileno_lines = {}
13
13
  begin
14
14
  files = [stdout, stderr]
@@ -17,7 +17,7 @@ class JustRun
17
17
  stderr.fileno => :stderr
18
18
  }
19
19
  until all_eof files do
20
- ready = IO.select files
20
+ ready = IO.select files, stdin.closed? ? [] : [stdin], [], 0.1
21
21
  if ready
22
22
  readable = ready[0]
23
23
  readable.each do |f|
@@ -26,25 +26,29 @@ class JustRun
26
26
  lines = fileno_lines[fileno]
27
27
  name = fileno2name[fileno]
28
28
  begin
29
- data = f.read_nonblock BLOCK_SIZE
29
+ data = f.read_nonblock block_size
30
30
  lines_new = data.lines
31
31
  if lines.length > 0 and lines[-1] !~ /\n\r?$/
32
32
  lines[-1] = lines[-1] + lines_new.shift
33
33
  end
34
34
  lines.push(*lines_new)
35
35
  while lines[0] =~ /\n\r?/
36
- block.call lines.shift.chomp, name, stdin
36
+ block.call lines.shift.chomp, name, writer
37
37
  end
38
38
  rescue EOFError => e
39
39
  # expected
40
40
  end
41
+ writable = ready[1]
42
+ writable.each do |stdin|
43
+ writer.process
44
+ end
41
45
  end
42
46
  end
43
47
  end
44
48
  fileno_lines.each do |fileno, lines|
45
49
  name = fileno2name[fileno]
46
50
  if lines.length > 0
47
- block.call lines.shift.chomp, name, stdin
51
+ block.call lines.shift.chomp, name, writer
48
52
  end
49
53
  end
50
54
  rescue IOError => e
@@ -59,6 +63,51 @@ class JustRun
59
63
  def self.all_eof(files)
60
64
  files.find { |f| !f.eof }.nil?
61
65
  end
62
- BLOCK_SIZE = 4096*4
63
66
 
67
+ class Writer
68
+ def initialize stdin
69
+ @buffer = ''
70
+ @stdin = stdin
71
+ end
72
+
73
+ def puts str
74
+ write "#{str}\n"
75
+ end
76
+
77
+ def write str
78
+ @buffer << str;
79
+ process
80
+ end
81
+
82
+ def on_empty callback
83
+ @on_empty = callback
84
+ end
85
+
86
+ def end str = ''
87
+ if str.length > 0
88
+ write str
89
+ end
90
+ if @buffer.length > 0
91
+ on_empty -> {
92
+ self.end
93
+ }
94
+ else
95
+ @stdin.close_write
96
+ end
97
+ end
98
+
99
+ def process
100
+ return unless @buffer.length > 0
101
+ loop do
102
+ written = @stdin.write_nonblock @buffer
103
+ @buffer = @buffer[written..-1]
104
+ if @buffer.length == 0 and @on_empty
105
+ @on_empty.call
106
+ return
107
+ end
108
+ break if written == 0
109
+ end
110
+ rescue IO::WaitWritable, Errno::EINTR
111
+ end
112
+ end
64
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: justrun
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Kaczmarek
@@ -53,7 +53,9 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.6.2
55
55
  description: Wraps popen3 in a nice interface that allows to just run a command and
56
- get line by line output
56
+ get live stdout and stderr on line by line basis using a callback. Additionally
57
+ a live chat with a command can be implemented with a buffered non-blocking writer
58
+ that's working out of the box.
57
59
  email: rush@virtkick.com
58
60
  executables: []
59
61
  extensions: []
@@ -83,5 +85,5 @@ rubyforge_project:
83
85
  rubygems_version: 2.4.6
84
86
  signing_key:
85
87
  specification_version: 4
86
- summary: Run command and get live line by line callback
88
+ summary: Run command and get live line by line callbacks
87
89
  test_files: []