about summary refs log tree commit diff
path: root/.bashrc
blob: b1d2c7b177ef2de75e36afd5eb75d20bffa2a4b8 (plain)
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
# Source Default /etc/bashrc
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Add .local/bin to PATH
if ! [[ "$PATH" =~ "$HOME/.local/bin:" ]]; then
    PATH="$HOME/.local/bin:$PATH"
fi
export PATH

# Custom Shell variables, aliases, and functions
[ -f "$HOME/.config/shell/alias" ] && source "$HOME/.config/shell/alias"
[ -f "$HOME/.config/shell/vars" ] && source "$HOME/.config/shell/vars"
[ -f "$HOME/.config/shell/functions" ] && source "$HOME/.config/shell/functions"

# Custom Prompt
USER_COLOR="\[\e[35m\]"          # magenta
VENV_COLOR="\[\e[33m\]"          # yellow
DIR_COLOR="\[\e[32m\]"           # green
GIT_COLOR="\[\e[34m\]"           # blue
GIT_DIRTY_COLOR="\[\e[31m\]"     # red
PROMPT_SYMBOL_COLOR="\[\e[33m\]" # yellow
RESET_COLOR="\[\e[0m\]"          # blank
WHITE_COLOR="\[\e[37m\]"         # white
TOOLBOX_COLOR="\[\e[36m\]"       # cyan

shorten_path() {
  if [[ "$PWD" == "$HOME" ]]; then
    echo "~"
    return
  fi

  if [[ "$PWD" == "$HOME/"* ]]; then
    local rel="${PWD#$HOME/}"
    local depth
    IFS='/' read -ra parts <<< "$rel"
    depth=${#parts[@]}

    if (( depth == 1 )); then
      echo "~/${parts[0]}"
    elif (( depth == 2 )); then
      echo ".../${parts[0]}/${parts[1]}"
    else
      echo ".../${parts[-2]}/${parts[-1]}"
    fi
    return
  fi

  IFS='/' read -ra parts <<< "$PWD"
  if (( ${#parts[@]} > 3 )); then
    echo ".../${parts[-2]}/${parts[-1]}"
  else
    echo "$PWD"
  fi
}

git_branch() {
  git symbolic-ref --short HEAD 2>/dev/null \
    || git rev-parse --short HEAD 2>/dev/null
}

git_dirty() {
  if [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then
    echo " ✗"
  else
    echo " ✓"
  fi
}

set_prompt() {
  local user="${USER_COLOR}\u${RESET_COLOR}"

  local toolbox=""
  if [[ -n "$TOOLBOX_PATH" ]]; then
    toolbox="${TOOLBOX_COLOR}(tlbx)${RESET_COLOR}"
  fi

  local venv=""
  if [[ -n "$VIRTUAL_ENV" ]]; then
    venv="${VENV_COLOR}($(basename "$VIRTUAL_ENV"))${RESET_COLOR} "
  fi

  local path="${DIR_COLOR}$(shorten_path)${RESET_COLOR}"
  local git_info=""
  local branch="$(git_branch)"

  if [[ -n "$branch" ]]; then
    git_info=" ${WHITE_COLOR}on${RESET_COLOR} ${GIT_COLOR}${branch}${RESET_COLOR}${GIT_DIRTY_COLOR}$(git_dirty)${RESET_COLOR}"
  fi

  PS1="${user}${toolbox} ${WHITE_COLOR}in${RESET_COLOR} ${venv}${path}${git_info}\n${PROMPT_SYMBOL_COLOR}λ${RESET_COLOR} "
}

PROMPT_COMMAND="set_prompt${PROMPT_COMMAND:+; $PROMPT_COMMAND}"