Skip to content

Instantly share code, notes, and snippets.

@segeljakt
Created November 15, 2025 16:56
Show Gist options
  • Select an option

  • Save segeljakt/890476a8c160937cfff32abcf0f7cdca to your computer and use it in GitHub Desktop.

Select an option

Save segeljakt/890476a8c160937cfff32abcf0f7cdca to your computer and use it in GitHub Desktop.
A script for printing code. This relies on the `fd` command in Rust:
#!/bin/bash
while [ "$#" -gt 0 ]; do
case "$1" in
--include-tests)
INCLUDE_TESTS=1
;;
--include-docs)
INCLUDE_DOCS=1
;;
--include-bench)
INCLUDE_BENCH=1
;;
--include-fuzz)
INCLUDE_FUZZ=1
;;
--header-only)
HEADER_ONLY=1
;;
--include-data)
INCLUDE_DATA=1
;;
--include-gitmodules)
INCLUDE_GITMODULES=1
;;
--debug)
DEBUG=1
;;
--help)
echo "Usage: $0 [options] [paths...]"
echo "Options:"
echo " --include-tests Include test files"
echo " --include-docs Include documentation files"
echo " --include-bench Include benchmark files"
echo " --include-fuzz Include fuzzing files"
echo " --header-only Include only header files for C/C++"
echo " --include-data Include data files (JSON, XML, CSV)"
echo " --include-gitmodules Include files in git submodules"
echo " --debug Enable debug mode"
echo " --help Show this help message"
exit 0
;;
*)
break
# handle other arguments if needed
;;
esac
shift
done
HEADER_ONLY=${HEADER_ONLY:-0}
INCLUDE_TESTS=${INCLUDE_TESTS:-0}
INCLUDE_DOCS=${INCLUDE_DOCS:-0}
INCLUDE_BENCH=${INCLUDE_BENCH:-0}
INCLUDE_FUZZ=${INCLUDE_FUZZ:-0}
INCLUDE_DATA=${INCLUDE_DATA:-0}
INCLUDE_GITMODULES=${INCLUDE_GITMODULES:-0}
DEBUG=${DEBUG:-0}
BASE=$(basename $(pwd))
if [ $# -eq 0 ]; then
args=("./")
else
args=("$@")
fi
function print_file() {
local file_path="$1"
if [ "$DEBUG" -eq 1 ]; then
echo "$file_path"
else
echo "--- BEGIN $BASE FILE ${file_path#./} ---"
echo
sed 's/^/ /' "$file_path"
echo
echo "--- END $BASE FILE ${file_path#./} ---"
echo
fi
}
for arg in "${args[@]}"; do
if [ -f "$arg" ]; then
print_file "$arg"
elif [ -d "$arg" ]; then
flags=()
glob=()
glob+=("Dockerfile") # Docker
glob+=("*.go" "go.mod" "go.sum") # Go
glob+=("*.tsx" "*.ts" "package.json" "tsconfig.json") # TypeScript/JavaScript
if [ "$HEADER_ONLY" -eq 1 ]; then
glob+=("*.h")
else
glob+=("*.c" "*.h" "Makefile" "CMakeLists.txt" "*.cmake") # C/C++
fi
glob+=("*.zig" "*.zon") # Zig
glob+=("*.py" "requirements.txt" "pyproject.toml") # Python
glob+=("*.rs" "Cargo.toml") # Rust
glob+=("*.r") # R
glob+=("*.lua") # Lua
glob+=("*.env") # Environment files
glob+=("*.toml") # TOML
glob+=("*.md") # Markdown
glob+=("*.yaml" "*.yml") # YAML
glob+=("*.sh") # Shell scripts
if [ "$INCLUDE_DATA" -eq 1 ]; then
glob+=("*.json" "*.xml" "*.csv") # Data files
fi
flags+=("--glob" "{$(IFS=, ; echo "${glob[*]}")}")
if [ "$INCLUDE_GITMODULES" -eq 0 ]; then
if [ -f "$arg/.gitmodules" ]; then
for name in $(git -C "$arg" config --file .gitmodules --get-regexp path | awk '{ print $2 }'); do
flags+=("--exclude" "$name")
done
fi
fi
if [ "$INCLUDE_TESTS" -eq 0 ]; then
flags+=("--exclude" "*test*")
fi
if [ "$INCLUDE_FUZZ" -eq 0 ]; then
flags+=("--exclude" "*fuzz*")
fi
if [ "$INCLUDE_DOCS" -eq 0 ]; then
flags+=("--exclude" "*.md")
fi
if [ "$INCLUDE_BENCH" -eq 0 ]; then
flags+=("--exclude" "*bench*")
fi
flags+=("--exclude" "vendor")
flags+=("--exclude" "build")
flags+=("--exclude" "venv")
flags+=("--exclude" "node_modules")
fd --type file "${flags[@]}" "$arg" | while read -r file; do
print_file "$file"
done
fi
done
echo ""
@segeljakt
Copy link
Author

Note: The fd command is used by this script.

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