biscuide revised this gist . Go to revision
1 file changed, 127 insertions
printFilesAsMdCode.bash(file created)
| @@ -0,0 +1,127 @@ | |||
| 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" | |
Newer
Older