Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A script to take you to a random example on the readme for this repo :) #326

Open
shiv19 opened this issue Jun 25, 2024 · 2 comments
Open
Labels
new-example A proposal of the new example

Comments

@shiv19
Copy link

shiv19 commented Jun 25, 2024

#!/bin/bash

# URL of the GitHub repository
repo_url="https://github.com/denysdovhan/wtfjs"

# Fetch the README content
readme_content=$(curl -s "$repo_url")

# Extract all anchor links from the README
anchor_links=$(echo "$readme_content" | sed -n 's/.*<a href="#\([^"]*\)".*/\1/p')

# Count the number of anchor links
num_links=$(echo "$anchor_links" | wc -l)

# Generate a random number between 1 and the number of links
random_num=$((RANDOM % num_links + 1))

# Select the random anchor link
random_link=$(echo "$anchor_links" | sed -n "${random_num}p")

# Construct the full URL
full_url="${repo_url}#${random_link}"

# Print the URL (for debugging)
echo "Opening URL: $full_url"

# Open the URL in the default browser
if command -v open &> /dev/null; then
    open "$full_url"  # For macOS
elif command -v xdg-open &> /dev/null; then
    xdg-open "$full_url"  # For Linux
elif command -v start &> /dev/null; then
    start "$full_url"  # For Windows
else
    echo "Unable to open the browser automatically. Please open this URL manually:"
    echo "$full_url"
fi

Save as wtfjs.sh

sh wtfjs.sh

Disclaimer: This script was co-created with Claude 3.5 Sonnet.

@shiv19 shiv19 added the new-example A proposal of the new example label Jun 25, 2024
@shiv19
Copy link
Author

shiv19 commented Jun 25, 2024

@denysdovhan Please remove the label from this issue.

@shiv19
Copy link
Author

shiv19 commented Jun 25, 2024

My motivation to write this script was because simply running

npm install -g wtfjs
wtfjs

is currently broken.
And even when you get it working, it just prints the entire readme to the console.

I rather just have it to show me 1 example at a time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-example A proposal of the new example
Projects
None yet
Development

No branches or pull requests

1 participant