summaryrefslogtreecommitdiff
path: root/roles/shell/bash/templates/bashrc.j2
blob: 1bf94e7d3064f90a5bba7d94c44f0f80a0206d15 (plain) (blame)
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
#
# ~/.bashrc
#

[[ -f "$XDG_STATE_HOME/bash/bashrc_temp" ]] && . $XDG_STATE_HOME/bash/bashrc_temp

if [ -n "$HOME/.local/bin" ]; then
  PATH="$HOME/.local/bin:$PATH"
fi

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

export GPG_TTY=$(tty)
export HISTFILE="$XDG_STATE_HOME/bash/history"
export HISTTIMEFORMAT='%F %T '
export HISTCONTROL="ignorespace:ignoredups"

alias vi=vim
alias ls='ls --color=auto -F -T 0'
alias tmux='tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf'

alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

alias sudo="sudo "
alias mv="mv -i"
alias cp="cp -i"
alias rm="rm -i"
alias psp="pstree -aUp $USER"
alias info='info -v active-link-style=yellow,bold -v link-style=yellow'

if [[ -d "$XDG_DATA_HOME/bash_completion" ]]; then
  for i in $XDG_DATA_HOME/bash_completion/*; do
    [[ -f "$i" && -r "$i" ]] && . "$i"
  done
fi

{% if role.get(profile) == "fretlink" %}
##### Some Fretlink aliases
alias vpn=fl_vpn
alias tmux_dev=fl_tmux_dev
complete -o filenames -F _fl_tmux_dev tmux_dev

function dev_prompt() {
  if [ -n "$FRETLINK_ENV" ]; then
    echo "{$FRETLINK_ENV} "
  fi
}

if [ -n "$TMUX" ]; then
  TMUX_SESSION_NAME=$(tmux display-message -p "#S")
  export HISTFILE="$XDG_STATE_HOME/bash/tmux_${TMUX_SESSION_NAME}_history"
fi

function load_dev_env() {
  if [ -z "$1" ]; then
    source ../environment
  else
    FL_ENV="$1" source ../environment
  fi
}

##### /Fretlink
{% endif %}

# Non-printable chars need to be wrapped by \[ ... \] otherwise bash
# will wrongly count the size of the prompt and glitch when going
# through history
function git_prompt_color() {
  git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
  gitstatus=$?
  if [ "$gitstatus" -gt 0 ]; then
    av=$'\033[01;31m'
  else
    av=$'\033[01;34m'
  fi
  echo "$av"
}

function git_prompt() {
  gitinfo=$(git branch 2>/dev/null | grep "^*" | cut -c3-);
  git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
  gitstatus=$?
  gitlsfiles=$(git ls-files . 2>/dev/null | wc -l);
  if [ "$gitinfo" -a "$gitlsfiles" -gt 0 ]; then
    echo -e "($gitinfo) "
  fi
}
{% set ps1_git = '\[$(git_prompt_color)\]$(git_prompt)' %}

function running_jobs() {
  ret=$(jobs | wc -l)
  if [[ $ret != 0  ]]; then
    echo "[$ret] "
  fi
}

PROMPT_COMMAND=__prompt_command

__prompt_command () {
  local ret="$?"
  PS1=""
  d="\[\033[00;00m\]"
  red="\[\033[01;31m\]"
  bgreen="\[\033[01;32m\]"
  green="\[\033[00;32m\]"
  blue="\[\033[94m\]"

  # Ensure new line before the prompt
  PS1='$(printf "%$((`tput cols`-1))s\r")'
  if [[ $ret != 0  ]]; then
    PS1+="$red$ret $d"
  fi
  PS1+='$(running_jobs)'
{% if role.get(profile) == "fretlink" %}
  PS1+="$bgreen"'$(dev_prompt)'
{% endif %}
  PS1+='\[$(git_prompt_color)\]$(git_prompt)'
  PS1+="$green\u@\h$blue \w \$$d "
}

source /usr/share/fzf/key-bindings.bash
source /usr/share/fzf/completion.bash
export FZF_COMPLETION_TRIGGER='#'

{% if "nodejs" in features.get(profile, []) %}
source /usr/share/nvm/init-nvm.sh
{% endif %}