-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellactivities
executable file
·277 lines (249 loc) · 6.92 KB
/
shellactivities
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/sh
prompt_reset="\e[m"
prompt_user_color="\e[1;34m" # BLUE
prompt_preposition_color="\e[1;37m" # WHITE
prompt_device_color="\e[01;32m"
prompt_direct_color="\e[1;34m"
prompt_git_status_color="\e[1;33m" # YELLOW
prompt_git_progress_color="\e[1;31m" # RED
prompt_symbol_color="" # NORMAL
prompt_git_master_color="\e[0;31m"
prompt_git_branch_color="\e[0;32m"
function get_git_branch() {
# On branches, this will return the branch name
# On non-branches, (no branch)
ref="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')"
if [[ "$ref" != "" ]]; then
echo "$ref"
else
echo "(no branch)"
fi
}
function get_git_progress() {
# Detect in-progress actions (e.g. merge, rebase)
# https://github.com/git/git/blob/v1.9-rc2/wt-status.c#L1199-L1241
git_dir="$(git rev-parse --git-dir)"
# git merge
if [[ -f "$git_dir/MERGE_HEAD" ]]; then
echo " [merge]"
elif [[ -d "$git_dir/rebase-apply" ]]; then
# git am
if [[ -f "$git_dir/rebase-apply/applying" ]]; then
echo " [am]"
# git rebase
else
echo " [rebase]"
fi
elif [[ -d "$git_dir/rebase-merge" ]]; then
# git rebase --interactive/--merge
echo " [rebase]"
elif [[ -f "$git_dir/CHERRY_PICK_HEAD" ]]; then
# git cherry-pick
echo " [cherry-pick]"
fi
if [[ -f "$git_dir/BISECT_LOG" ]]; then
# git bisect
echo " [bisect]"
fi
if [[ -f "$git_dir/REVERT_HEAD" ]]; then
# git revert --no-commit
echo " [revert]"
fi
}
function is_branch1_behind_branch2 () {
# Find the first log (if any) that is in branch1 but not branch2
first_log="$(git log $1..$2 -1 2> /dev/null)"
# Exit with 0 if there is a first log, 1 if there is not
[[ -n "$first_log" ]]
}
function branch_exists () {
# List remote branches | # Find our branch and exit with 0 or 1 if found/not found
git branch --remote 2> /dev/null | grep --quiet "$1"
}
function parse_git_ahead () {
# Grab the local and remote branch
branch="$(get_git_branch)"
remote_branch="origin/$branch"
# If the remote branch is behind the local branch
# or it has not been merged into origin (remote branch doesn't exist)
if (is_branch1_behind_branch2 "$remote_branch" "$branch" ||
! branch_exists "$remote_branch"); then
# echo our character
echo 1
fi
}
function parse_git_behind () {
# Grab the branch
branch="$(get_git_branch)"
remote_branch="origin/$branch"
# If the local branch is behind the remote branch
if is_branch1_behind_branch2 "$branch" "$remote_branch"; then
# echo our character
echo 1
fi
}
function parse_git_dirty() {
# If the git status has *any* changes (e.g. dirty), echo our character
if [[ -n "$(git status --porcelain 2> /dev/null)" ]]; then
echo 1
fi
}
function is_on_git() {
git rev-parse 2> /dev/null
}
function get_git_status() {
# Grab the git dirty and git behind
dirty_branch="$(parse_git_dirty)"
branch_ahead="$(parse_git_ahead)"
branch_behind="$(parse_git_behind)"
output=""
# Iterate through all the cases and if it matches, then echo
if [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
output="⬢"
elif [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 ]]; then
output="▲"
elif [[ "$dirty_branch" == 1 && "$branch_behind" == 1 ]]; then
output="▼"
elif [[ "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
output="⬡"
elif [[ "$branch_ahead" == 1 ]]; then
output="△"
elif [[ "$branch_behind" == 1 ]]; then
output="▽"
elif [[ "$dirty_branch" == 1 ]]; then
output="*"
fi
if [[ -n $output ]]; then
echo " $output"
fi
}
function get_git_info () {
# Grab the branch
branch="$(get_git_branch)"
# If there are any branches
if [[ "$branch" != "" ]]; then
# Echo the branch
output="$branch"
# Add on the git status
output="$output$(get_git_status)"
# Echo our output
echo "$output"
fi
}
function get_git_info_master () {
# Grab the branch
branch="$(get_git_branch)"
# If there are any branches
if [[ "$branch" != "" ]]; then
# Echo the branch
if [[ "master" == "$branch" || "$branch" == head* ]]; then
output="$branch"
fi
# Echo our output
echo "$output"
fi
}
function get_git_info_branch () {
# Grab the branch
branch="$(get_git_branch)"
# If there are any branches
if [[ "$branch" != "" ]]; then
# Echo the branch
if [[ "master" != "$branch" && "$branch" != head* ]]; then
output="$branch"
fi
# Echo our output
echo "$output"
fi
}
# Check status on all sub directories
function gsub () {
for file in `ls`;
do
if [ -d $file ]; then
cd $file;
echo -e '\e[37;44m'"\e[1m$file\e[0m"
git status;
cd ..;
fi
done;
}
# Find files and exec commands at them.
# $ find-exec .coffee cat | wc -l
# # => 9762
function find-exec() {
find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \;
}
# Count code lines in some directory.
# $ loc py js css
# # => Lines of code for .py: 3781
# # => Lines of code for .js: 3354
# # => Lines of code for .css: 2970
# # => Total lines of code: 10105
function loc() {
local total
local firstletter
local ext
local lines
total=0
for ext in $@; do
firstletter=$(echo $ext | cut -c1-1)
if [[ firstletter != "." ]]; then
ext=".$ext"
fi
lines=`find-exec "*$ext" cat | wc -l`
lines=${lines// /}
total=$(($total + $lines))
echo "Lines of code for ${fg[blue]}$ext${reset_color}: ${fg[green]}$lines${reset_color}"
done
echo "${fg[blue]}Total${reset_color} lines of code: ${fg[green]}$total${reset_color}"
}
# Show how much RAM application uses.
# $ ram safari
# # => safari uses 154.69 MBs of RAM.
function ram() {
local sum
local items
local app="$1"
if [ -z "$app" ]; then
echo "First argument - pattern to grep from processes"
else
sum=0
for i in `ps aux | grep -i "$app" | grep -v "grep" | awk '{print $6}'`; do
sum=$(($i + $sum))
done
sum=$(echo "scale=2; $sum / 1024.0" | bc)
if [[ $sum != "0" ]]; then
echo "${fg[blue]}${app}${reset_color} uses ${fg[green]}${sum}${reset_color} MBs of RAM."
else
echo "There are no processes with pattern '${fg[blue]}${app}${reset_color}' are running."
fi
fi
}
function cmd_exists() {
type "$1" &> /dev/null ;
}
function parse_yaml() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
function yaml_to_env() {
local file=$1
raw=$(parse_yaml $file)
for option in `echo $raw`
do
echo $option
eval export $option
done
}