Module: Mina::ExecHelpers

Defined in:
lib/mina-extras/jruby-patch.rb

Defined Under Namespace

Modules: Sys

Instance Method Summary collapse

Instance Method Details

#pretty_system(code) ⇒ Object

### pretty_system Internal: A pretty version of the default ‘#system` commands, but indents and puts color.

Returns the exit code in integer form.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mina-extras/jruby-patch.rb', line 14

def pretty_system(code)
  require 'shellwords'
  cmds = Shellwords.shellsplit(code)
  coathooks = 0

  status =
    Tools.popen4(*cmds) do |pid, i, o, e|
      # Handle `^C`.
      trap("INT") { Sys.handle_sigint(coathooks += 1, pid, self) }

      # __In the background,__ make stdin passthru, and stream stderr.
      th_err = Sys.stream_stderr!(e) { |str| print_stderr str }
      th_in  = Sys.stream_stdin!     { |chr| i.putc chr }

      # __In the foreground,__ stream stdout to the output helper.
      Sys.stream_stdout(o) { |ch| print_char ch }

      th_err.join
      th_in.terminate
    end

  status.exitstatus
end