/ bashrc
bashrc
1 ######################################################################## 2 # Colour definitions for parts of $PS1. 3 4 # Do we even have a terminal we can do things to? 5 we_have_term() { 6 [ "$TERM" ] && [ "$TERM" != dumb ] 7 } 8 9 raw_termattr() { 10 if we_have_term; then 11 for attr in "$@"; do 12 case "$attr" in 13 normal) 14 tput sgr0 15 ;; 16 red) 17 tput setaf 1 18 ;; 19 blue) 20 tput setaf 4 21 ;; 22 bold) 23 tput bold 24 ;; 25 esac 26 done 27 fi 28 } 29 30 # Output terminal attribute changing sequence, if there's a terminal. 31 # Also output output bash $PS1 sequences to tell bash it's 32 # non-printing characters. 33 termattr() { 34 printf '\[' 35 raw_termattr "$@" 36 printf '\]' 37 } 38 39 ######################################################################## 40 # Collect info to show in $PS1 41 42 find_git_root() { 43 ( 44 set -e 45 while [ "$(pwd)" != / ]; do 46 if [ -d .git ]; then 47 pwd 48 break 49 else 50 cd .. 51 fi 52 done 53 ) 54 } 55 56 is_git_repo() { 57 case "$(find_git_root)" in 58 /*) return 0 ;; 59 *) return 1 ;; 60 esac 61 } 62 63 get_git_branch() { 64 git branch --no-color | 65 awk ' 66 BEGIN { max = 30 } 67 /^\* / { 68 name = $2 69 if (length(name) > max) 70 name = "..." substr(name, length(name) - max) 71 print name 72 } 73 ' 74 } 75 76 ######################################################################## 77 # Set window title for terminal emulator, screen. 78 79 set_window_title() { 80 case "$TERM" in 81 xterm*) 82 printf '\033]1;%s\07' "$1" >/dev/tty 83 printf '\033]2;%s\07' "$1" >/dev/tty 84 ;; 85 screen) 86 printf '\ek%s\e\\' "$1" >/dev/tty 87 ;; 88 esac 89 } 90 91 ######################################################################## 92 # Dynamically change $PS1 or output other things just before the prompt. 93 94 wrap_if_nonempty() { 95 local thing="$1" 96 local before="$2" 97 local after="$3" 98 99 if [ -n "$thing" ]; then 100 printf '%s%s%s' "$before" "$thing" "$after" 101 fi 102 } 103 104 define_ps1() { 105 termattr bold blue 106 date +%H:%M | tr -d '\n' 107 local hostname 108 hostname="$(hostname)" 109 case "$hostname" in 110 exolobe3) 111 printf ' \w ' 112 ;; 113 *) 114 termattr red 115 printf ' %s' "$hostname" 116 termattr bold blue 117 printf ':\w ' 118 ;; 119 esac 120 termattr normal 121 122 if is_git_repo; then 123 wrap_if_nonempty "$(get_git_branch)" "$(termattr bold blue)[" "]$(termattr normal)" 124 fi 125 126 termattr bold blue 127 printf '\$ ' 128 termattr normal 129 } 130 131 define_window_title() { 132 printf "%s:" "$(hostname)" 133 pwd | sed "s,$HOME,~," 134 } 135 136 tell_about_exit_code() { 137 local exit_code="$1" 138 if [ "$exit_code" != 0 ]; then 139 raw_termattr bold red 140 printf '[PREVIOUS COMMAND EXIT: %s]\n' "$exit_code" 141 raw_termattr normal 142 fi 143 } 144 145 prompt_set_window_title() { 146 set_window_title "$(define_window_title)" 147 } 148 prompt_command() { 149 tell_about_exit_code "$?" 150 PS1="$(define_ps1)" 151 prompt_set_window_title 152 } 153 154 ############### 155 156 ulimit -c 0 157 umask 002 158 159 # Get environment variables from systemd, if any are set. 160 if systemctl --user show-environment >/dev/null 2>&1; then 161 . <(systemctl --user show-environment | sed 's/^/export /') 162 fi 163 164 if command -v emacs >/dev/null; then 165 export EDITOR=emacs 166 else 167 export EDITOR=vi 168 fi 169 export PATH="/scratch/dist/rust/bin:$HOME/dist/rust/bin:$HOME/dist/rust/bin:$HOME/.cargo/bin:$PATH:$HOME/bin:$HOME/.local/bin:$HOME/.radicle/bin:/usr/games" 170 export PAGER=less 171 export LESSCHARSET=utf-8 172 export HISTCONTROL=ignoredups 173 export HISTFILESIZE=500 174 export DEBEMAIL=liw@liw.fi 175 export VIRSH_DEFAULT_CONNECT_URI=qemu:///system 176 export LANG=en_US.UTF-8 177 export LC_COLLATE=fi_FI.UTF-8 178 export LC_TIME=en_GB 179 export ANSIBLE_ROLES_PATH="$HOME/pers/puomi/git/roles:$HOME/pers/debian-ansible/roles" 180 export PYTHONDONTWRITEBYTECODE=1 181 export STARSHIP_CONFIG=~/.config/starship/config.toml 182 unset HISTFILE 183 184 if [ -e /scratch/cargo-cache ]; then 185 export CARGO_TARGET_DIR="/scratch/cargo-cache" 186 elif [ -e "$HOME/tmp/cargo" ]; then 187 export CARGO_TARGET_DIR="$HOME/tmp/cargo" 188 elif [ -e "$HOME/cargo" ]; then 189 export CARGO_TARGET_DIR="$HOME/cargo" 190 fi 191 192 PROMPT_DIRTRIM=1 193 194 set +H 195 if [ -n "$PS1" ]; then 196 bind -r '' 197 fi 198 199 gotcmd() { 200 command -v "$1" >/dev/null 201 } 202 203 if gotcmd starship; then 204 export PROMPT_COMMAND=prompt_set_window_title 205 eval "$(starship init bash)" 206 else 207 export PROMPT_COMMAND=prompt_command 208 fi 209 210 if [ -e "$HOME/.cargo/env" ]; then 211 . "$HOME/.cargo/env" 212 fi 213 214 if command -v pathdedup >/dev/null; then 215 PATH="$(pathdedup)" 216 fi 217 218 if gotcmd zoxide; then 219 eval "$(zoxide init --hook prompt bash)" 220 fi