Repository navigation
Expand file tree
/
Copy pathcopy_code.sh
More file actions
executable file
·73 lines (63 loc) · 2.09 KB
/
Copy pathcopy_code.sh
File metadata and controls
executable file
·73 lines (63 loc) · 2.09 KB
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
#!/bin/bash
copy_clipboard() {
if command -v pbcopy >/dev/null 2>&1; then
pbcopy
elif command -v clip.exe >/dev/null 2>&1; then
clip.exe
elif command -v wl-copy >/dev/null 2>&1; then
wl-copy
elif command -v xclip >/dev/null 2>&1; then
xclip -selection clipboard
elif command -v xsel >/dev/null 2>&1; then
xsel --clipboard --input
elif command -v termux-clipboard-set >/dev/null 2>&1; then
termux-clipboard-set
else
cat >/dev/null
echo "copy_code: no clipboard utility found" >&2
return 1
fi
}
# Initialise arrays
DIRS=() # For arguments that start with a dot
EXTENSIONS=() # For arguments that do not start with a dot
# Temporary file buffer
CATBUFFER=$(mktemp)
# Loop through all arguments
for arg in "$@"; do
if [[ $arg == .* ]]; then
DIRS+=( "$arg" )
else
EXTENSIONS+=( "-iname" "*.$arg" "-o" )
fi
done
if [ ${#DIRS[@]} -eq 0 ]; then
DIRS+=( "." )
fi
# Loop through all directories
for dir in "${DIRS[@]}"; do
# remove trailing '-o' if present
if [ ${#EXTENSIONS[@]} -gt 0 ]; then
if [ ${EXTENSIONS[${#EXTENSIONS[@]}-1]} = "-o" ]; then
EXTENSIONS=("${EXTENSIONS[@]:0:${#EXTENSIONS[@]}-1}")
fi
# find files based on constructed conditions, excluding .git directory
find "$dir" -type f \
\( "${EXTENSIONS[@]}" \) \
-not -path './build/*' \
-not -path '*/.*' \
-exec sh -c \
'for file; do echo "======================[ $file ]======================" \
>> "$0"; cat "$file" >> "$0"; done' "$CATBUFFER" {} +
else
find "$dir" -type f \
-not -path './build/*' \
-not -path '*/.*' \
-exec sh -c \
'for file; do echo "======================[ $file ]======================" \
>> "$0"; cat "$file" >> "$0"; done' "$CATBUFFER" {} +
fi
done
# Remove temporary file buffer
copy_clipboard < "$CATBUFFER"
rm "$CATBUFFER"