From 0c769c01709959a2d0f9a70ae75ba547ad916c15 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Sat, 6 Jul 2024 01:24:06 -0700 Subject: [PATCH 1/4] Add print_files cli tool --- scripts/bin/print_files | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 scripts/bin/print_files diff --git a/scripts/bin/print_files b/scripts/bin/print_files new file mode 100755 index 0000000..94bb7a7 --- /dev/null +++ b/scripts/bin/print_files @@ -0,0 +1,24 @@ +#!/bin/bash + +# Function to print file content with the format +print_file_content() { + local filepath=$1 + echo "$filepath:" + cat "$filepath" + echo -e "\n" +} + +# List of file extensions to include +include_extensions=("py" "yml" "toml" "md" "txt" "Dockerfile") + +# Iterate through each file in the directory structure +find . -type f | while read -r filepath; do + filename=$(basename -- "$filepath") + extension="${filename##*.}" + + # Check if the file has an extension or is a Dockerfile + if [[ " ${include_extensions[@]} " =~ " ${extension} " ]] || [[ "$filename" == "Dockerfile" ]]; then + print_file_content "$filepath" + fi +done + From 319e1d1e44d6ad1b0d7cbd0dbbe7425c3a97f283 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Sat, 6 Jul 2024 01:27:38 -0700 Subject: [PATCH 2/4] Add option to dry-run -n --- scripts/bin/print_files | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/scripts/bin/print_files b/scripts/bin/print_files index 94bb7a7..9005e3a 100755 --- a/scripts/bin/print_files +++ b/scripts/bin/print_files @@ -11,6 +11,21 @@ print_file_content() { # List of file extensions to include include_extensions=("py" "yml" "toml" "md" "txt" "Dockerfile") +# Parse command line options +print_content=true +while getopts "n" opt; do + case $opt in + n) + print_content=false + ;; + *) + echo "Usage: $0 [-n]" + echo " -n Print only file names" + exit 1 + ;; + esac +done + # Iterate through each file in the directory structure find . -type f | while read -r filepath; do filename=$(basename -- "$filepath") @@ -18,7 +33,10 @@ find . -type f | while read -r filepath; do # Check if the file has an extension or is a Dockerfile if [[ " ${include_extensions[@]} " =~ " ${extension} " ]] || [[ "$filename" == "Dockerfile" ]]; then - print_file_content "$filepath" + echo "$filepath" + if $print_content; then + print_file_content "$filepath" + fi fi done From e74627568983b5b500815b4920098e0185cdbaf3 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Sat, 6 Jul 2024 01:28:42 -0700 Subject: [PATCH 3/4] Add option to copy content to clipboard Taking into account of the OS type like darwin vs linux --- scripts/bin/print_files | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/scripts/bin/print_files b/scripts/bin/print_files index 9005e3a..96952dc 100755 --- a/scripts/bin/print_files +++ b/scripts/bin/print_files @@ -8,19 +8,39 @@ print_file_content() { echo -e "\n" } +# Function to copy file content to clipboard +copy_to_clipboard() { + local filepath=$1 + if [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + cat "$filepath" | pbcopy + elif [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Linux + cat "$filepath" | 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 -while getopts "n" opt; do +copy_content=false +while getopts "nc" opt; do case $opt in n) print_content=false ;; + c) + copy_content=true + ;; *) - echo "Usage: $0 [-n]" + echo "Usage: $0 [-n] [-c]" echo " -n Print only file names" + echo " -c Copy content to clipboard" exit 1 ;; esac @@ -37,6 +57,10 @@ find . -type f | while read -r filepath; do if $print_content; then print_file_content "$filepath" fi + if $copy_content; then + copy_to_clipboard "$filepath" + echo "Content of $filepath copied to clipboard." + fi fi done From 6dba54257eb4e65a762c6386d2577f3b9ebec435 Mon Sep 17 00:00:00 2001 From: Shubham Chaudhary Date: Sat, 6 Jul 2024 01:31:30 -0700 Subject: [PATCH 4/4] Copy whole content instead of line by line --- scripts/bin/print_files | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/scripts/bin/print_files b/scripts/bin/print_files index 96952dc..19b475d 100755 --- a/scripts/bin/print_files +++ b/scripts/bin/print_files @@ -8,15 +8,22 @@ print_file_content() { echo -e "\n" } -# Function to copy file content to clipboard -copy_to_clipboard() { +# 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 - cat "$filepath" | pbcopy + echo "$combined_content" | pbcopy elif [[ "$OSTYPE" == "linux-gnu"* ]]; then # Linux - cat "$filepath" | xclip -selection clipboard + echo "$combined_content" | xclip -selection clipboard else echo "Clipboard copy not supported on this OS." exit 1 @@ -40,12 +47,15 @@ while getopts "nc" opt; do *) echo "Usage: $0 [-n] [-c]" echo " -n Print only file names" - echo " -c Copy content to clipboard" + 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 find . -type f | while read -r filepath; do filename=$(basename -- "$filepath") @@ -58,9 +68,12 @@ find . -type f | while read -r filepath; do print_file_content "$filepath" fi if $copy_content; then - copy_to_clipboard "$filepath" - echo "Content of $filepath copied to clipboard." + 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