Last active
August 29, 2019 17:08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Launch pastel commands using GST syntax. | |
# | |
# Example | |
# | |
# $ pastel random ! mix green ! lighten 0.2 ! format | |
# | |
# https://github.com/sharkdp/pastel is expected to be in | |
# /usr/local/bin/pastel. Change the PROGRAM constant if | |
# you want something else. | |
SEPARATOR = "!" | |
PROGRAM = "/usr/local/bin/pastel" | |
Command = Struct.new(:args, :stdin) | |
if not ARGV.include?(SEPARATOR) | |
# Just execute pastel if we don't have multiple commands | |
exec(PROGRAM, *ARGV) | |
end | |
argv = ARGV | |
commands = [] | |
loop do | |
sep_idx = argv.index(SEPARATOR) | |
if sep_idx | |
commands << Command.new(argv.take(sep_idx)) | |
argv = argv.drop(sep_idx+1) | |
else | |
commands << Command.new(argv) | |
break | |
end | |
end | |
pids = commands.zip(commands.drop(1)).map do |cmd, next_cmd| | |
stdout = nil | |
if not next_cmd.nil? | |
r, w = IO.pipe | |
stdout = w | |
next_cmd.stdin = r | |
end | |
pid = fork do | |
STDOUT.reopen(stdout) if stdout | |
STDIN.reopen(cmd.stdin) if cmd.stdin | |
exec(PROGRAM, *cmd.args) | |
end | |
cmd.stdin&.close | |
stdout&.close | |
pid | |
end | |
# Execute them | |
exitstatus = 0 | |
pids.each do |pid| | |
Process.waitpid(pid) | |
if not $?.success? | |
exitstatus = $?.exitstatus | |
end | |
end | |
exit(exitstatus) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment