]> git.immae.eu Git - perso/Immae/Config/Ansible.git/blob - roles/shell/bash/templates/bashrc.j2
Add bash history file timestamp
[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 else
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 fi
84 fi
85 }
86
87 _tmux_dev() {
88 COMPREPLY=()
89 local cur="${COMP_WORDS[COMP_CWORD]}"
90 prefix="$HOME/workdir/"
91 local IFS=$'\n'
92 local items=($(IFS=' ' compgen -W "bash remote puppet" -- $cur) $(compgen -d $prefix$cur))
93 for item in ${items[@]}; do
94 [[ $item == $prefix.* ]] && continue
95 COMPREPLY+=("${item#$prefix}")
96 done
97 }
98 complete -o filenames -F _tmux_dev tmux_dev
99
100 if [ -n "$TMUX" ]; then
101 TMUX_SESSION_NAME=$(tmux display-message -p "#S")
102 export HISTFILE="$XDG_STATE_HOME/bash/tmux_${TMUX_SESSION_NAME}_history"
103 fi
104 ##### /Fretlink
105 {% endif %}
106
107 # Non-printable chars need to be wrapped by \[ ... \] otherwise bash
108 # will wrongly count the size of the prompt and glitch when going
109 # through history
110 function git_prompt_color() {
111 git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
112 gitstatus=$?
113 if [ "$gitstatus" -gt 0 ]; then
114 av=$'\033[01;31m'
115 else
116 av=$'\033[01;34m'
117 fi
118 echo "$av"
119 }
120
121 function git_prompt() {
122 gitinfo=$(git branch 2>/dev/null | grep "^*" | cut -c3-);
123 git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
124 gitstatus=$?
125 gitlsfiles=$(git ls-files . 2>/dev/null | wc -l);
126 if [ "$gitinfo" -a "$gitlsfiles" -gt 0 ]; then
127 echo -e "($gitinfo) "
128 fi
129 }
130 {% set ps1_git = '\[$(git_prompt_color)\]$(git_prompt)' %}
131
132 function error_code() {
133 ret=$?
134 if [[ $ret != 0 ]]; then
135 echo "$ret "
136 fi
137 }
138
139 function running_jobs() {
140 ret=$(jobs | wc -l)
141 if [[ $ret != 0 ]]; then
142 echo "[$ret] "
143 fi
144 }
145 {% set ps1_status = '\[\\033[01;31m\]$(error_code)\[\\033[00;00m\]$(running_jobs)' -%}
146
147 {# Ensure new line before the prompt #}
148 {% set ps1_newline = '$(printf "%$((`tput cols`-1))s\\r")' -%}
149
150 {% set ps1_other = '' %}
151 {% if role.get(profile) == "fretlink" -%}
152 {% set ps1_other = '\[\\033[01;32m\]$(dev_prompt)' %}
153 {%- endif %}
154
155 PS1='{{ ps1_newline }}{{ ps1_status }}{{ ps1_other }}{{ ps1_git }}\[\033[00;32m\]\u@\h\[\033[94m\] \w \$\[\033[00m\] '
156
157 PROMPT_COMMAND="[[ -f $XDG_STATE_HOME/bash/bashrc_temp ]] && . $XDG_STATE_HOME/bash/bashrc_temp"
158
159 {% if "nodejs" in features.get(profile, []) %}
160 source /usr/share/nvm/init-nvm.sh
161 {% endif %}