Better bash prompt
[pkgusr] / etc / pkgusr / bash_profile
1 # -*- shell-script -*-
2 ## Ensure sanity
3 set +h
4 umask 022
5
6 ## We live in a unicode world now
7 LANG=en_AU.UTF-8
8 LC_CTYPE=en_AU.UTF-8
9
10 ## PATH
11 # The wrappers directory must be the first entry in the PATH, and
12 # /tools/bin should be last.
13 #
14 # Once you are no longer needing or using the tools dir you can remove
15 # it from the PATH.
16 #PATH=/usr/lib/pkgusr:/usr/bin:/usr/X11R6/bin:/opt/qt/bin:/tools/bin
17 PATH=/usr/lib/pkgusr:/usr/bin:/usr/X11R6/bin:/opt/qt/bin
18
19 ## A couple things to make less(1) nicer.
20 LESS='-FgiMrswX --use-color'
21 LESSCHARSET=utf-8
22 LESSOPEN='|lesspipe.sh %s'
23
24 ## Timezone -- set to your local zone
25 TZ='Australia/Brisbane'
26
27 ## QT
28 QTDIR=/opt/qt
29
30 ## pkg-config
31 _XORG=/usr/X11R6/lib/pkgconfig:/usr/X11R6/share/pkgconfig
32 _KDE=/opt/kde/lib/pkgconfig
33 _QT=${QTDIR}/lib/pkgconfig
34 _PKGCFG=$(pkg-config --variable pc_path pkg-config)
35 PKG_CONFIG_PATH=${_PKGCFG}:${_XORG}:${_KDE}:${_QT}
36
37 # unset the tmp vars
38 unset _XORG _KDE _QT _PKGCFG
39
40 ## Locale directory suppression.
41 #  If this is set to `1' (one) then the install and mkdir wrappers
42 #  won't put anything in /usr/share/locale.  Technically, the mkdir
43 #  wrapper still creates the dir, but it is removed afterward.  If
44 #  you want to override this behaviour globally, set this to `0'
45 #  (zero) here, or you can override an individual package by adding
46 #  `SUPPRESSLOCALEDIR=0' to that pkgusr's ~/.pkgusrrc.
47 #
48 #  See the comments in the install wrapper for more detail and
49 #  rationale.
50 SUPPRESSLOCALEDIR=1
51
52 ## Build script update checks
53 #  If this is set to `1' (one) a check is done to see if there is a
54 #  newer build script available that the pkgusr could update to.  If an
55 #  update is available a message is printed to stdout with instructions
56 #  of how to proceed.
57 #
58 #  Override this in ~/.pkgusrrc.
59 CHECKUPDATES=1
60
61 ## Slackware PKG local repo
62 #  Having a shortcut to a local Slackware PKG repo is very useful.
63 #  Pat Volkerding and his team are of a minority who actually know
64 #  what they are doing.  I always check Slackware for patches.
65 #
66 #  I get the Slack source via rsync.
67 #  See https://mirrors.slackware.com/mirrorlist/
68 #
69 SLACKPKG=/home/steve/download/Slackware/slackware64-current/source
70 #  Obviously edit to suit your situation
71
72 ## Export everything
73 export LANG LC_CTYPE PATH LESS LESSCHARSET LESSOPEN TZ PKG_CONFIG_PATH QTDIR
74 export SUPPRESSLOCALEDIR CHECKUPDATES SLACKPKG
75
76 ## Bash Prompt
77 #  Shell prompts are not just eye-candy they should show what you need
78 #  to know most and they should do it in such a way that you don't
79 #  even notice.  This sets up a two-line prompt that reminds you that
80 #  you're currently logged in as a pkgusr, which pkgusr, which group
81 #  if that pkgusr has multiple groups and you've newgrp'd, if you're
82 #  inside a git repo display the branch and other pertinent info, if
83 #  the previous command had a non-zero exit display the code, display
84 #  the current time, and current working directory (truncated to
85 #  prevent "prompt-creep").  Colourised so that often you won't even
86 #  need to read it to take in the info.
87
88 # Colours (doesn't include black (30))
89 CO="\[\e[0m\]"        # Colour Off
90 RED="\[\e[0;31m\]"    # Red
91 GRN="\[\e[0;32m\]"    # Green
92 YEL="\[\e[0;33m\]"    # Yellow
93 BLU="\[\e[0;34m\]"    # Blue
94 MAG="\[\e[0;35m\]"    # Magenta
95 CYN="\[\e[0;36m\]"    # Cyan
96 WTE="\[\e[0;37m\]"    # White
97 # Bold (bright)
98 BRED="\[\e[1;31m\]"   # Bold Red
99 BGRN="\[\e[1;32m\]"   # Bold Green
100 BYEL="\[\e[1;33m\]"   # Bold Yellow
101 BBLU="\[\e[1;34m\]"   # Bold Blue
102 BMAG="\[\e[1;35m\]"   # Bold Magenta
103 BCYN="\[\e[1;36m\]"   # Bold Cyan
104 BWTE="\[\e[1;37m\]"   # Bold White
105
106 # Some pkgusrs may use extra groups so the prompt should reflect that.
107 # This function adds the group name to the prompt if necessary.
108 _sg()
109 {
110     [[ $(id -gn) != $(id -un) ]] && echo "$WTE:$CYN$(id -gn)"
111 }
112
113 # This function, called from $PROMPT_COMMAND, puts all the pieces of
114 # the prompt together.
115 make_prompt()
116 {
117     rc=$?                       # Return code from previous cmd
118     [[ $rc -gt 0 ]] && prc="$BWTE[$BRED$rc$BWTE] " || prc=
119
120     local sg gitp p
121
122     # Using a subgroup
123     sg=$(_sg)
124
125     # Git
126     [[ -f /etc/pkgusr/git-prompt ]] && gitp=$(__git_ps1) || gitp=
127
128     p="pkgusr"
129
130     # Build up the PS1
131     PS1="$BWTE[$WTE$p $BWTE($CYN\u$sg$BWTE)]"  # [pkgusr (usr:grp)]
132     PS1+="$CO"                                 # reset to not mess up git prompt
133     PS1+="$gitp"                               # (gitstuff)
134     PS1+="\n$prc$BYEL\A$WTE:$BMAG\w$BWTE>$CO " # [rc]TIME:CWD>
135 }
136
137 # The git goodies
138 [[ -f /etc/pkgusr/git-prompt ]] && source /etc/pkgusr/git-prompt
139 GIT_PS1_SHOWDIRTYSTATE=true
140 GIT_PS1_SHOWSTASHSTATE=true
141 GIT_PS1_SHOWUNTRACKEDFILES=true
142 GIT_PS1_SHOWUPSTREAM=auto
143 GIT_PS1_DESCRIBE_STYLE=branch
144 GIT_PS1_SHOWCOLORHINTS=true
145
146 # So the prompt doesn't fly off the right-hand edge of the screen
147 # when you're down a long directory tree.
148 PROMPT_DIRTRIM=4
149
150 # Finally, make the prompt happen!
151 PROMPT_COMMAND=make_prompt
152
153
154 ## Pretty colours for ls
155 eval $(dircolors -b)
156 alias ls='ls --color=always -Fb'
157
158 ## Suck in some handy shell functions
159 . /etc/pkgusr/handy_funcs
160
161 ## Go to the home directory whenever we su to a package user.
162 cd
163
164 ## Setup the git config if needed
165 if [[ ! -d ${HOME}/.config/git && -f ${HOME}/.gitconfig ]]; then
166     prep_git
167 fi
168
169 ## Bash Completion
170 if [ -f /usr/share/bash-completion/bash_completion ]; then
171     . /usr/share/bash-completion/bash_completion
172 fi
173
174 ## Per-user settings
175 #  Called last so that defaults can be overridden.
176 [[ -f ${HOME}/.pkgusrrc ]] && . ${HOME}/.pkgusrrc
177
178 ###===============================================================###
179 # Don't put anything below here unless it relies on something being #
180 # set in ~/.pkgusrrc                                                #
181 ###===============================================================###
182
183 # Maybe check if the build script can be updated, but not on dumb
184 # terms so that TRAMP still works.
185 if [ ${CHECKUPDATES} -eq 1 -a "$TERM" != "dumb" ]; then
186     checkupdates
187 fi
188
189
190 # Local variables:
191 # sh-basic-offset: 4
192 # End: