|
1 |
| -[[ -s ~/.bashrc ]] && source ~/.bashrc |
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +function __init { |
| 4 | + function __source { |
| 5 | + if [ -f "$1" ]; then |
| 6 | + # shellcheck source=/dev/null |
| 7 | + source "$1" |
| 8 | + fi |
| 9 | + } |
| 10 | + |
| 11 | + function __init_aliases { |
| 12 | + function __reattach_to_user_namespace { |
| 13 | + local -a apps=( |
| 14 | + vim |
| 15 | + mvim |
| 16 | + pbcopy |
| 17 | + pbcopy |
| 18 | + ) |
| 19 | + for app in ${apps[*]}; do |
| 20 | + eval "alias $app=\"reattach-to-user-namespace $app\"" |
| 21 | + done; |
| 22 | + } |
| 23 | + if ! [ -z "$TMUX" ]; then |
| 24 | + __reattach_to_user_namespace |
| 25 | + fi |
| 26 | + |
| 27 | + alias lock="pmset displaysleepnow" |
| 28 | + } |
| 29 | + |
| 30 | + function __init_shopt { |
| 31 | + local -a options=( |
| 32 | + nocaseglob |
| 33 | + histappend |
| 34 | + cdspell |
| 35 | + autocd |
| 36 | + globstar |
| 37 | + ) |
| 38 | + for option in ${options[*]}; do |
| 39 | + shopt -s "$option" 2> /dev/null; |
| 40 | + done; |
| 41 | + } |
| 42 | + |
| 43 | + function __eval_dircolors { |
| 44 | + local dircolors="$HOME/.dircolors" |
| 45 | + if [ -f "$dircolors" ]; then |
| 46 | + eval "$("gdircolors" "-b" "$dircolors")" |
| 47 | + fi |
| 48 | + unset "dircolors" |
| 49 | + alias ls="gls --color=always -F" |
| 50 | + } |
| 51 | + |
| 52 | + function __init_exports { |
| 53 | + export HOMEBREW_GITHUB_API_TOKEN= |
| 54 | + export EDITOR='vim'; |
| 55 | + export NODE_REPL_HISTORY="$HOME/.node_history" |
| 56 | + export NODE_REPL_HISTORY_SIZE='32768'; |
| 57 | + export NODE_REPL_MODE='sloppy'; |
| 58 | + export HISTSIZE='32768'; |
| 59 | + export HISTFILESIZE="${HISTSIZE}"; |
| 60 | + export HISTCONTROL='ignoreboth'; |
| 61 | + export LANG='en_US.UTF-8'; |
| 62 | + export LC_ALL='en_US.UTF-8'; |
| 63 | + export MANPAGER='less -X'; |
| 64 | + export PYTHONSTARTUP="$HOME/.python/startup.py" |
| 65 | + export GPG_TTY="$(tty)" |
| 66 | + export PATH="$HOME/.bin:$PATH" |
| 67 | + } |
| 68 | + |
| 69 | + function __init_iterm_integration { |
| 70 | + __source "${HOME}/.iterm2_shell_integration.bash" |
| 71 | + } |
| 72 | + |
| 73 | + function __init_completions { |
| 74 | + __source "/usr/local/share/bash-completion/bash_completion" |
| 75 | + } |
| 76 | + |
| 77 | + if [ 0 -lt "$#" ]; then |
| 78 | + "$@" |
| 79 | + else |
| 80 | + __init_aliases |
| 81 | + __eval_dircolors |
| 82 | + __init_shopt |
| 83 | + __init_exports |
| 84 | + __init_completions |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +function __init_prompt { |
| 89 | + function __wrapper { |
| 90 | + [[ -n "$1" ]] || return 1 |
| 91 | + printf "%s" "${2}${1}${3}" |
| 92 | + } |
| 93 | + |
| 94 | + function __ps1 { |
| 95 | + local prefix empty_prefix joiner is_prompt_empty=1 |
| 96 | + prefix="${branch_bg}${branch_fg}${branch_bg}${space}" |
| 97 | + joiner="${branch_fg}${branch_bg}${space}" |
| 98 | + empty_prefix="${branch_fg}${branch_bg}${space}" |
| 99 | + [ $is_prompt_empty -eq 1 ] && prefix="$empty_prefix" |
| 100 | + |
| 101 | + __wrapper "$(__branch)" "$prefix" "$space" && { |
| 102 | + prefix="$joiner"; |
| 103 | + is_prompt_empty=0; |
| 104 | + } |
| 105 | + |
| 106 | + prefix="${cwd_bg}${cwd_fg}${cwd_bg}${space}" |
| 107 | + joiner="${cwd_fg}${cwd_bg}${space}" |
| 108 | + empty_prefix="${cwd_fg}${cwd_bg}${space}" |
| 109 | + [ $is_prompt_empty -eq 1 ] && prefix="$empty_prefix" |
| 110 | + |
| 111 | + # section "c" slices |
| 112 | + __wrapper "$(__cwd)" "$prefix" "$space" && { |
| 113 | + prefix="$joiner"; |
| 114 | + is_prompt_empty=0; |
| 115 | + } |
| 116 | + |
| 117 | + printf "%s" "${reset_bg}$reset$space" |
| 118 | + } |
| 119 | + |
| 120 | + function __ps1_short { |
| 121 | + printf " " |
| 122 | + } |
| 123 | + |
| 124 | + function __ps2 { |
| 125 | + local prefix empty_prefix joiner is_prompt_empty=1 |
| 126 | + prefix="${user_bg}${user_fg}${user_bg}${space}" |
| 127 | + joiner="${user_fg}${user_bg}${space}" |
| 128 | + |
| 129 | + empty_prefix="${user_fg}${user_bg}${space}" |
| 130 | + [ $is_prompt_empty -eq 1 ] && prefix="$empty_prefix" |
| 131 | + |
| 132 | + __wrapper "⤷ >" "$prefix" "$space" && { |
| 133 | + prefix="$joiner"; |
| 134 | + is_prompt_empty=0; |
| 135 | + } |
| 136 | + |
| 137 | + printf "%s" "${reset_bg}$reset$space" |
| 138 | + } |
| 139 | + |
| 140 | + function __branch { |
| 141 | + local branch |
| 142 | + local branch_symbol=" " |
| 143 | + |
| 144 | + # git |
| 145 | + if hash git 2>/dev/null; then |
| 146 | + if branch=$( { git symbolic-ref --quiet HEAD || git rev-parse --short HEAD; } 2>/dev/null ); then |
| 147 | + branch=${branch##*/} |
| 148 | + printf "%s" "${branch_symbol}${branch:-unknown}" |
| 149 | + return |
| 150 | + fi |
| 151 | + fi |
| 152 | + return 1 |
| 153 | + } |
| 154 | + |
| 155 | + function __cwd { |
| 156 | + local dir_limit="3" |
| 157 | + local truncation="⋯ " |
| 158 | + local first_char |
| 159 | + local part_count=0 |
| 160 | + local formatted_cwd="" |
| 161 | + local dir_sep=" " |
| 162 | + local tilde="~" |
| 163 | + |
| 164 | + if [ ! -z "$PS1_DIR_LIMIT" ]; then |
| 165 | + dir_limit="$PS1_DIR_LIMIT" |
| 166 | + fi |
| 167 | + |
| 168 | + local cwd="${PWD/#$HOME/$tilde}" |
| 169 | + |
| 170 | + # get first char of the path, i.e. tilde or slash |
| 171 | + first_char=${cwd::1} |
| 172 | + |
| 173 | + # remove leading tilde |
| 174 | + cwd="${cwd#\~}" |
| 175 | + |
| 176 | + while [[ "$cwd" == */* && "$cwd" != "/" ]]; do |
| 177 | + # pop off last part of cwd |
| 178 | + local part="${cwd##*/}" |
| 179 | + cwd="${cwd%/*}" |
| 180 | + |
| 181 | + formatted_cwd="$dir_sep$part$formatted_cwd" |
| 182 | + part_count=$((part_count+1)) |
| 183 | + |
| 184 | + [[ $part_count -eq $dir_limit ]] && first_char="$truncation" && break |
| 185 | + done |
| 186 | + |
| 187 | + printf "%s" "$first_char$formatted_cwd" |
| 188 | + } |
| 189 | + |
| 190 | + function __last_status { |
| 191 | + local status="" |
| 192 | + if [ "$last_status" != 0 ]; then |
| 193 | + status=" $last_status" |
| 194 | + fi |
| 195 | + |
| 196 | + printf "%s" "${reset_bg}$reset$space" |
| 197 | + } |
| 198 | + |
| 199 | + function __prompt { |
| 200 | + local last_status=$? |
| 201 | + local esc=$'[' end_esc=m |
| 202 | + local noprint='\[' end_noprint='\]' |
| 203 | + local wrap="$noprint$esc" end_wrap="$end_esc$end_noprint" |
| 204 | + local space=" " |
| 205 | + local reset="${wrap}0${end_wrap}" |
| 206 | + local reset_bg="${wrap}49${end_wrap}" |
| 207 | + local branch_fg="${wrap}38;5;252${end_wrap}" |
| 208 | + local branch_bg="${wrap}48;5;236${end_wrap}" |
| 209 | + local user_fg="${wrap}38;5;252${end_wrap}" |
| 210 | + local user_bg="${wrap}48;5;238${end_wrap}" |
| 211 | + local cwd_fg="${wrap}38;5;252${end_wrap}" |
| 212 | + local cwd_bg="${wrap}48;5;234${end_wrap}" |
| 213 | + |
| 214 | + if [ "$PS1_SHORT" == 1 ]; then |
| 215 | + PS1="$(__ps1_short)" |
| 216 | + else |
| 217 | + PS1="$(__ps1)" |
| 218 | + fi |
| 219 | + |
| 220 | + PS2="$(__ps2)" |
| 221 | + } |
| 222 | + |
| 223 | + if [[ ! "$PROMPT_COMMAND" == *__prompt* ]]; then |
| 224 | + PROMPT_COMMAND='__prompt;'$'\n'"$PROMPT_COMMAND" |
| 225 | + fi |
| 226 | + |
| 227 | + function pt { |
| 228 | + if [[ "PS1_SHORT" -eq 1 ]]; then |
| 229 | + export PS1_SHORT=0 |
| 230 | + else |
| 231 | + export PS1_SHORT=1 |
| 232 | + fi |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +function __init_marks { |
| 237 | + export MARKPATH="$HOME/.marks" |
| 238 | + |
| 239 | + function jump { |
| 240 | + cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1" |
| 241 | + } |
| 242 | + |
| 243 | + function mark { |
| 244 | + mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1" |
| 245 | + } |
| 246 | + |
| 247 | + function unmark { |
| 248 | + rm -i "$MARKPATH/$1" |
| 249 | + } |
| 250 | + |
| 251 | + function marks { |
| 252 | + \ls -l "$MARKPATH" | tail -n +2 | sed 's/ / /g' | cut -d' ' -f9- | awk -F ' -> ' '{printf "%-10s -> %s\n", $1, $2}' |
| 253 | + } |
| 254 | + |
| 255 | + function __completemarks { |
| 256 | + local curw wordlist |
| 257 | + curw=${COMP_WORDS[COMP_CWORD]} |
| 258 | + wordlist=$(find "$MARKPATH" -type l | rev | cut -d"/" -f1 | rev) |
| 259 | + COMPREPLY=($(compgen -W "${wordlist[@]}" -- "$curw")) |
| 260 | + return 0 |
| 261 | + } |
| 262 | + |
| 263 | + complete -F __completemarks jump unmark |
| 264 | + |
| 265 | + export -f jump |
| 266 | + export -f mark |
| 267 | + export -f unmark |
| 268 | + export -f marks |
| 269 | +} |
| 270 | + |
| 271 | +function title { |
| 272 | + echo -ne "\033]0;"$*"\007" |
| 273 | +} |
| 274 | + |
| 275 | +__init_prompt |
| 276 | +__init_marks |
| 277 | +__init "$@" |
0 commit comments