printFilesAsMdCode.bash
· 3.4 KiB · Bash
Raw
#!/bin/bash
# Function to print help message
print_help() {
echo "Description:"
echo " This script traverses a directory and prints the contents of files in markdown format."
echo " It was designed to simplify sharing code from a project with ChatGPT by formatting the files as markdown text."
echo " By default, it only includes files of type 'application' or 'text'."
echo ""
echo " Example output:"
echo ""
echo " ./example.txt"
echo " "'```'
echo " This is an example file content."
echo " "'```'
echo ""
echo ""
echo "Usage: printFilesAsMdCode [directory] [options]"
echo ""
echo "Options:"
echo " --clipboard, -c Copy output to clipboard"
echo " --ignore-types, -i Ignore file types and output all files"
echo " --include-hidden-files, -f Include hidden files"
echo " --include-hidden-dirs, -d Include hidden directories"
echo " --help, -h Display this help message"
echo ""
echo "Examples:"
echo " printFilesAsMdCode"
echo " # Outputs files from the current directory."
echo " printFilesAsMdCode /path/to/directory"
echo " # Outputs files from the specified directory."
echo " printFilesAsMdCode /path/to/directory --clipboard --ignore-types --include-hidden-files --include-hidden-dirs"
echo " printFilesAsMdCode /path/to/directory -c -i -f -d"
echo " # Outputs all files, from the specified directory including hidden files and directories, to the clipboard."
}
# Function to output file contents
output_file_content() {
local filepath=$1
echo ""
echo "$filepath"
echo '```'
cat "$filepath"
echo '```'
echo ""
}
# Default settings
DIR=$(pwd)
CLIPBOARD=false
IGNORE_TYPES=false
INCLUDE_HIDDEN_FILES=false
INCLUDE_HIDDEN_DIRS=false
# Parse flags and arguments
while [[ $# -gt 0 ]]; do
case $1 in
--clipboard|-c)
CLIPBOARD=true
shift
;;
--ignore-types|-i)
IGNORE_TYPES=true
shift
;;
--include-hidden-files|-f)
INCLUDE_HIDDEN_FILES=true
shift
;;
--include-hidden-dirs|-d)
INCLUDE_HIDDEN_DIRS=true
shift
;;
--help|-h)
print_help
exit 0
;;
*)
DIR=$1
shift
;;
esac
done
# Create a temporary file for clipboard
TEMP_FILE=$(mktemp)
# Build find command based on hidden file/dir inclusion
FIND_CMD="find \"$DIR\" -type f"
if ! $INCLUDE_HIDDEN_FILES; then
FIND_CMD="$FIND_CMD ! -name '.*'"
fi
if ! $INCLUDE_HIDDEN_DIRS; then
FIND_CMD="$FIND_CMD ! -path '*/.*/*'"
fi
# Traverse directory and output contents
{
eval "$FIND_CMD" | while read -r file; do
if $IGNORE_TYPES; then
relative_path="${file#"$DIR"/}"
output_file_content "$relative_path"
else
file_type=$(xdg-mime query filetype "$file")
major_type=$(echo "$file_type" | cut -d'/' -f1)
if [[ "$major_type" == "application" || "$major_type" == "text" ]]; then
relative_path="${file#"$DIR"/}"
output_file_content "$relative_path"
fi
fi
done
} | tee "$TEMP_FILE"
# Copy to clipboard if flag is set
if $CLIPBOARD; then
if command -v wl-copy &> /dev/null; then
wl-copy < "$TEMP_FILE"
elif command -v xclip &> /dev/null; then
xclip -selection clipboard < "$TEMP_FILE"
else
echo "No clipboard utility found. Please install wl-clipboard or xclip."
fi
echo "Output copied to clipboard."
fi
# Cleanup
rm "$TEMP_FILE"
| 1 | #!/bin/bash |
| 2 | |
| 3 | # Function to print help message |
| 4 | print_help() { |
| 5 | echo "Description:" |
| 6 | echo " This script traverses a directory and prints the contents of files in markdown format." |
| 7 | echo " It was designed to simplify sharing code from a project with ChatGPT by formatting the files as markdown text." |
| 8 | echo " By default, it only includes files of type 'application' or 'text'." |
| 9 | echo "" |
| 10 | echo " Example output:" |
| 11 | echo "" |
| 12 | echo " ./example.txt" |
| 13 | echo " "'```' |
| 14 | echo " This is an example file content." |
| 15 | echo " "'```' |
| 16 | echo "" |
| 17 | echo "" |
| 18 | echo "Usage: printFilesAsMdCode [directory] [options]" |
| 19 | echo "" |
| 20 | echo "Options:" |
| 21 | echo " --clipboard, -c Copy output to clipboard" |
| 22 | echo " --ignore-types, -i Ignore file types and output all files" |
| 23 | echo " --include-hidden-files, -f Include hidden files" |
| 24 | echo " --include-hidden-dirs, -d Include hidden directories" |
| 25 | echo " --help, -h Display this help message" |
| 26 | echo "" |
| 27 | echo "Examples:" |
| 28 | echo " printFilesAsMdCode" |
| 29 | echo " # Outputs files from the current directory." |
| 30 | echo " printFilesAsMdCode /path/to/directory" |
| 31 | echo " # Outputs files from the specified directory." |
| 32 | echo " printFilesAsMdCode /path/to/directory --clipboard --ignore-types --include-hidden-files --include-hidden-dirs" |
| 33 | echo " printFilesAsMdCode /path/to/directory -c -i -f -d" |
| 34 | echo " # Outputs all files, from the specified directory including hidden files and directories, to the clipboard." |
| 35 | } |
| 36 | |
| 37 | # Function to output file contents |
| 38 | output_file_content() { |
| 39 | local filepath=$1 |
| 40 | echo "" |
| 41 | echo "$filepath" |
| 42 | echo '```' |
| 43 | cat "$filepath" |
| 44 | echo '```' |
| 45 | echo "" |
| 46 | } |
| 47 | |
| 48 | # Default settings |
| 49 | DIR=$(pwd) |
| 50 | CLIPBOARD=false |
| 51 | IGNORE_TYPES=false |
| 52 | INCLUDE_HIDDEN_FILES=false |
| 53 | INCLUDE_HIDDEN_DIRS=false |
| 54 | |
| 55 | # Parse flags and arguments |
| 56 | while [[ $# -gt 0 ]]; do |
| 57 | case $1 in |
| 58 | --clipboard|-c) |
| 59 | CLIPBOARD=true |
| 60 | shift |
| 61 | ;; |
| 62 | --ignore-types|-i) |
| 63 | IGNORE_TYPES=true |
| 64 | shift |
| 65 | ;; |
| 66 | --include-hidden-files|-f) |
| 67 | INCLUDE_HIDDEN_FILES=true |
| 68 | shift |
| 69 | ;; |
| 70 | --include-hidden-dirs|-d) |
| 71 | INCLUDE_HIDDEN_DIRS=true |
| 72 | shift |
| 73 | ;; |
| 74 | --help|-h) |
| 75 | print_help |
| 76 | exit 0 |
| 77 | ;; |
| 78 | *) |
| 79 | DIR=$1 |
| 80 | shift |
| 81 | ;; |
| 82 | esac |
| 83 | done |
| 84 | |
| 85 | # Create a temporary file for clipboard |
| 86 | TEMP_FILE=$(mktemp) |
| 87 | |
| 88 | # Build find command based on hidden file/dir inclusion |
| 89 | FIND_CMD="find \"$DIR\" -type f" |
| 90 | if ! $INCLUDE_HIDDEN_FILES; then |
| 91 | FIND_CMD="$FIND_CMD ! -name '.*'" |
| 92 | fi |
| 93 | if ! $INCLUDE_HIDDEN_DIRS; then |
| 94 | FIND_CMD="$FIND_CMD ! -path '*/.*/*'" |
| 95 | fi |
| 96 | |
| 97 | # Traverse directory and output contents |
| 98 | { |
| 99 | eval "$FIND_CMD" | while read -r file; do |
| 100 | if $IGNORE_TYPES; then |
| 101 | relative_path="${file#"$DIR"/}" |
| 102 | output_file_content "$relative_path" |
| 103 | else |
| 104 | file_type=$(xdg-mime query filetype "$file") |
| 105 | major_type=$(echo "$file_type" | cut -d'/' -f1) |
| 106 | if [[ "$major_type" == "application" || "$major_type" == "text" ]]; then |
| 107 | relative_path="${file#"$DIR"/}" |
| 108 | output_file_content "$relative_path" |
| 109 | fi |
| 110 | fi |
| 111 | done |
| 112 | } | tee "$TEMP_FILE" |
| 113 | |
| 114 | # Copy to clipboard if flag is set |
| 115 | if $CLIPBOARD; then |
| 116 | if command -v wl-copy &> /dev/null; then |
| 117 | wl-copy < "$TEMP_FILE" |
| 118 | elif command -v xclip &> /dev/null; then |
| 119 | xclip -selection clipboard < "$TEMP_FILE" |
| 120 | else |
| 121 | echo "No clipboard utility found. Please install wl-clipboard or xclip." |
| 122 | fi |
| 123 | echo "Output copied to clipboard." |
| 124 | fi |
| 125 | |
| 126 | # Cleanup |
| 127 | rm "$TEMP_FILE" |
| 128 |