profile-util 3.1 KB

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