Skip to content

Instantly share code, notes, and snippets.

@dorianmariecom
Created August 24, 2024 09:13
Show Gist options
  • Save dorianmariecom/7e2b959c890260d3fdbc28cfa5f7e73c to your computer and use it in GitHub Desktop.
Save dorianmariecom/7e2b959c890260d3fdbc28cfa5f7e73c to your computer and use it in GitHub Desktop.
require "parallel"
require "dorian/arguments"
def red(string)
"\e[31m#{string}\e[0m"
end
parsed = Dorian::Arguments.parse(
output: { aliases: [:o, :out], default: true },
error: { aliases: [:e, :err], default: true },
)
Parallel.each($stdin.each_line.to_a) do |it|
it = it.strip!
read_out, write_out = IO.pipe
read_err, write_err = IO.pipe
pid = Process.spawn(
RbConfig.ruby,
"-e",
"it = #{it.inspect}",
"-e",
parsed.arguments.join(" "),
out: write_out,
err: write_err
)
write_out.close
write_err.close
while !read_out.eof? || !read_err.eof?
out = read_out.gets
puts "[#{it}] #{out.strip}" if out && parsed.options.output
err = read_err.gets
puts "[#{it}] #{red(err.strip)}" if err && parsed.options.error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment