This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp-auto.bash
115 lines (105 loc) · 2.95 KB
/
mcp-auto.bash
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
#!/bin/bash
# will automaticly activate and deactivate virtual environments.
cd() {
# run main cd command
builtin cd "$@"
# check for "ls"
if [[ "$MCP_AUTO_LS" == true ]]; then
filecount=$(ls | wc -l)
if [[ "$filecount" -le "$MCP_LS_LIMIT" ]]; then
ls
fi
fi
# deactivate when going to a higher lvl than the env folder
if [[ "$PWD" != *"$autoenv_path"* ]] ; then
deactivate
unset autoenv_path
fi
# if virtual_env string length is zero (not activated)
if [[ -z "$VIRTUAL_ENV" ]]; then
## activate when going INTO a folder with .env & autoenv
if [[ -a "$PWD/.env/Scripts/autoenv" ]]; then
source "$PWD/.env/Scripts/activate"
autoenv_path="$PWD"
else if [[ -a '.env/bin/autoenv' ]]; then
source "$PWD/.env/bin/activate"
autoenv_path="$PWD"
fi
fi
fi
}
# goes back multiple dir's. (usage: cd.. 3)
cd_up() {
cd $(printf "%0.s../" $(seq 1 $1 ));
}
alias 'cd..'='cd_up'
# will activate .env if found
activate(){
# virtual env is not active
if [[ -z "$VIRTUAL_ENV" ]]; then
# set current path
local folder="$PWD"
# backtrack to find existing .env folder
for i in {1..10}; do
# activate when going INTO a folder with venv
if [[ -d "$folder/.env/" ]] ; then
if [[ -d "$folder/.env/Scripts" ]]; then
source "$folder/.env/Scripts/activate"
else
source "$folder/.env/bin/activate"
fi
autoenv_path="$folder"
return
else
# backtrack - remove last /*
local folder="$(echo "$folder" | sed 's|\(.*\)/.*|\1|')"
fi
done
echo 'error, no environment found.'
else
deactivate
activate
echo "Environment has been reloaded."
fi
}
# will run "ls" command after every "cd" command
autols(){
if [[ ! -f "$MCP_PATH/default_values" ]]; then
echo "First time setup..."
echo "This file is used to keep track of you settings:" > "$MCP_PATH/default_values"
echo "MCP_AUTO_LS=true" >> "$MCP_PATH/default_values"
echo "MCP_LS_LIMIT=20" >> "$MCP_PATH/default_values"
MCP_AUTO_LS=true
MCP_LS_LIMIT=20
echo "autols is enabled (disabled when more than $MCP_LS_LIMIT itmes in dir)"
else
if [[ $1 ]]; then
re='^[0-9]+$'
if [[ $1 =~ $re ]]; then
# set new limit nr
sed -i "s/MCP_LS_LIMIT=.*/MCP_LS_LIMIT=$1/" "$MCP_PATH/default_values"
MCP_LS_LIMIT=$1
echo 'limit updated to:' $MCP_LS_LIMIT
else
echo 'not valid, need a number'
fi
else
# toggle value
if [[ "$MCP_AUTO_LS" == false ]]; then
MCP_AUTO_LS=true
echo "autols is enabled (disabled when more than $MCP_LS_LIMIT itmes in dir)"
else
MCP_AUTO_LS=false
echo 'autols is disabled'
fi
# edit file
sed -i "s/MCP_AUTO_LS=.*/MCP_AUTO_LS=$MCP_AUTO_LS/" "$MCP_PATH/default_values"
fi
fi
}
# file dir path
MCP_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [[ -f "$MCP_PATH/default_values" ]]; then
MCP_AUTO_LS=$(grep "MCP_AUTO_LS=.*" "$MCP_PATH/default_values" | cut -d '=' -f2)
MCP_LS_LIMIT=$(grep "MCP_LS_LIMIT=.*" "$MCP_PATH/default_values" | cut -d '=' -f2)
fi