-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground
168 lines (142 loc) Β· 6.07 KB
/
playground
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
RC="$SHELL"
RC=$(echo $SHELL | grep -c 'bash')
MODE=""
if [[ $RC -eq 1 ]]
then
RC="$HOME/.bashrc"
MODE="BASH"
elif [[ $(echo $SHELL | grep -c 'zsh') -eq 1 ]]
then
RC="$HOME/.zshrc"
MODE="ZSH"
else
echo "[UNSUPPORTED SHELL] exiting ..." | lolcat
exit 1
fi
#################### UPDATE ENV START #######################
function update_env() {
EXISTS=$(grep -c 'PLAYGROUND ENV' $RC)
if [[ $EXISTS -eq 0 ]]
then
echo '#################### PLAYGROUND ENV ####################' | tee -a $RC 1>/dev/null
echo '' | tee -a $RC 1>/dev/null
echo '#################### PLAYGROUND END ####################' | tee -a $RC 1>/dev/null
fi
ACTIVE=$(cat $HOME/.playground/.active)
if [ "$ACTIVE" != "" ]
then
cp $RC.original $RC 2>/dev/null
fi
cp $RC $RC.original 2>/dev/null
touch "$RC-plgrnd-env"
HEAD=$(grep -n 'PLAYGROUND ENV' $RC | cut -d ':' -f 1)
head -n $HEAD $RC | tee -a "$RC-plgrnd-env" 1>/dev/null
cat $HOME/.playground/$(cat $HOME/.playground/.active)/.envstd | tee -a "$RC-plgrnd-env" 1>/dev/null
cat $HOME/.playground/$(cat $HOME/.playground/.active)/.env | tee -a "$RC-plgrnd-env" 1>/dev/null
TAIL=`expr $(wc -l $RC | cut -d " " -f 1) - $(grep -n 'PLAYGROUND ENV' $RC | cut -d ':' -f 1)`
tail -n $TAIL $RC | tee -a "$RC-plgrnd-env" 1>/dev/null
mv "$RC-plgrnd-env" $RC 2>/dev/null
kill -USR1 $PPID
#$SHELL
}
###################### UPDATE ENV END #######################
###################### EXIT PLAYGROUND ######################
function exit_playground() {
echo "[+] exiting $(cat $HOME/.playground/.active) ..." | lolcat
echo "" | tee $HOME/.playground/.active 1>/dev/null
cp "$RC.original" $RC 2>/dev/null
cd $HOME
kill -USR1 $PPID
#$SHELL
}
######################### EXIT END ##########################
########################## HELP MENU ########################
function display_help() {
cat <<EOF
Playground script usage:
playground init [PLAYGROUND_NAME] - create a new playground
playground delete [PLAYGROUND_NAME] - delete a playground
playground list - list all playgrounds
playground export [VAR_NAME] [VAR_VALUE] - set an environment variable in the active playground
playground unset [VAR_NAME] - unset an environment variable in the active playground
playground clear - clear all environment variables in the active playground
playground update - update the current playground
playground exit - exit the active playground
playground help - display this help menu
EOF
}
#############################################################
####################### MAIN START ##########################
if [[ $# -eq 0 ]]
then
display_help
exit 0
fi
if [ "$1" == "help" ]
then
display_help
exit 0
fi
if [ "$1" == "init" ]
then
if [ "$2" != "" ]
then
if [ -x "$HOME/.playground/$2" ]
then
echo "" 1>/dev/null
else
echo "$2" | toilet | lolcat
mkdir -p "$HOME/.playground/$2" 2>/dev/null
if [ "$MODE" == "BASH" ]
then
echo "export PS1="'"\[\033[38;5;220m\](\[$(tput sgr0)\]\[\033[38;5;208m\]\u π₯ '"$2"'\[$(tput sgr0)\]\[\033[38;5;220m\])-[\[$(tput sgr0)\]\w\[$(tput sgr0)\]\[\033[38;5;220m\]]\[$(tput sgr0)\]\n\[$(tput sgr0)\]\[\033[38;5;220m\]\\$\[$(tput sgr0)\] \[$(tput sgr0)\]"' | tee -a $HOME/.playground/$2/.envstd 1>/dev/null
elif [ "$MODE" == "ZSH" ]
then
echo "export NEWLINE=$'\n'" | tee -a $HOME/.playground/$2/.envstd 1>/dev/null
echo "export PROMPT="'"%F{214}(%f%F{208}%n%fπ₯%F{208}'"$2"'%f%F{214})%f%F{214}-%f%F{214}[%f%F{white}%~%f%F{214}]%f%F{208}${NEWLINE}$%f "' | tee -a $HOME/.playground/$2/.envstd 1>/dev/null
fi
echo "cd $HOME/.playground/$2" | tee -a $HOME/.playground/$2/.envstd 1>/dev/null
echo "alias $2='""cd $HOME/.playground/$2'" | tee -a $HOME/.playground/$2/.envstd 1>/dev/null
touch $HOME/.playground/$2/.env 2>/dev/null
fi
echo $2 | tee $HOME/.playground/.active 1>/dev/null
update_env
else
echo "[-] please specify a playgroung name" | lolcat
fi
elif [ "$1" == "list" ]
then
echo ""
ls -1 "$HOME/.playground" | lolcat 2>/dev/null
echo ""
elif [ "$(cat $HOME/.playground/.active)" != "" ]
then
if [ "$1" == "export" ] || [ "$1" == "unset" ]
then
echo "export" "$2=""'""$3'" | tee -a $HOME/.playground/$(cat $HOME/.playground/.active)/.env 2>/dev/null
update_env
elif [ "$1" == "clear" ]
then
echo "" | tee $HOME/.playground/$(cat $HOME/.playground/.active)/.env 2>/dev/null
update_env
elif [ "$1" == "exit" ]
then
exit_playground
fi
fi
if [ "$1" == "delete" ]
then
echo "[+] deleting playground $2 ..." | lolcat
rm -rf $HOME/.playground/$2 2>/dev/null
# BUG
if [ "$2" == "$(grep -w "$2" $HOME/.playground/.active)" ]
then
exit_playground
fi
elif [ "$1" == "update" ]
then
echo "[+] updating ..." | lolcat
kill -USR1 $PPID
fi
########################## MAIN END #############################