module Bash

Public Class Methods

evaluate_string(script) click to toggle source
# File lib/tailog/ext/bash.rb, line 2
def self.evaluate_string script
  output = []

  Open3.popen3("bash -x -e") do |stdin, stdout, stderr, wait_thr|
    Thread.new do until (line = stdout.gets).nil? do output << [ :stdout, line ] end end
    Thread.new do until (line = stderr.gets).nil? do output << [ :stderr, line ] end end

    stdin.puts script
    stdin.puts "exit"

    wait_thr.join
  end

  output.map do |key, line|
    if key == :stderr && line =~ /^\+ (.*)/
      [:stdin, line]
    else
      [key, line]
    end
  end
end