-
Notifications
You must be signed in to change notification settings - Fork 0
/
cattree
94 lines (83 loc) · 2.94 KB
/
cattree
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
#!/bin/bash
# Default values
EXTENSIONS="cs,csproj,sln,json,xml,config,js,ts,html,css,scss,jsx,tsx,py,java,class,jar,properties,sh,bash,zsh,bat,cmd,gradle,groovy,kts,yaml,yml,ini,md,txt,sql,ps1,psm1,cshtml,vbhtml,razor,vb,fs,fsharp,dll,pdb,exe,asp,aspx,ashx,asax,asp,ascx,master,sitemap,resx,rdlc,wsdl,xsd,svc,edmx,rdl,rdlc,rptproj,vbproj,vb,csx,targets,props,nuspec,pkgdef,vsix,suo,user,tt,tmpl,t4,t4s,license,log,cache,db,migrations,csv,tsv,pdf,info"
DEPTH=""
MAX_DEPTH=""
SHOW_HIDDEN="false"
# Default exclude pattern
EXCLUDE_PATTERN="bin|obj|.vscode|.vs|Migrations|.Designer.cs|.idea"
# Display help message
function show_help() {
echo "Usage: cattree [-e extensions] [-d depth] [-a] [-x pattern]"
echo " -e, --extensions Comma-separated list of file extensions to include (e.g., cs,json)"
echo " -d, --depth Depth level for the tree output and file search (e.g., 2)"
echo " -a, --all Include hidden directories and files (default is to exclude .vscode, .vs, bin, obj)"
echo " -x Custom pattern to exclude directories/files (overrides the default)"
echo " -h, --help Display this help message"
}
# Parse options
while [[ $# -gt 0 ]]; do
case $1 in
-e|--extensions)
EXTENSIONS=$(echo "$2" | tr -d '[:space:]')
shift
shift
;;
-d|--depth)
DEPTH="-L $2"
MAX_DEPTH="-maxdepth $2"
shift
shift
;;
-a|--all)
SHOW_HIDDEN="true"
shift
;;
-x)
EXCLUDE_PATTERN="$2"
shift
shift
;;
-h|--help)
show_help
exit 0
;;
*)
echo "Invalid option: $1"
show_help
exit 1
;;
esac
done
# Convert the extensions string into an array
IFS=',' read -r -a EXT_ARRAY <<< "$EXTENSIONS"
# Build the extensions pattern for the tree command
EXT_PATTERN=""
for ext in "${EXT_ARRAY[@]}"; do
EXT_PATTERN="$EXT_PATTERN -P '*.$ext'"
done
# Handle exclusion of directories if SHOW_HIDDEN is false
if [ "$SHOW_HIDDEN" = "false" ]; then
EXCLUDE_GREP="-E '$EXCLUDE_PATTERN'"
TREE_EXCLUDE="-I '$EXCLUDE_PATTERN'"
else
EXCLUDE_GREP=""
TREE_EXCLUDE=""
fi
echo -e "\n\033[0;34mCatTreeing \033[0;36m${PWD}\033[0m..."
# Properly format the find command to include all extensions and depth
FIND_PATTERN=""
for ext in "${EXT_ARRAY[@]}"; do
FIND_PATTERN="$FIND_PATTERN -name '*.$ext' -o"
done
FIND_PATTERN="${FIND_PATTERN% -o}" # Remove trailing '-o'
# Use find with the constructed pattern and depth
eval "find . $MAX_DEPTH -type f \( $FIND_PATTERN \) | grep -Ev $EXCLUDE_GREP | while read file; do
echo -e \"\n\033[0;34m==================== \033[0;36m\$file \033[0;34m====================\033[0m\n\"
cat \"\$file\"
echo
done"
# Generate tree output with files filtered by extensions
echo -e $'\n\033[0;34m==================== \033[0;36mCat Tree \033[0;34m====================\033[0m\n\n /\\_/\\\n( o.o )\n > ^ <\033[0;32m'
eval "tree $DEPTH $EXT_PATTERN $TREE_EXCLUDE" | sed 's/^/ /'
echo -e '\n\033[0m'