]> git.immae.eu Git - perso/Immae/Config/Ansible.git/blame - roles/shell/bash/templates/bashrc.j2
Make bash xdg-compliant
[perso/Immae/Config/Ansible.git] / roles / shell / bash / templates / bashrc.j2
CommitLineData
397c4fed
IB
1#
2# ~/.bashrc
3#
4
a19180df 5[[ -f "$XDG_STATE_HOME/bash/bashrc_temp" ]] && . $XDG_STATE_HOME/bash/bashrc_temp
c235f9be 6
397c4fed
IB
7if [ -n "$HOME/.local/bin" ]; then
8 PATH="$HOME/.local/bin:$PATH"
9fi
10
11# If not running interactively, don't do anything
12[[ $- != *i* ]] && return
13
14export GPG_TTY=$(tty)
15export HISTFILE="$XDG_STATE_HOME/bash/history"
16
17alias vi=vim
18alias ls='ls --color=auto -F -T 0'
19alias tmux='tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf'
20
21alias grep='grep --color=auto'
22alias fgrep='fgrep --color=auto'
23alias egrep='egrep --color=auto'
24
25alias sudo="sudo "
26alias mv="mv -i"
27alias cp="cp -i"
28alias rm="rm -i"
29alias psp="pstree -aUp $USER"
853c1445 30alias info='info -v active-link-style=yellow,bold -v link-style=yellow'
397c4fed 31
ae3ee401
IB
32[[ -f /usr/share/stgit/completion/stgit-completion.bash ]] && . /usr/share/stgit/completion/stgit-completion.bash
33
397c4fed
IB
34{% if role.get(profile) == "fretlink" %}
35##### Some Fretlink aliases
36alias getDump='s3cmd -c <(pass show Travail/Fretlink/S3) get '
37
38function vpn() {
39 TMPFILE=$(mktemp /tmp/openvpn.XXXXXX)
40 pass show Travail/Fretlink/OpenVPN >> $TMPFILE
41 sudo openvpn $TMPFILE
42 rm -f $TMPFILE
43}
44
45function dev_prompt() {
46 if [ -n "$FRETLINK_ENV" ]; then
47 echo "{$FRETLINK_ENV} "
48 fi
49}
de1c634c
IB
50
51function tmux_dev() {
52 type="${1:-bash}"
53 if [ -n "$TMUX" ]; then
54 m=switch-client
55 else
56 m=attach-session
57 fi
58
a65169cc
IB
59 case "$type" in
60 "bash") start_dir="$HOME";;
1f22cde1 61 "remote") start_dir="$HOME";;
a65169cc
IB
62 "puppet") start_dir="/etc/puppetlabs/code"
63 esac
64
de1c634c
IB
65 if tmux has-session -t "$type" 2>/dev/null; then
66 tmux $m -t "$type"
67 else
1f22cde1
IB
68 if [ "$type" = "remote" ]; then
69 tmux new-session -d -c $start_dir -s "$type" mosh immae.eu \; \
70 $m -t "$type"
71 elif [ -n "$start_dir" ]; then
a65169cc
IB
72 tmux new-session -d -c $start_dir -s "$type" \; \
73 $m -t "$type" \; \
74 new-window -c $start_dir
de1c634c
IB
75 else
76 tmux new-session -d -c "$HOME/workdir/$type" -s "$type" \; \
77 $m -t "$type" \; \
78 send-keys "../start" C-m \; \
79 rename-window "running" \; \
80 new-window -c "$HOME/workdir/$type"
81 fi
82 fi
83}
84
85_tmux_dev() {
86 COMPREPLY=()
87 local cur="${COMP_WORDS[COMP_CWORD]}"
88 prefix="$HOME/workdir/"
89 local IFS=$'\n'
1f22cde1 90 local items=($(IFS=' ' compgen -W "bash remote puppet" -- $cur) $(compgen -d $prefix$cur))
de1c634c
IB
91 for item in ${items[@]}; do
92 [[ $item == $prefix.* ]] && continue
93 COMPREPLY+=("${item#$prefix}")
94 done
95}
96complete -o filenames -F _tmux_dev tmux_dev
bfe7d9cb
IB
97
98if [ -n "$TMUX" ]; then
99 TMUX_SESSION_NAME=$(tmux display-message -p "#S")
100 export HISTFILE="$XDG_STATE_HOME/bash/tmux_${TMUX_SESSION_NAME}_history"
101fi
397c4fed
IB
102##### /Fretlink
103{% endif %}
104
105# Non-printable chars need to be wrapped by \[ ... \] otherwise bash
106# will wrongly count the size of the prompt and glitch when going
107# through history
108function git_prompt_color() {
109 git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
110 gitstatus=$?
111 if [ "$gitstatus" -gt 0 ]; then
112 av=$'\033[01;31m'
113 else
114 av=$'\033[01;34m'
115 fi
116 echo "$av"
117}
118
119function git_prompt() {
120 gitinfo=$(git branch 2>/dev/null | grep "^*" | cut -c3-);
121 git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
122 gitstatus=$?
123 gitlsfiles=$(git ls-files . 2>/dev/null | wc -l);
124 if [ "$gitinfo" -a "$gitlsfiles" -gt 0 ]; then
125 echo -e "($gitinfo) "
126 fi
127}
853c1445 128{% set ps1_git = '\[$(git_prompt_color)\]$(git_prompt)' %}
397c4fed
IB
129
130function error_code() {
131 ret=$?
132 if [[ $ret != 0 ]]; then
133 echo "$ret "
134 fi
135}
136
137function running_jobs() {
138 ret=$(jobs | wc -l)
139 if [[ $ret != 0 ]]; then
140 echo "[$ret] "
141 fi
142}
853c1445 143{% set ps1_status = '\[\\033[01;31m\]$(error_code)\[\\033[00;00m\]$(running_jobs)' -%}
397c4fed 144
853c1445
IB
145{# Ensure new line before the prompt #}
146{% set ps1_newline = '$(printf "%$((`tput cols`-1))s\\r")' -%}
147
148{% set ps1_other = '' %}
149{% if role.get(profile) == "fretlink" -%}
150 {% set ps1_other = '\[\\033[01;32m\]$(dev_prompt)' %}
151{%- endif %}
152
153PS1='{{ ps1_newline }}{{ ps1_status }}{{ ps1_other }}{{ ps1_git }}\[\033[00;32m\]\u@\h\[\033[94m\] \w \$\[\033[00m\] '
397c4fed 154
853c1445 155PROMPT_COMMAND="[[ -f $XDG_STATE_HOME/bash/bashrc_temp ]] && . $XDG_STATE_HOME/bash/bashrc_temp"
a19180df 156
397c4fed
IB
157{% if "nodejs" in features.get(profile, []) %}
158source /usr/share/nvm/init-nvm.sh
159{% endif %}