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