profile-util 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. if [[ -z ${branch} ]]; then return; fi
  20. if [[ ${branch} == "HEAD" ]]; then
  21. echo -n ":$(git show -s --format=%h)"
  22. else
  23. echo -n ":${branch}"
  24. fi
  25. }
  26. export PS1='$(rc=$?; echo -e "\[$CYAN\]\D{%F %T} \[$BBLUE\]\u\[$RESET\]@\[$(SUCCESS $rc)\]\h:[\w$(GITBRANCH)] $(CODE $rc)\[$RESET\]$ ")'
  27. export EDITOR=nvim
  28. PATH="$HOME/usr/local/bin:$HOME/bin:$HOME/usr/bin:$PATH"
  29. export PATH
  30. export LC_ALL=en_US.UTF-8
  31. if [[ -z "$BASEPATH" ]]; then
  32. export BASEPATH="$PATH"
  33. fi
  34. alias reset-path="export PATH=\"$BASEPATH\""
  35. # Turn a number of symlink files into copies of their original
  36. # Usage:
  37. # detach file...
  38. detach() {
  39. for i; do [ -L "$i" ] && cp --remove-destination $(readlink "$i") "$i"; done
  40. }
  41. link-tools() {
  42. PATH=$HOME/tools/bin:$HOME/tools/usr/bin:$PATH
  43. LD_LIBRARY_PATH=$HOME/tools/lib:$HOME/tools/usr/lib:$LD_LIBRARY_PATH
  44. export PATH
  45. export LD_LIBRARY_PATH
  46. }
  47. unlink-tools() {
  48. PATH=$(echo $PATH | perl -pne "s/(?<=:)$HOME/tools.*?://g")
  49. LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | perl -pne "s/(?<=:)$HOME/tools.*?://g")
  50. export PATH
  51. export LD_LIBRARY_PATH
  52. }
  53. alias listall="list -A"
  54. alias disk-usage="ls -A | xargs du -shc | sort -h"
  55. alias reload="source ~/.bashrc"
  56. alias crontab="VIM_CRONTAB=true crontab"
  57. refreshterm() {
  58. clear
  59. uname -a
  60. cat /etc/motd
  61. echo ""
  62. pwd
  63. list
  64. }
  65. _mkcd_fun() {
  66. if [ ! -d "$2" ]; then
  67. if [ -e "$2" ]; then
  68. echo "mkcd: file exists and is not a directory"
  69. exit
  70. else
  71. $1 "$2"
  72. fi
  73. fi
  74. cd "$2"
  75. }
  76. alias mkcd="_mkcd_fun mkdir"
  77. mkdirs() {
  78. mkdir -p "$1"
  79. }
  80. alias mkcds="_mkcd_fun mkdirs"
  81. alias diffr="diff --ignore-blank-lines --minimal --side-by-side --report-identical-files --suppress-common-lines"
  82. alias diffl="diff --changed-group-format='%<' --unchanged-group-format=''"
  83. _recall_fun() {
  84. if [ -z "$1" ]; then echo "recall: usage: recall string"; return 1; fi
  85. history | cut -c 8- | grep --extended-regexp "$1" | \
  86. grep --invert-match --extended-regexp "^(history|recall|redo)" | sort | uniq
  87. }
  88. redo() {
  89. IFS=$'\n'
  90. hist=($(_recall_fun $1))
  91. for ((i=0; i<${#hist[@]}; i++));
  92. do
  93. echo "[$i] ${hist[$i]}"
  94. done
  95. printf "Index: "
  96. read idx
  97. if [[ $idx =~ ^[0-9]+$ ]];
  98. then
  99. eval "${hist[$idx]}"
  100. fi
  101. unset IFS
  102. }
  103. recall(){
  104. _recall_fun $1 | sed 's/^/ /'
  105. }
  106. clear() {
  107. /usr/bin/clear
  108. if [[ ! -z "${TMUX}" ]]; then tmux clear-history; fi
  109. }
  110. swap() {
  111. if [[ $# -ne 2 ]]; then
  112. echo "swap file1 file2" >&2
  113. return 1
  114. fi
  115. local tmpfile=$(mktemp)
  116. mv "${1}" "${tmpfile}"
  117. mv "${2}" "${1}"
  118. mv "${tmpfile}" "${2}"
  119. }
  120. has() {
  121. [[ "$(type -t "${1}")" =~ function|alias|file ]]
  122. }
  123. maybe_do() {
  124. has "${1}" && "$@"
  125. }
  126. alias coms="${HOME}/tools/comstore/commands.sh"
  127. clrun() {
  128. clear
  129. echo "$@"
  130. "$@"
  131. }
  132. _total_on_bottom() {
  133. local cap=""
  134. while read line; do
  135. if [[ ${line} =~ .*total$ ]]; then
  136. local cap="${line}"
  137. else
  138. echo "${line}"
  139. fi
  140. done
  141. if [[ -n "${cap}" ]]; then
  142. echo "${cap}"
  143. fi
  144. }
  145. _rlall_noself() {
  146. for i in "$@"; do
  147. [[ $(basename ${i}) =~ ^\.\.?$ ]] || readlink -f "${i}"
  148. done
  149. }
  150. dus() {
  151. if [[ ${1} == "-a" ]]; then
  152. IFS=$'\n' args=($(_rlall_noself "${2}/"* "${2}/".* )); unset IFS
  153. elif [[ ${1} == "-d" ]]; then
  154. IFS=$'\n' args=($(_rlall_noself "${2}/"* )); unset IFS
  155. else
  156. IFS=$'\n' args=($(_rlall_noself "$@")); unset IFS
  157. fi
  158. du -shc "${args[@]}" | sort -h | _total_on_bottom
  159. }
  160. repeat() {
  161. local n=$1
  162. shift
  163. for ((i=0;i<n;++i)); do
  164. "$@"
  165. done
  166. }
  167. scriptdiff() {
  168. vimdiff <("${@:3}" "$1") <("${@:3}" "$2")
  169. }
  170. vimdiff_jq() {
  171. scriptdiff "$2" "$3" jq -M "$1"
  172. }
  173. if [[ $(uname) == "Darwin" ]]; then # OSX
  174. . ${HOME}/.profile-osx
  175. elif [[ $(uname) == *BSD* ]]; then # FreeBSD
  176. . ${HOME}/.profile-bsd
  177. else
  178. . ${HOME}/.profile-linux
  179. fi
  180. . ${HOME}/.profile-dev
  181. has git && . ${HOME}/.profile-git || true
  182. has p4 && . ${HOME}/.profile-p4 || true