Skip to content

Instantly share code, notes, and snippets.

@Omnikron13
Created January 24, 2025 22:10
Show Gist options
  • Select an option

  • Save Omnikron13/794073108fab87289458ca30b450a310 to your computer and use it in GitHub Desktop.

Select an option

Save Omnikron13/794073108fab87289458ca30b450a310 to your computer and use it in GitHub Desktop.
A small script that prints an overview of the Rust crates found in its directory
#!/usr/bin/env bash
# overview.sh - Provides an overview of Rust crates in a directory.
#
# This script scans a directory for Rust crates (identified by Cargo.toml files),
# extracts relevant information from the Cargo.toml files (name, description, author),
# and displays it in a formatted, color-coded output.
#
# The script uses 'fd' to find Cargo.toml files and 'ripgrep' to extract information
# from them. It is designed to be run from the root of a directory containing
# multiple Rust projects/crates.
#
# Features:
# - Color-coded output for improved readability.
# - Customizable styles for path, name, author, and missing description.
# - Skips crate name in output if it's the same as the directory name.
# - Handles missing descriptions gracefully.
# - Excludes configurable directories (default: 'upstream').
# - Extracts the first author from the 'authors' field in Cargo.toml.
#
# Usage:
# 1. Save this script as 'overview.sh' in a directory containing Rust crates.
# 2. Make the script executable: 'chmod +x overview.sh'
# 3. Run the script from the directory: './overview.sh'
#
# Configuration:
# - EXCLUDE_DIRS: Space-separated list of directories to exclude (default: 'upstream').
# - PATH_STYLE: ANSI escape code for path color (default: yellow).
# - NAME_STYLE: ANSI escape code for crate name color (default: green).
# - AUTHOR_STYLE: ANSI escape code for author color (default: dark gray).
# - NO_DESC_STYLE: ANSI escape code for missing description style (default: italic blue).
#
# To customize colors, modify the ANSI escape codes in the configuration
# variables at the beginning of the script.
# Configuration variables
EXCLUDE_DIRS="upstream" # Space-separated list of directories to exclude
PATH_STYLE=$'\033[33m' # Yellow
NAME_STYLE=$'\033[32m' # Green
AUTHOR_STYLE=$'\033[90m' # Dark Gray (or use [38;5;242m for more precise grey if your terminal supports it)
NO_DESC_STYLE=$'\033[3;34m' # Italic Blue
# Build the fd exclude options
EXCLUDE_OPTS=""
for dir in $EXCLUDE_DIRS; do
EXCLUDE_OPTS+="--exclude $dir "
done
fd --glob 'Cargo.toml' $EXCLUDE_OPTS | while IFS= read -r cargo_file; do
name=$(rg -o '^name = "([^"]+)"' -r '$1' "$cargo_file" -m 1)
description=$(rg -o '^description = "([^"]+)"' -r '$1' "$cargo_file")
author=$(rg -o '^authors = \[["<]([^">]+)["\]>]' -r '$1' "$cargo_file" -m 1)
path=$(dirname "$cargo_file")
if [[ -z "$description" ]]; then
description="No description provided."
desc_color="$NO_DESC_STYLE"
else
description=$(printf '%s' "$description" | sed 's/"/\\"/g')
desc_color=""
fi
if [[ "$path" == "$name" ]]; then
printf "%s%s\033[0m: %s%s\033[0m %s%s\033[0m\n" "$PATH_STYLE" "$path" "$desc_color" "$description" "$AUTHOR_STYLE" "$author"
else
printf "%s%s\033[0m:%s%s\033[0m: %s%s\033[0m %s%s\033[0m\n" "$PATH_STYLE" "$path" "$NAME_STYLE" "$name" "$desc_color" "$description" "$AUTHOR_STYLE" "$author"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment