Skip to content

Commit

Permalink
Merge branch 'feature/print-files'
Browse files Browse the repository at this point in the history
  • Loading branch information
ylogx committed Jul 7, 2024
2 parents 37e88ca + 57a8433 commit fda83d1
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions scripts/bin/print_files
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

# Function to print file content with the format
print_file_content() {
local filepath=$1
echo "$filepath:"
cat "$filepath"
echo -e "\n"
}

# Function to collect file content
collect_file_content() {
local filepath=$1
echo "$filepath:"
cat "$filepath"
echo -e "\n"
}

# Function to copy combined content to clipboard
copy_combined_content_to_clipboard() {
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
echo "$combined_content" | pbcopy
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux
echo "$combined_content" | xclip -selection clipboard
else
echo "Clipboard copy not supported on this OS."
exit 1
fi
}

# List of file extensions to include
include_extensions=("py" "yml" "toml" "md" "txt" "Dockerfile")

# Parse command line options
print_content=true
copy_content=false
while getopts "nc" opt; do
case $opt in
n)
print_content=false
;;
c)
copy_content=true
;;
*)
echo "Usage: $0 [-n] [-c]"
echo " -n Print only file names"
echo " -c Copy combined content to clipboard"
exit 1
;;
esac
done

# Initialize combined content variable
combined_content=""

# Iterate through each file in the directory structure
for filepath in $(find . -type f); do
filename=$(basename -- "$filepath")
extension="${filename##*.}"

# Check if the file has an extension or is a Dockerfile
if [[ " ${include_extensions[@]} " =~ " ${extension} " ]] || [[ "$filename" == "Dockerfile" ]]; then
echo "$filepath"
if $print_content; then
print_file_content "$filepath"
fi
if $copy_content; then
combined_content+=$(collect_file_content "$filepath")
fi
fi
done

if $copy_content; then
copy_combined_content_to_clipboard
echo "Combined content of all files copied to clipboard."
fi

0 comments on commit fda83d1

Please sign in to comment.