aboutsummaryrefslogtreecommitdiffstats
path: root/INSTALL/config/bashrc
blob: a8222a0cfc725b6e741df0a4183697cf43153801 (plain)
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
# Sample .bashrc for SuSE Linux
# Copyright (c) SuSE GmbH Nuernberg

# There are 3 different types of shells in bash: the login shell, normal shell
# and interactive shell. Login shells read ~/.profile and interactive shells
# read ~/.bashrc; in our setup, /etc/profile sources ~/.bashrc - thus all
# settings made here will also take effect in a login shell.
#
# NOTE: It is recommended to make language settings in ~/.profile rather than
# here, since multilingual X sessions would not work properly if LANG is over-
# ridden in every subshell.

# Some applications read the EDITOR variable to determine your favourite text
# editor. So uncomment the line below and enter the editor of your choice :-)
#export EDITOR=/usr/bin/vim
#export EDITOR=/usr/bin/mcedit

# For some news readers it makes sense to specify the NEWSSERVER variable here
#export NEWSSERVER=your.news.server

# If you want to use a Palm device with Linux, uncomment the two lines below.
# For some (older) Palm Pilots, you might need to set a lower baud rate
# e.g. 57600 or 38400; lowest is 9600 (very slow!)
#
#export PILOTPORT=/dev/pilot
#export PILOTRATE=115200

test -s ~/.alias && . ~/.alias || true

if [ "$PS1" ]; then
	function set_bash_prompt {
		# save rc
		local RC=$?

		function color {
			echo -n "\["
			for k in $*; do
				case $k in
					bold) tput bold 2>/dev/null;;
					none) tput sgr0 2>/dev/null;;
					*) tput setaf $k 2>/dev/null;;
				esac
			done
			echo -n "\]"
		}

		local DARK_RED=$(color 1)
		local DARK_GREEN=$(color 2)
		local BROWN=$(color 3)
		local DARK_BLUE=$(color 4)
		local GRAY=$(color 7)

		local RED=$(color bold 1)
		local GREEN=$(color bold 2)
		local YELLOW=$(color bold 3)
		local BLUE=$(color bold 4)
		local MAGENTA=$(color bold 5)
		local WHITE=$(color bold 7)

		local COLOR_NONE=$(color none)

		function is_git_repository {
			git branch >/dev/null 2>&1
		}

		function set_git_branch {
			# Capture the output of the "git status" command.
			git_status="$(git status 2> /dev/null)"
			 
			# Set color based on clean/staged/dirty.
			status_pattern="working (tree|directory) clean"
			if [[ ${git_status} =~ ${status_pattern} ]]; then
				state="${GREEN}"
			elif [[ ${git_status} =~ "Changes to be committed" ]]; then
				state="${YELLOW}"
			else
				state="${RED}"
			fi

			# Set arrow icon based on status against remote.
			remote_pattern="Your branch is (ahead|behind)"
			if [[ ${git_status} =~ ${remote_pattern} ]]; then
				if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
					remote="${YELLOW}↑"
				elif [[ ${BASH_REMATCH[1]} == "behind" ]]; then
					remote="${YELLOW}↓"
				fi
			else
				remote=""
			fi
			diverge_pattern="Your branch and (.*) have diverged"
			if [[ ${git_status} =~ ${diverge_pattern} ]]; then
				remote="${RED}↕"
			fi

			# Get the name of the branch.
			branch=$(a=$(git rev-parse --abbrev-ref HEAD 2>/dev/null); [[ $? == 0 ]] && echo $a || echo "<nowhere>")
			 
			# Set the final branch string.
			BRANCH="${state}(${branch})${remote}${COLOR_NONE}"
		}

		function set_prompt_symbol () {
			mark="\$"
			if [[ "`id -u`" -eq 0 ]]; then
				mark="#"
			fi

			if [[ $1 -eq 0 ]]; then
				PROMPT_SYMBOL=$mark
			elif [[ $1 -eq 127 ]]; then
				PROMPT_SYMBOL="${MAGENTA}${mark}${COLOR_NONE}"
			else
				PROMPT_SYMBOL="${RED}${mark}${COLOR_NONE}"
			fi
		}

		# Set the PROMPT_SYMBOL variable. We do this first so we don't lose the
		# return value of the last command.
		set_prompt_symbol $RC
		 
		local usercol=$BLUE
		local hostcol=$GREEN
		local dircol=$YELLOW
		if [ "`id -u`" -eq 0 ]; then
			usercol=$RED 
			hostcol=$RED
			dircol=$RED
		fi

		# Set the BRANCH variable.
		if is_git_repository ; then
			set_git_branch
		else
			BRANCH=''
		fi

		# Set the bash prompt variable.
		PS1="\[\$(ppwd \l)\][$(date +%H:%M:%S)] ${usercol}\u${COLOR_NONE}@${hostcol}\h${COLOR_NONE}:${dircol}\w${COLOR_NONE}${BRANCH}${PROMPT_SYMBOL} "
	}

	if [[ "$(type -t ppwd)" != "function" ]]; then
		if [[ "$TERM" =~ xterm ]]; then
			_tsl=$(echo -en '\e]2;')
			_isl=$(echo -en '\e]1;')
			_fsl=$(echo -en '\007')
			_sc=$(tput sc 2>/dev/null)
			_rc=$(tput rc 2>/dev/null)
			TS1="$_sc$_tsl%s@%s:%s$_fsl$_isl%s$_fsl$_rc"
			unset _tsl _isl _fsl _sc _rc
			function ppwd () {
				local dir
				local -i width
				test -n "$TS1" || return;
				dir="$(dirs +0)"
				let width=${#dir}-18
				test ${#dir} -le 18 || dir="...${dir#$(printf "%.*s" $width "$dir")}"
				local host
				host=$(uname -n)
				host=${host//.*/}
				printf "$TS1" "$USER" "$host" "$dir" "$host"
			}
		else
			function ppwd () { true; }
		fi
	fi

	PROMPT_COMMAND=set_bash_prompt

	function rcd () {
      [ "${1:0:1}" == "/" ] && { cd $1; } || { cd $(pwd -P)/$1; }
   }

	alias ll="ls -l"
	alias lr="ls -ltr"
	alias la="ls -al"
	alias egrep='egrep --color=auto'
	alias fgrep='fgrep --color=auto'
	alias grep='grep --color=auto'

fi

# force locale
#export LANG=en_US.UTF-8
#export LANGUAGE=$LANG
#export LC_ALL=$LANG
#export LC_PAPER=$LANG
#export LC_MONETARY=$LANG
#export LC_NUMERIC=$LANG
#export LC_MEASUREMENT=$LANG
#export LC_TIME=$LANG