Skip to content

Instantly share code, notes, and snippets.

@Omustardo
Last active August 23, 2024 16:29
Show Gist options
  • Save Omustardo/ba21538445cbc9ff89ad43a4366f7382 to your computer and use it in GitHub Desktop.
Save Omustardo/ba21538445cbc9ff89ad43a4366f7382 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Function to retrieve a specific line from a GitHub file
fetch_github_line() {
local file="$1"
local line="$2"
# Construct GitHub raw content URL
github_path=$(echo "$file" | sed -E 's/.*cimgui-go\/?//')
github_url="https://raw.githubusercontent.com/gabstv/cimgui-go/main/$github_path"
# Fetch the specific line using sed
github_line=$(curl -s "$github_url" | sed -n "${line}p")
if [ -n "$github_line" ]; then
echo "Line $line from $(basename "$file"):" >&2
echo "$github_line" >&2
else
echo "Couldn't retrieve the specified line from GitHub." >&2
fi
}
# Create named pipes for output and exit status
output_pipe=$(mktemp -u)
mkfifo "$output_pipe"
exit_status_pipe=$(mktemp -u)
mkfifo "$exit_status_pipe"
# Temporary file for storing output
output_file=$(mktemp)
# Run the Go program in the background, capturing its output and exit status
{
"$@" > "$output_pipe" 2>&1
echo $? > "$exit_status_pipe"
} &
# Read and display output in real-time while capturing it
while IFS= read -r line; do
echo "$line"
echo "$line" >> "$output_file"
done < "$output_pipe" &
# Wait for the Go program to finish and read its exit status
read exit_status < "$exit_status_pipe"
# If exit status is 1, search for file and line number in the output
if [ "$exit_status" -eq 1 ]; then
file_line=$(grep -E "File: .*/cimgui-go/.*\.cpp, Line: [0-9]+" "$output_file" | tail -n 1)
if [ -n "$file_line" ]; then
file=$(echo "$file_line" | sed -E 's/File: (.*), Line:.*/\1/')
line=$(echo "$file_line" | sed -E 's/.*Line: ([0-9]+)/\1/')
fetch_github_line "$file" "$line"
fi
fi
# Clean up
rm -f "$output_pipe" "$exit_status_pipe" "$output_file"
# Exit with the same status as the Go program
exit "$exit_status"
@Omustardo
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment