Skip to content

Instantly share code, notes, and snippets.

@RyanSquared
Last active November 3, 2022 17:39
Show Gist options
  • Save RyanSquared/8cd217264351b2b0690c3345fccc2cb9 to your computer and use it in GitHub Desktop.
Save RyanSquared/8cd217264351b2b0690c3345fccc2cb9 to your computer and use it in GitHub Desktop.
Capturing optionally ANSI colored output of commands
#!/bin/zsh
output="$1"
if [ "$MODE" = "internal-run-command" ]; then
shift
# Run the command
"$@"
# Capture the output, removing empty lines from the end of the file.
tmux capture-pane -S - -e \; save-buffer /proc/self/fd/1 \; delete-buffer | sed -z 's/\n\+$/\n/' | (
if [ "$output" = "xclip" ]; then
xclip -sel c
else
cat > "$output"
fi
)
# should take 1 second before xclipboard is copied
tmux rename-window 'COMPLETED'
sleep 3
exit 0
fi
if [ "$2" = "" ]; then
echo "Usage: $0 <xclip | FILE> <command> [argument ...]" 1>&2
exit 1
fi
window_output="$(tmux new-window -P "MODE=internal-run-command $0 $*")"
window="$(echo "$window_output" | cut -d : -f 2 | cut -d . -f 1)"
echo "$window_output"
while tmux list-windows -F '#{window_index} #W' | grep "^$window" | grep -v 'COMPLETED' >/dev/null; do
sleep 1
done
if [ "$output" = "xclip" ]; then
# hoist ownership over xclip to this session
xclip -sel c -o | xclip -sel c
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment