-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashrc_root
More file actions
340 lines (293 loc) · 7.9 KB
/
bashrc_root
File metadata and controls
340 lines (293 loc) · 7.9 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#
# ~/.bashrc
#
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
export PATH="$HOME/bin:$PATH"
export LANG=en_US.UTF-8
export EDITOR="vim"
export synclient VertEdgeScroll=1
# for setting hist`ory length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1500
HISTFILESIZE=3000
# don't put duplicate lines in the history. See bash(1) for more options
HISTCONTROL=ignoredups
# check the window size after command and, if necessary,
# update the values of LINES and COLUMNS
shopt -s checkwinsize
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
alias ls='ls --color=auto'
alias ll='ls -lh --color=auto'
alias la='ls -la --color=auto'
alias lla='ls -lha --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
function scd()
{
cd $1
echo "$(pwd)" > ~/.lastcd
}
alias cd='scd'
alias lcd='cd "$(cat ~/.lastcd)"'
alias tree='tree -C'
alias pcolors='( x=`tput op` y=`printf %$((${COLUMNS}-6))s`;for i in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done; )'
alias pmh='su -c pm-hibernate'
alias pms='su -c pm-suspend'
alias pmm='su -c pm-suspend-hybrid'
alias poff='su -c poweroff'
export TERM=xterm-256color
# On branch this return the branch name else '(no branch)'.
function get_git_branch()
{
branch_name="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')"
if [[ "$branch_name" != "" ]]; then
echo -n "$branch_name"
else
echo -n "(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 ()
{
# $ git log origin/master..master -1
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# 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"
# $ git log origin/master..master
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# 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"
# $ git log master..origin/master
# commit 4a633f715caf26f6e9495198f89bba20f3402a32
# Author: Todd Wolfson <todd@twolfson.com>
# Date: Sun Jul 7 22:12:17 2013 -0700
#
# Unsynced commit
# 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 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)"
# Iterate through all the cases and if it matches, then echo
if [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
echo "⬢"
elif [[ "$dirty_branch" == 1 && "$branch_ahead" == 1 ]]; then
echo "▲"
elif [[ "$dirty_branch" == 1 && "$branch_behind" == 1 ]]; then
echo "▼"
elif [[ "$branch_ahead" == 1 && "$branch_behind" == 1 ]]; then
echo "⬡"
elif [[ "$branch_ahead" == 1 ]]; then
echo "△"
elif [[ "$branch_behind" == 1 ]]; then
echo "▽"
elif [[ "$dirty_branch" == 1 ]]; then
echo "*"
fi
}
function is_on_git()
{
git rev-parse 2> /dev/null
}
function print_git_info()
{
tput setaf 233
tput setab 235
echo -n ""
tput setaf 28
echo -n " "
tput setaf 24
tput bold
echo -n "$(get_git_branch)"
tput setaf 202
echo -n "$(get_git_status)"
tput sgr0
tput setaf 235
echo -n ""
tput sgr0
}
#
# others functions
#
function isD_w()
{
IFS=' ' data=($(ls -l -d $(pwd)))
if [[ $(id -u) == 0 || "${data[0]:8:1}" == "w" || ( "${data[2]}" == "$(whoami)" && "${data[0]:2:1}" == "w" ) || ( "${data[3]}" == "id -g -n $(whoami)" && "${data[0]:5:1}" == "w") ]]
then
echo "can write"
else
echo "can't write"
fi
}
function print_location()
{
spwd="$(pwd)"
IFS='/' folders=($spwd)
fristfolder=${folders[@]:1:1}
length=${#folders[@]}
lastpos=$(($length - 1))
lastfolder="${folders[$lastpos]}"
for f in ${folders[@]}
do
if [ "$f" = "$lastfolder" ]
then
tput bold
tput setab 233
tput setaf 242
if [ "$(isD_w)" == "can't write" ]
then
tput setaf 160
echo -n " "
fi
tput setaf 255
echo -n " $lastfolder"
tput sgr0
tput setaf 233
echo ""
elif [ "$f" != "" ]
then
tput setab 233
tput setaf 242
echo -n " $f "
tput setaf 236
echo -n ""
fi
done
tput sgr0
}
function print_user()
{
CountJobs=$(jobs | wc -l)
if [ $CountJobs != 0 ]
then
tput setab 24
tput setaf 233
echo -n "$CountJobs"
fi
tput setab 233
tput setaf 24
echo -n ""
tput bold
tput setab 233
if [ $(id -u) == 0 ]
then
tput setaf 160
else
tput setaf 166
fi
echo -n "$(whoami)"
tput bold
tput setaf 236
echo -n "at"
tput sgr0
tput setab 233
tput setaf 242
echo -n "$(uname -n)"
if [ "$(cat ~/.socr.bash)" == "0" ]
then
tput setaf 34
echo -n "✔"
else
tput sgr0
tput setab 233
tput setaf 236
echo -n ""
tput setab 233
tput setaf 88
echo -n "$(cat ~/.socr.bash)"
tput setaf 160
echo -n "✘"
fi
tput sgr0
tput setaf 233
!(is_on_git) && echo -n ""
tput sgr0
}
function save_old_comand_result()
{
echo "$?" > ~/.socr.bash
}
PS1="\$(save_old_comand_result)\$(print_location)\$(print_user)\
\$(is_on_git && print_git_info)
"