-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeclip.sh
executable file
·129 lines (102 loc) · 3.71 KB
/
mergeclip.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# Set maximum character limit
MAX_CHARS=100000
# Initialize counts
totalCharacterCount=0
totalFileCount=0
mergedContent=""
# Debug log file
DEBUG_LOG="/tmp/file_merger_debug.log"
# Temporary file for content
TEMP_FILE="/tmp/merged_content.txt"
# Function to log debug information
debug_log() {
echo "$(date): $1" >> "$DEBUG_LOG"
}
debug_log "Script started"
# Function to check if a file is binary
is_binary() {
file --mime-encoding "$1" | grep -q binary
}
# Function to process a single file
process_file() {
local file="$1"
if [ $totalCharacterCount -ge $MAX_CHARS ]; then
debug_log "Reached character limit. Stopping scan."
return 1
fi
if is_binary "$file"; then
debug_log "Skipping binary file: $file"
return 0
fi
filename=$(basename "$file")
extension="${filename##*.}"
fileContent="$filename\n\`\`\`$extension\n$(cat "$file")\n\`\`\`\n\n"
fileCharCount=${#fileContent}
if [ $((totalCharacterCount + fileCharCount)) -ge $MAX_CHARS ]; then
debug_log "Adding this file would exceed the character limit. Stopping scan."
return 1
fi
mergedContent+="$fileContent"
totalCharacterCount=$((totalCharacterCount + fileCharCount))
totalFileCount=$((totalFileCount + 1))
debug_log "Processed file: $file"
return 0
}
# Function to process a directory
process_directory() {
local directory="$1"
while IFS= read -r -d '' file; do
process_file "$file" || break
done < <(find "$directory" -type f ! -name '.*' -print0)
}
# Check if arguments are provided
if [ $# -eq 0 ]; then
debug_log "No arguments provided. Exiting."
exit 1
fi
# Process all arguments
for item in "$@"; do
if [ -f "$item" ]; then
process_file "$item"
elif [ -d "$item" ]; then
process_directory "$item"
else
debug_log "Error: '$item' is not a valid file or directory."
fi
done
# Write content to temporary file
echo -n "$mergedContent" > "$TEMP_FILE"
debug_log "Content written to temporary file: $TEMP_FILE"
# Attempt to copy to clipboard using pbcopy
debug_log "Attempting to copy to clipboard using pbcopy..."
cat "$TEMP_FILE" | pbcopy
pbcopy_exit_code=$?
debug_log "pbcopy operation completed. Exit code: $pbcopy_exit_code"
# Check if pbcopy was successful
if [ $pbcopy_exit_code -ne 0 ] || [ $(pbpaste | wc -c) -eq 0 ]; then
debug_log "pbcopy failed or clipboard is empty. Trying osascript method..."
osascript -e "set the clipboard to (do shell script \"cat $TEMP_FILE\")"
osascript_exit_code=$?
debug_log "osascript clipboard operation completed. Exit code: $osascript_exit_code"
if [ $osascript_exit_code -ne 0 ]; then
debug_log "Error: Both pbcopy and osascript methods failed."
osascript -e 'display alert "Error" message "Failed to copy content to clipboard. Please check the debug log for more information."'
exit 1
fi
fi
# Get the character count of the clipboard content
clipboardCharCount=$(pbpaste | wc -c | tr -d '[:space:]')
debug_log "Characters in clipboard: $clipboardCharCount"
# Output the result
debug_log "Total files processed: $totalFileCount"
debug_log "Total characters in processed files: $totalCharacterCount"
debug_log "Characters copied to clipboard: $clipboardCharCount"
# Display result using osascript
# osascript -e "display dialog \"$totalFileCount files merged and copied to clipboard. $totalCharacterCount characters in total.\" buttons {\"OK\"} default button \"OK\"" 2>> "$DEBUG_LOG"
# Display result in command line
echo "$totalFileCount files merged and copied to clipboard. $totalCharacterCount characters in total."
# Clean up temporary file
rm "$TEMP_FILE"
debug_log "Temporary file removed"
debug_log "Script completed"