darkhelmet-darkext 0.0.3 → 0.1.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.
data/Manifest.txt CHANGED
@@ -12,6 +12,7 @@ test/test_helper.rb
12
12
  lib/darkext/array.rb
13
13
  lib/darkext/boolean.rb
14
14
  lib/darkext/hash.rb
15
+ lib/darkext/io.rb
15
16
  lib/darkext/numeric.rb
16
17
  lib/darkext/statistics.rb
17
18
  lib/darkext/string.rb
data/lib/darkext/io.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'darkext/hash'
2
+
3
+ module IO
4
+ # Runs a block and captures the output it generates
5
+ def self.capture_output(opts = { }) # yield e
6
+ opts.with_defaults!(:stdout => true, :stderr => true)
7
+ cout,cerr = opts[:stdout],opts[:stderr]
8
+ return if !cout && !cerr
9
+ old_stdout = STDOUT.dup if cout
10
+ old_stderr = STDERR.dup if cerr
11
+ r_stdout,w_stdout = IO.pipe if cout
12
+ r_stderr,w_stderr = IO.pipe if cerr
13
+ STDOUT.reopen(w_stdout) if cout
14
+ STDERR.reopen(w_stderr) if cerr
15
+ begin
16
+ yield
17
+ ensure
18
+ STDOUT.reopen(old_stdout) if cout
19
+ STDERR.reopen(old_stderr) if cerr
20
+ w_stdout.close if cout
21
+ w_stderr.close if cerr
22
+ end
23
+ ret_stdout = r_stdout.read if cout
24
+ ret_stderr = r_stderr.read if cerr
25
+ ret_stdout.close if cout
26
+ ret_stderr.close if cerr
27
+ return ret_stdout,ret_stderr if cout && cerr
28
+ return ret_stdout if cout
29
+ return ret_stderr if cerr
30
+ end
31
+ end
@@ -15,11 +15,15 @@ class String
15
15
  end
16
16
 
17
17
  # Executes the string with system
18
+ # * :background => true to run command in the background using & (currently only works on *nix systems)
19
+ # * :capture => true to capture the output. If :capture => true, background is voided
18
20
  def exec(opts = {})
19
- opts.with_defaults!(:background => false)
21
+ opts.with_defaults!(:background => false, :capture => false)
20
22
  cmd = self
21
- cmd += " &" if opts[:background]
22
- system(cmd)
23
+ # TODO: Do this with threads maybe, so it's OS agnostic?
24
+ cmd += " &" if opts[:background] && !opts[:capture]
25
+ return system(cmd) if !opts[:capture]
26
+ return `#{cmd}` if opts[:capture]
23
27
  end
24
28
 
25
29
  alias :/ :split
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darkhelmet-darkext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Huckstep
@@ -57,6 +57,7 @@ files:
57
57
  - lib/darkext/array.rb
58
58
  - lib/darkext/boolean.rb
59
59
  - lib/darkext/hash.rb
60
+ - lib/darkext/io.rb
60
61
  - lib/darkext/numeric.rb
61
62
  - lib/darkext/statistics.rb
62
63
  - lib/darkext/string.rb