diff --git a/.gitignore b/.gitignore index 36d193c..ecf2f52 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ fish/nginx.conf git/git-achievements* git.pgp + +bash/secrets.bash diff --git a/bash/alias.bash b/bash/alias.bash new file mode 100644 index 0000000..14010b8 --- /dev/null +++ b/bash/alias.bash @@ -0,0 +1,31 @@ +# General + +alias ll="ls -lAFh" +alias subl="/Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl" + +# Git +alias git="git-achievements" +alias ga="git add -u; git add .; git status -sb" +alias gb="git branch -v" +alias gc="git ci -S --allow-empty -m" +alias gd="git diff" +alias gdc="git diff --cached" +alias gl="git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) %C(bold +green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all" +alias gp="git push" +alias gs="git status -sb" +alias gu="git up" +alias gcl="git co -" +alias gml="git merge -" + +# Ruby +alias bd="bundle install --jobs=8 --path vendor/bundle" + +# Go +alias goi="go install ./..." + +# OSX +alias hide-desktop="defaults write com.apple.finder CreateDesktop -bool false; killall Finder" +alias show-desktop="defaults write com.apple.finder CreateDesktop -bool true; killall Finder" +alias prefs="/Applications/System\ Preferences.app/Contents/MacOS/System\ Preferences" +alias admin-prefs="sudo /Applications/System\ Preferences.app/Contents/MacOS/System\ Preferences" diff --git a/bash/bashrc.bash b/bash/bashrc.bash new file mode 100644 index 0000000..7675558 --- /dev/null +++ b/bash/bashrc.bash @@ -0,0 +1,41 @@ +export TERM=xterm-256color +export EDITOR=vim + +# Colors +export CLICOLOR=1 +export LSCOLORS=Exfxcxdxbxegedabagacad + +export EDITOR=vim +export PROJECTS=$HOME/Code + +# Go +export GOROOT=$PROJECTS/go +export GOPATH=$HOME/go + +export SSL_CERT_FILE="/usr/local/opt/curl-ca-bundle/share/ca-bundle.crt" + +function __bash_import { source $HOME/.bash/$1.bash; } +__bash_import "functions_internal" +__bash_import "history" +__bash_import "path" +__bash_import "format" +__bash_import "iterm" +__bash_import "alias" +__bash_import "prompt" +__bash_import "functions" +__bash_import "docker" +__bash_import "secrets" +# __bash_import "homebrew_completions" + + +if [ -f $(brew --prefix)/etc/bash_completion ]; then + . $(brew --prefix)/etc/bash_completion +fi + +# +# Misc +# + +# . $HOME/.config/on_reboot.sh + +test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash" diff --git a/bash/docker.bash b/bash/docker.bash new file mode 100644 index 0000000..6a914ef --- /dev/null +++ b/bash/docker.bash @@ -0,0 +1,12 @@ +function d-ssh { + docker exec -it $1 /bin/bash +} + +function d-ps { + docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}\t{{.CreatedAt}}" $@ +} + +function d-clean { + local images=$(docker images -a | grep -e '^' | awk '{print $3}' | cut -d: -f2) + [[ -n $images ]] && docker rmi -f $images +} diff --git a/bash/format.bash b/bash/format.bash new file mode 100644 index 0000000..0de4e11 --- /dev/null +++ b/bash/format.bash @@ -0,0 +1,63 @@ +# +# Attributes +# + +_BOLD=$(__bash_escape 1) +_DIM=$(__bash_escape 2) +_ITALIC=$(__bash_escape 3) +_UNDERLINED=$(__bash_escape 4) +_BLINK=$(__bash_escape 5) +# _BRIGHT=$(__bash_escape 6) +_INVERTED=$(__bash_escape 7) +_HIDDEN=$(__bash_escape 8) # E.g. for passwords + +_RESET=$(__bash_escape 0) +_RESET_BOLD=$(__bash_escape 21) # Doesn't work? +_RESET_DIM=$(__bash_escape 22) +_RESET_ITALIC=$(__bash_escape 23) +_RESET_UNDERLINED=$(__bash_escape 24) +_RESET_BLINK=$(__bash_escape 25) +# _RESET_BRIGHT=$(__bash_escape 26) +_RESET_INVERTED=$(__bash_escape 27) +_RESET_HIDDEN=$(__bash_escape 28) + +# +# Colors +# + +_BLACK=$(__bash_escape 30) +_RED=$(__bash_escape 31) +_GREEN=$(__bash_escape 32) +_YELLOW=$(__bash_escape 33) +_BLUE=$(__bash_escape 34) +_MAGENTA=$(__bash_escape 35) +_CYAN=$(__bash_escape 36) +_LIGHT_GRAY=$(__bash_escape 37) +_DARK_GRAY=$(__bash_escape 90) +_LIGHT_RED=$(__bash_escape 91) +_LIGHT_GREEN=$(__bash_escape 92) +_LIGHT_YELLOW=$(__bash_escape 93) +_LIGHT_BLUE=$(__bash_escape 94) +_LIGHT_MAGENTA=$(__bash_escape 95) +_LIGHT_CYAN=$(__bash_escape 96) +_WHITE=$(__bash_escape 97) + +_BG_BLACK=$(__bash_escape 40) +_BG_RED=$(__bash_escape 41) +_BG_GREEN=$(__bash_escape 42) +_BG_YELLOW=$(__bash_escape 43) +_BG_BLUE=$(__bash_escape 44) +_BG_MAGENTA=$(__bash_escape 45) +_BG_CYAN=$(__bash_escape 46) +_BG_LIGHT_GRAY=$(__bash_escape 47) +_BG_DARK_GRAY=$(__bash_escape 100) +_BG_LIGHT_RED=$(__bash_escape 101) +_BG_LIGHT_GREEN=$(__bash_escape 102) +_BG_LIGHT_YELLOW=$(__bash_escape 103) +_BG_LIGHT_BLUE=$(__bash_escape 104) +_BG_LIGHT_MAGENTA=$(__bash_escape 105) +_BG_LIGHT_CYAN=$(__bash_escape 106) +_BG_WHITE=$(__bash_escape 107) + +_RESET_COLOR=$(__bash_escape 39) +_RESET_BG=$(__bash_escape 49) diff --git a/bash/functions.bash b/bash/functions.bash new file mode 100644 index 0000000..54bf51e --- /dev/null +++ b/bash/functions.bash @@ -0,0 +1,49 @@ +function reload { + source $HOME/.bash_profile +} + +function psg { + ps aux | head -n1 + ps aux | grep $1 | grep -v grep +} +function ssh { + # Change tab title + local host=$(echo $1 | cut -d@ -f1) + __iterm_set_title $host + + # Change tab color and reset back when conneciton is closed + __iterm_set_bg_rgb 220 20 0 && \ + /usr/bin/ssh $@ && \ + __iterm_set_bg_reset +} + + +function shuf { + awk 'BEGIN {srand(); OFMT="%.17f"} {print rand(), $0}' "$@" | \ + sort -k1,1n | \ + cut -d' ' -f2- +} + +function __bash_ssh_complete { + local cur=${COMP_WORDS[COMP_CWORD]} + local opts=$(cat $HOME/.ssh/known_hosts | cut -d' ' -f1 | cut -d, -f1) + COMPREPLY=($(compgen -o nospace -W "$opts" -- $cur)) +} +complete -F __bash_ssh_complete ssh + +# +# Directory shortcuts +# + +function h { cd $HOME/$1; } +function _h { __bash_directory_complete $HOME; } +complete -F _h h + +function c { cd $(realpath $PROJECTS/$1); } +function _c { __bash_directory_complete $PROJECTS; } +complete -F _c c + +function __bash_directory_complete { + local cur=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=($(compgen -o nospace -W "$(ls $1)" -- $cur)) +} diff --git a/bash/functions_internal.bash b/bash/functions_internal.bash new file mode 100644 index 0000000..6ac84de --- /dev/null +++ b/bash/functions_internal.bash @@ -0,0 +1,17 @@ +function __bash_basedir { + local full=$(pwd) + if [ $full == "/" ] + then + echo $full + else + echo $full | rev | cut -d/ -f1 | rev + fi +} + +function __bash_escape { + echo -ne "\e[$1m" +} + +function __bash_is_empty { + if [ "$1" == "" ]; then return 0; else return 1; fi +} diff --git a/bash/history.bash b/bash/history.bash new file mode 100644 index 0000000..1b4a1d5 --- /dev/null +++ b/bash/history.bash @@ -0,0 +1,9 @@ +shopt -s histappend # Append history instead of rewriting it +shopt -s cmdhist # Use one command per line +HISTCONTROL=ignoreboth # Don’t store specific lines +HISTTIMEFORMAT='%F %T ' # Record timestamps +HISTFILESIZE=1000000 # Allow a larger history file +HISTSIZE=1000000 # Allow a longer history +PROMPT_COMMAND='history -a' # Store history immediately +# Don't add such calls to history +HISTIGNORE='ls:ll:bg:fg:history:ga:gd:gc:gl:gp' diff --git a/bash/iterm.bash b/bash/iterm.bash new file mode 100644 index 0000000..5db4317 --- /dev/null +++ b/bash/iterm.bash @@ -0,0 +1,31 @@ +# $1 - title +function __iterm_set_title { + echo -ne "\033]0;$1\007" +} + +# $1 - basedir +function __iterm_set_bg_color_by_dir { + case $1 in + "example" ) __iterm_set_bg_rgb 0 150 255 ;; + * ) __iterm_set_bg_reset ;; + esac +} + +# $1 red (0-255) +# $2 green (0-255) +# $3 blue (0-255) +function __iterm_set_bg_rgb { + __iterm_set_bg_channel red $1 + __iterm_set_bg_channel green $2 + __iterm_set_bg_channel blue $3 +} + +function __iterm_set_bg_reset { + echo -ne "\033]6;1;bg;*;default\a" +} + +# $1 channel (red, green, blue) +# $2 brightness (0-255) +function __iterm_set_bg_channel { + echo -ne "\033]6;1;bg;$1;brightness;$2\a" +} diff --git a/bash/path.bash b/bash/path.bash new file mode 100644 index 0000000..0236d40 --- /dev/null +++ b/bash/path.bash @@ -0,0 +1,21 @@ +export PATH=/usr/local/bin:$PATH # Homebrew tools +export PATH=$PATH:/usr/local/sbin # Homebrew apps +export PATH=$PATH:$HOME/.dotfiles/bin # My stuff + +# NodeJS +# export PATH=$PATH:/usr/local/share/npm/bin + +# Ruby +export PATH=$PATH:$HOME/.rbenv/plugins/ruby-build/bin # rbenv plugins +export PATH=$HOME/.rbenv/shims:$PATH # Gem binaries + +# git achievements +export PATH=$PATH:$HOME/.misc/git-achievements + +# Go +export PATH=$PATH:$GOROOT/bin # Go runtime binaries +export PATH=$PATH:$GOPATH/bin # Go package binaries + +# Python +# WTF +export PKG_CONFIG_PATH=/usr/local/opt/python3/Frameworks/Python.framework/Versions/3.4/lib/pkgconfig diff --git a/bash/prompt.bash b/bash/prompt.bash new file mode 100644 index 0000000..9918d58 --- /dev/null +++ b/bash/prompt.bash @@ -0,0 +1,64 @@ +GIT="/usr/local/bin/git" + +function __bash_prompt { + local baseDir=$(__bash_basedir) + __iterm_set_title $baseDir + __iterm_set_bg_color_by_dir $baseDir + + # Define building blocks + local basedir="$_BOLD$_BLUE\W$_RESET_COLOR$_RESET" + local git_y_u_no_commit="$_MAGENTA$(__bash_git_y_u_no_commit_warn)$_RESET_COLOR" + local git_branch="$(__bash_prompt_git)" + local cursor="\[${_DIM}\]›\[$_RESET\]" + + PS1="\n$basedir$git_branch$git_y_u_no_commit\n$cursor\[$_RESET\] " +} +PROMPT_COMMAND=__bash_prompt # <--- ta-da! + +function __bash_prompt_escape { + echo -ne "\[$1\]" +} + +# +# Git prompt +# + +function __bash_prompt_git { + __bash_is_git_dir && { + __bash_git_is_clean && \ + local branch="$_GREEN$(__bash_git_branch)$_RESET_COLOR" || \ + local branch="$_RED$(__bash_git_branch)$_RESET_COLOR" + __bash_git_is_pushed || \ + branch="$_UNDERLINED$branch$_RESET_UNDERLINED" + + echo -ne " ${_DIM}git:$_RESET_DIM$branch" + } +} + +function __bash_git_y_u_no_commit_warn { + __bash_is_git_dir && { + $GIT diff --stat 2>/dev/null | awk -F',' '/files? changed/ { lc += $2 + $3 } END { + if (lc > 100) printf " -- Y U NO COMMIT!? --" + }' + } +} + +function __bash_git_branch { + __bash_is_git_dir && { + $GIT rev-parse --symbolic-full-name --abbrev-ref HEAD + } +} + +function __bash_git_is_clean { + __bash_is_git_dir && { + __bash_is_empty $($GIT status --porcelain) + } +} + +function __bash_git_is_pushed { + __bash_is_git_dir && __bash_is_empty $($GIT cherry -v "@{upstream}" 2>/dev/null) +} + +function __bash_is_git_dir { + if [ -d ".git" ]; then return 0; else return 1; fi +}