profile-util 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. CYAN="\e[96m"
  2. GREEN="\e[92m"
  3. RED="\e[91m"
  4. BBLUE="\e[34;1m"
  5. RESET="\e[0m"
  6. IS_PASS() {
  7. [[ $1 -eq 0 ]] || [[ $1 -gt 128 ]]
  8. }
  9. SUCCESS() {
  10. if IS_PASS $1; then
  11. echo -en $GREEN
  12. else
  13. echo -en $RED
  14. fi
  15. }
  16. CODE() { IS_PASS $1 || echo "(rc=$1) "; }
  17. GITBRANCH() {
  18. local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
  19. [[ -z ${branch} ]] || echo -n ":${branch}"
  20. }
  21. export PS1='$(rc=$?; echo -e "\[$CYAN\]\D{%F %T} \[$BBLUE\]\u\[$RESET\]@\[$(SUCCESS $rc)\]\h:[\w$(GITBRANCH)] $(CODE $rc)\[$RESET\]$ ")'
  22. PATH="$HOME/usr/local/bin:$HOME/bin:$HOME/usr/bin:$PATH"
  23. export PATH
  24. export LC_ALL=en_US.UTF-8
  25. if [[ -z "$BASEPATH" ]]; then
  26. export BASEPATH="$PATH"
  27. fi
  28. alias reset-path="export PATH=\"$BASEPATH\""
  29. # Turn a number of symlink files into copies of their original
  30. # Usage:
  31. # detach file...
  32. detach() {
  33. for i; do [ -L "$i" ] && cp --remove-destination $(readlink "$i") "$i"; done
  34. }
  35. link-tools() {
  36. PATH=$HOME/tools/bin:$HOME/tools/usr/bin:$PATH
  37. LD_LIBRARY_PATH=$HOME/tools/lib:$HOME/tools/usr/lib:$LD_LIBRARY_PATH
  38. export PATH
  39. export LD_LIBRARY_PATH
  40. }
  41. unlink-tools() {
  42. PATH=$(echo $PATH | perl -pne "s/(?<=:)$HOME/tools.*?://g")
  43. LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | perl -pne "s/(?<=:)$HOME/tools.*?://g")
  44. export PATH
  45. export LD_LIBRARY_PATH
  46. }
  47. alias listall="list -A"
  48. alias disk-usage="ls -A | xargs du -shc | sort -h"
  49. alias reload="source ~/.bashrc"
  50. alias crontab="VIM_CRONTAB=true crontab"
  51. refreshterm() {
  52. clear
  53. uname -a
  54. cat /etc/motd
  55. echo ""
  56. pwd
  57. list
  58. }
  59. _mkcd_fun() {
  60. if [ ! -d "$2" ]; then
  61. if [ -e "$2" ]; then
  62. echo "mkcd: file exists and is not a directory"
  63. exit
  64. else
  65. $1 "$2"
  66. fi
  67. fi
  68. cd "$2"
  69. }
  70. alias mkcd="_mkcd_fun mkdir"
  71. mkdirs() {
  72. mkdir -p "$1"
  73. }
  74. alias mkcds="_mkcd_fun mkdirs"
  75. alias diffr="diff --ignore-blank-lines --minimal --side-by-side --report-identical-files --suppress-common-lines"
  76. alias diffl="diff --changed-group-format='%<' --unchanged-group-format=''"
  77. _recall_fun() {
  78. if [ -z "$1" ]; then echo "recall: usage: recall string"; return 1; fi
  79. history | cut -c 8- | grep --extended-regexp "$1" | \
  80. grep --invert-match --extended-regexp "^(history|recall|redo)" | sort | uniq
  81. }
  82. redo() {
  83. IFS=$'\n'
  84. hist=($(_recall_fun $1))
  85. for ((i=0; i<${#hist[@]}; i++));
  86. do
  87. echo "[$i] ${hist[$i]}"
  88. done
  89. printf "Index: "
  90. read idx
  91. if [[ $idx =~ ^[0-9]+$ ]];
  92. then
  93. eval "${hist[$idx]}"
  94. fi
  95. unset IFS
  96. }
  97. recall(){
  98. _recall_fun $1 | sed 's/^/ /'
  99. }
  100. clear() {
  101. /usr/bin/clear
  102. if [[ ! -z "${TMUX}" ]]; then tmux clear-history; fi
  103. }
  104. swap() {
  105. if [[ $# -ne 2 ]]; then
  106. echo "swap file1 file2" >&2
  107. return 1
  108. fi
  109. local tmpfile=$(mktemp)
  110. mv "${1}" "${tmpfile}"
  111. mv "${2}" "${1}"
  112. mv "${tmpfile}" "${2}"
  113. }
  114. has() {
  115. [[ "$(type -t "${1}")" =~ function|alias|file ]]
  116. }
  117. maybe_do() {
  118. has "${1}" && "$@"
  119. }
  120. alias coms="${HOME}/tools/comstore/commands.sh"
  121. clrun() {
  122. clear
  123. echo "$@"
  124. "$@"
  125. }
  126. if [[ $(uname) == "Darwin" ]]; then # OSX
  127. . ${HOME}/.profile-osx
  128. elif [[ $(uname) == *BSD* ]]; then # FreeBSD
  129. . ${HOME}/.profile-bsd
  130. else
  131. . ${HOME}/.profile-linux
  132. fi
  133. has git && . ${HOME}/.profile-git || true
  134. has p4 && . ${HOME}/.profile-p4 || true