1
0
Fork 0

Bash stuff

This commit is contained in:
Gregory Eremin 2017-03-10 11:42:12 +01:00
parent 758f399776
commit c8ec9f8a7c
11 changed files with 340 additions and 0 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ fish/nginx.conf
git/git-achievements*
git.pgp
bash/secrets.bash

31
bash/alias.bash Normal file
View File

@ -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"

41
bash/bashrc.bash Normal file
View File

@ -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"

12
bash/docker.bash Normal file
View File

@ -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 '^<none>' | awk '{print $3}' | cut -d: -f2)
[[ -n $images ]] && docker rmi -f $images
}

63
bash/format.bash Normal file
View File

@ -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)

49
bash/functions.bash Normal file
View File

@ -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))
}

View File

@ -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
}

9
bash/history.bash Normal file
View File

@ -0,0 +1,9 @@
shopt -s histappend # Append history instead of rewriting it
shopt -s cmdhist # Use one command per line
HISTCONTROL=ignoreboth # Dont 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'

31
bash/iterm.bash Normal file
View File

@ -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"
}

21
bash/path.bash Normal file
View File

@ -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

64
bash/prompt.bash Normal file
View File

@ -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
}