From f20d62d1e2a658c98141fe71ccea03f92725277f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 13 Sep 2021 14:04:35 +0300 Subject: bashrc: Set CARGO_TARGET_DIR if ~/tmp/cargo or ~/cargo exist Sponsored-by: author --- bashrc | 253 +++++++++++++++++++++++++++++------------------------------------ 1 file changed, 111 insertions(+), 142 deletions(-) (limited to 'bashrc') diff --git a/bashrc b/bashrc index e430dba..ec4078d 100644 --- a/bashrc +++ b/bashrc @@ -1,86 +1,68 @@ ######################################################################## # Colour definitions for parts of $PS1. - # Do we even have a terminal we can do things to? -we_have_term() -{ - [ "$TERM" ] && [ "$TERM" != dumb ] +we_have_term() { + [ "$TERM" ] && [ "$TERM" != dumb ] } - -raw_termattr() -{ - if we_have_term - then - for attr in "$@" - do - case "$attr" in - normal) - tput sgr0 - ;; - red) - tput setaf 1 - ;; - blue) - tput setaf 4 - ;; - bold) - tput bold - ;; - esac - done - fi +raw_termattr() { + if we_have_term; then + for attr in "$@"; do + case "$attr" in + normal) + tput sgr0 + ;; + red) + tput setaf 1 + ;; + blue) + tput setaf 4 + ;; + bold) + tput bold + ;; + esac + done + fi } - # Output terminal attribute changing sequence, if there's a terminal. # Also output output bash $PS1 sequences to tell bash it's # non-printing characters. -termattr() -{ - printf '\[' - raw_termattr "$@" - printf '\]' +termattr() { + printf '\[' + raw_termattr "$@" + printf '\]' } - - ######################################################################## # Collect info to show in $PS1 - -find_git_root() -{ - ( - set -e - while [ "$(pwd)" != / ] - do - if [ -d .git ] - then - pwd - break - else - cd .. - fi - done - ) +find_git_root() { + ( + set -e + while [ "$(pwd)" != / ]; do + if [ -d .git ]; then + pwd + break + else + cd .. + fi + done + ) } - -is_git_repo() -{ - case "$(find_git_root)" in - /*) return 0 ;; - *) return 1 ;; - esac +is_git_repo() { + case "$(find_git_root)" in + /*) return 0 ;; + *) return 1 ;; + esac } - -get_git_branch() -{ - git branch --no-color | - awk ' +get_git_branch() { + git branch --no-color | + awk ' BEGIN { max = 30 } /^\* / { name = $2 @@ -91,108 +73,90 @@ get_git_branch() ' } - ######################################################################## # Set window title for terminal emulator, screen. -set_window_title() -{ - case "$TERM" in - xterm*) - printf '\033]1;%s\07' "$1" > /dev/tty - printf '\033]2;%s\07' "$1" > /dev/tty - ;; - screen) - printf '\ek%s\e\\' "$1" > /dev/tty - ;; - esac +set_window_title() { + case "$TERM" in + xterm*) + printf '\033]1;%s\07' "$1" >/dev/tty + printf '\033]2;%s\07' "$1" >/dev/tty + ;; + screen) + printf '\ek%s\e\\' "$1" >/dev/tty + ;; + esac } - - ######################################################################## # Dynamically change $PS1 or output other things just before the prompt. +wrap_if_nonempty() { + local thing="$1" + local before="$2" + local after="$3" -wrap_if_nonempty() -{ - local thing="$1" - local before="$2" - local after="$3" - - if [ -n "$thing" ] - then - printf '%s%s%s' "$before" "$thing" "$after" - fi + if [ -n "$thing" ]; then + printf '%s%s%s' "$before" "$thing" "$after" + fi } - -define_ps1() -{ - termattr bold blue - date +%H:%M | tr -d '\n' - local hostname="$(hostname)" - case "$hostname" in - exolobe3) - printf ' \w ' - ;; - *) - termattr red - printf ' %s' "$hostname" - termattr bold blue - printf ':\w ' - ;; - esac - termattr normal - - if is_git_repo - then - wrap_if_nonempty "$(get_git_branch)" "$(termattr bold blue)[" "]$(termattr normal)" - fi - - termattr bold blue - printf '\$ ' - termattr normal +define_ps1() { + termattr bold blue + date +%H:%M | tr -d '\n' + local hostname="$(hostname)" + case "$hostname" in + exolobe3) + printf ' \w ' + ;; + *) + termattr red + printf ' %s' "$hostname" + termattr bold blue + printf ':\w ' + ;; + esac + termattr normal + + if is_git_repo; then + wrap_if_nonempty "$(get_git_branch)" "$(termattr bold blue)[" "]$(termattr normal)" + fi + + termattr bold blue + printf '\$ ' + termattr normal } - -define_window_title() -{ - printf "%s:" "$(hostname)" - pwd | sed "s,$HOME,~," +define_window_title() { + printf "%s:" "$(hostname)" + pwd | sed "s,$HOME,~," } - -tell_about_exit_code() -{ - local exit_code="$1" - if [ "$exit_code" != 0 ] - then - raw_termattr bold red - printf '[PREVIOUS COMMAND EXIT: %s]\n' "$exit_code" - raw_termattr normal - fi +tell_about_exit_code() { + local exit_code="$1" + if [ "$exit_code" != 0 ]; then + raw_termattr bold red + printf '[PREVIOUS COMMAND EXIT: %s]\n' "$exit_code" + raw_termattr normal + fi } - export PROMPT_COMMAND=prompt_command -prompt_command() { - tell_about_exit_code "$?" - PS1="$(define_ps1)" - set_window_title "$(define_window_title)" +prompt_command() { + tell_about_exit_code "$?" + PS1="$(define_ps1)" + set_window_title "$(define_window_title)" } - ############### ulimit -c unlimited umask 002 -if command -v emacs > /dev/null -then - export EDITOR=emacs +if command -v emacs >/dev/null; then + export EDITOR=emacs else - export EDITOR=vi + export EDITOR=vi fi export PATH="$HOME/.cargo/bin:$PATH:$HOME/bin" export PAGER=less @@ -207,10 +171,15 @@ export ANSIBLE_ROLES_PATH="$HOME/code/debian-ansible/roles:$HOME/pers/debian-ans export ANSIBLE_STDOUT_CALLBACK=yaml export PYTHONDONTWRITEBYTECODE=1 +if [ -e "$HOME/tmp/cargo" ]; then + export CARGO_TARGET_DIR="$HOME/tmp/cargo" +elif [ -e "$HOME/cargo" ]; then + export CARGO_TARGET_DIR="$HOME/cargo" +fi + PROMPT_DIRTRIM=1 set +H -if [ -n "$PS1" ] -then - bind -r '' +if [ -n "$PS1" ]; then + bind -r '' fi -- cgit v1.2.1