You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bashrc 18 KiB

13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. #!/bin/bash
  2. # If not running interactively, don't do anything
  3. [[ $- != *i* ]] && return
  4. ##########################################
  5. null(){
  6. "$@" >/dev/null 2>&1
  7. }
  8. __try_exec(){
  9. type $1 >/dev/null 2>&1 && "$@"
  10. }
  11. ##########################
  12. # system type
  13. alias ismsys=false
  14. alias iscygwin=false
  15. alias iswindows=false
  16. alias isdarwin=false
  17. alias islinux=false
  18. alias with_coreutils=false # for mac
  19. case `uname` in
  20. (MINGW*) alias ismsys=true ;;
  21. (CYGWIN*) alias iscygwin=true ;;
  22. (Darwin*) alias isdarwin=true ;;
  23. (Linux*) alias islinux=true ;;
  24. esac
  25. null ls --version && alias with_coreutils=true
  26. ( ismsys || iscygwin ) && alias iswindows=true
  27. alias inbash=false
  28. test -n "$BASH_VERSION" && alias inbash=true
  29. #################################
  30. if echo $PATH | grep "$HOME" >/dev/null 2>&1
  31. then
  32. PATH="${PATH}:${HOME}/.local/bin"
  33. fi
  34. if false iswindows
  35. then
  36. export PAGER='tr -d \\r | less'
  37. else
  38. export PAGER="less"
  39. fi
  40. export LESS="-iRMX"
  41. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  42. if test -n "$_src_hilite_lp_path"
  43. then
  44. export LESSOPEN="| $_src_hilite_lp_path %s"
  45. fi
  46. if null type vim
  47. then
  48. export EDITOR=vim
  49. else
  50. export EDITOR=vi
  51. fi
  52. export LANG=ja_JP.UTF-8
  53. export LC_MESSAGES=C
  54. # export CDPATH=".:~"
  55. export VISUAL="$EDITOR"
  56. export GIT_PAGER="less -F"
  57. export GIT_EDITOR="$EDITOR"
  58. if test -n "$TMUX" && \
  59. echo $TERM | grep screen >/dev/null 2>&1 && \
  60. tmux display -p '#{client_termname}' | grep 256color >/dev/null 2>&1
  61. then
  62. TERM=screen-256color
  63. fi
  64. test -z "$TMP" && export TMP=/tmp/${USER}-tmp
  65. mkdir -p "$TMP"
  66. ! iswindows && null type stty && {
  67. stty stop undef # unbind C-s to stop displaying output
  68. # stty erase '^h'
  69. }
  70. if iswindows; then
  71. export USER=$USERNAME
  72. fi
  73. _tmux_prefs(){
  74. null type tmux || return 1
  75. tmux set -g mode-keys vi
  76. }
  77. if test -d ~/dbx
  78. then
  79. export CHIT_PATH="$HOME/dbx/.chit"
  80. fi
  81. #######################
  82. iswindows && alias tty="echo cmd.exe"
  83. type fortune >/dev/null 2>&1 && {
  84. fortune
  85. echo
  86. fortune -o
  87. echo
  88. }
  89. uname -a
  90. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  91. running $BASH $BASH_VERSION
  92. if test -n "$TMUX"
  93. then
  94. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  95. 2>/dev/null
  96. echo
  97. fi
  98. ###################################
  99. # some aliases and functions
  100. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  101. _coloroption=" --color=auto"
  102. ( ! with_coreutils && isdarwin ) || iswindows || \
  103. _timeoption=" --time-style=long-iso"
  104. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  105. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  106. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  107. alias ls="ls -hCF${_coloroption}${_timeoption}"
  108. if ! with_coreutils
  109. then
  110. export LSCOLORS=gxfxcxdxbxegedabagacad
  111. alias ls="ls -G"
  112. fi
  113. # export GREP_OPTIONS=""
  114. alias gr="grep -n --color=always"
  115. iswindows && alias grep="grep -n"
  116. # alias ll="ls -l"
  117. # alias la="ls -A"
  118. # alias lla="ls -Al"
  119. alias less="less -F"
  120. null type emacs && alias em="emacs -nw"
  121. null type vim && alias vi=vim
  122. alias pstree="LANG=C pstree"
  123. alias cp="cp -v"
  124. alias mv="mv -v"
  125. alias rm="rm -v"
  126. alias psaux="ps auxww"
  127. alias q=exit
  128. null type e3em && alias e3=e3em
  129. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  130. alias po=popd
  131. alias pu=pushd
  132. null type sudo && alias sudo="sudo " # use aliases through sudo
  133. null type sudoedit && alias sudoe="sudoedit"
  134. null type halt && alias halt="sudo halt"
  135. null type reboot && alias reboot="sudo reboot"
  136. null type dbus-send && {
  137. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  138. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  139. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  140. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  141. }
  142. alias rand="echo \$RANDOM"
  143. null type file-roller && alias xunp="file-roller -h"
  144. null type paco && alias pc="sudo \paco -D"
  145. alias pycalc="python -i -c 'from math import *' "
  146. null type python3 && alias py3=python3
  147. null type python2 && alias py2=python2
  148. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  149. # alias mytime="date +%Y%m%d-%H%M%S"
  150. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  151. # type trash >/dev/null 2>&1 && alias rm=trash
  152. null type mpg123 && alias mpg123="mpg123 -C -v --title"
  153. null type xmms2 && alias xm="xmms2"
  154. #export PLAYER="mpg123 -C -v --title"
  155. null type screen && alias screen="screen -e^z^z"
  156. #alias zcd="cd \`zenity --file-selection --directory\`"
  157. null type gtags && alias gtags="gtags --verbose"
  158. null type htags && alias htags="htags --xhtml --symbol --line-number \
  159. --frame --alphabet --verbose"
  160. null type aunpack && alias aun=aunpack
  161. null type lv && alias lv="lv|less"
  162. isdarwin && alias updatedb="LC_ALL=C updatedb"
  163. # do not use locate installed by macports
  164. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  165. # pad
  166. alias pad=notepad
  167. null type gedit && alias pad=gedit
  168. null type leafpad && alias pad=leafpad
  169. isdarwin && alias pad="open -e"
  170. null type wicd-curses && alias wic=wicd-curses
  171. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  172. #alias wicn="wicd-cli -y -c -n"
  173. wicn(){
  174. if test $# -eq 0
  175. then
  176. local num
  177. wicd-cli -y -l | head
  178. echo -n "input num: "
  179. read num
  180. test -n "$num" && wicd-cli -y -c -n $num
  181. else
  182. wicd-cli -y -c -n $1
  183. fi
  184. }
  185. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  186. /usr/share/vim/vim73/macros/less.sh \
  187. /usr/share/vim/vim72/macros/less.sh
  188. do
  189. test -f $f && alias vl=$f && break
  190. done
  191. null type yaourt && alias yt=yaourt
  192. null type cower && alias cower="cower --color=auto"
  193. null type pacmatic && {
  194. alias pacman="pacmatic"
  195. export PACMAN="pacmatic"
  196. }
  197. null type apt-get && {
  198. alias aupgrade="sudo apt-get autoremove --yes && \
  199. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  200. alias aptin="apt-get install"
  201. alias aptsearch="apt-cache search"
  202. alias aptshow="apt-cache show"
  203. }
  204. null type port && {
  205. alias port="port -v"
  206. alias pupgrade="sudo port -v selfupdate && \
  207. { sudo port -v upgrade outdated; }"
  208. }
  209. if iscygwin; then
  210. null type windate || \
  211. alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  212. alias cygsu="cygstart /cygwinsetup.exe"
  213. alias emacs="CYGWIN=tty emacs -nw"
  214. alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  215. fi
  216. g(){
  217. if test $# -eq 0 && null type git-info
  218. then
  219. git info
  220. else
  221. git "$@"
  222. fi
  223. }
  224. if null type _git && inbash
  225. then
  226. # enable programmable completion for g
  227. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  228. || complete -o default -o nospace -F _git g
  229. fi
  230. null type gitmemo && alias m=gitmemo
  231. null type gitmemo && alias m=gitmemo
  232. alias setup.py="sudo python3 setup.py install --record files.txt"
  233. ssh(){
  234. __my_set_screen_title ssh
  235. command ssh "$@"
  236. }
  237. clk(){
  238. local tformat="%Y/%m/%d %H:%M:%S %z"
  239. cal
  240. while true
  241. do
  242. printf "\\r`date "+${tformat}"`"
  243. sleep 1
  244. done
  245. }
  246. man(){
  247. env \
  248. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  249. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  250. LESS_TERMCAP_me=$(printf "\e[0m") \
  251. LESS_TERMCAP_se=$(printf "\e[0m") \
  252. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  253. LESS_TERMCAP_ue=$(printf "\e[0m") \
  254. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  255. man "$@"
  256. }
  257. scr(){
  258. test -n "$1" && pf="${1}-"
  259. local _tformat="%Y%m%d-%H%M%S%z"
  260. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  261. SCRIPT=${_file} script ${_file} "$@"
  262. }
  263. netwait(){
  264. while ! ping -c 1 -t 1 example.com
  265. do
  266. true
  267. done
  268. echo network works.
  269. }
  270. cd(){
  271. if test $# -eq 0
  272. then
  273. pushd ~/ >/dev/null
  274. elif test "$1" = -
  275. then
  276. local pwd="$PWD"
  277. command cd "$OLDPWD"
  278. pushd -n "$pwd" >/dev/null # stack last dir
  279. elif ! test -d "$1"
  280. then
  281. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  282. return 1
  283. else
  284. pushd "$1" >/dev/null
  285. fi
  286. __dirs_rm_dup "$PWD"
  287. echo "$PWD"
  288. }
  289. __dirs_rm_dup(){
  290. for d in "$@"
  291. do
  292. local next="$(__realpath --strip "$d")"
  293. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  294. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  295. do
  296. popd +$l -n >/dev/null
  297. done
  298. done
  299. }
  300. __realpath(){
  301. if type realpath >/dev/null 2>&1
  302. then
  303. command realpath "$@"
  304. else
  305. while ! test -d $1
  306. do
  307. shift
  308. done
  309. local d="$OLDPWD"
  310. command cd "$1"
  311. echo "$PWD"
  312. command cd "$d"
  313. fi
  314. }
  315. dh(){
  316. if test $# -eq 0
  317. then
  318. dirs -v -l
  319. else
  320. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  321. cd "$dir"
  322. fi
  323. }
  324. input(){
  325. local foo
  326. stty -echo
  327. read foo
  328. stty echo
  329. echo $foo
  330. }
  331. tmux(){
  332. if test $# -eq 0
  333. then
  334. (cd ~; command tmux start;)
  335. if command tmux has -t main
  336. then
  337. command tmux attach -t main
  338. else
  339. (cd ~; command tmux new -s main;)
  340. fi
  341. else
  342. command tmux "$@"
  343. fi
  344. }
  345. mcrypt-stream(){
  346. test $# -eq 2 || return 1
  347. case $1 in
  348. en)
  349. mcrypt --key $2 | base64 ;;
  350. de)
  351. base64 -d | mcrypt -d --key $2 ;;
  352. esac
  353. }
  354. gpg-stream(){
  355. test $# -eq 2 || return 1
  356. case $1 in
  357. en)
  358. gpg --passphrase $2 -c --batch |base64 ;;
  359. de)
  360. base64 -d|gpg --passphrase $2 -d --batch ;;
  361. esac
  362. }
  363. dgpg(){
  364. if test "$1" = help || test -z "$2"
  365. then
  366. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  367. return
  368. fi
  369. local srcs="$2"
  370. local dsts="$3"
  371. test -z "$dsts" && dsts="${srcs}.out"
  372. local pw
  373. echo -n "dgpg pw: "
  374. read -s pw
  375. echo ""
  376. test -z "$pw" && return 1
  377. for f in *${srcs}
  378. do
  379. local d="$(basename "$f" "${srcs}")${dsts}"
  380. echo -n "Processing $f to $d..."
  381. if test -d "$f"
  382. then
  383. echo "`printf 'failed (%s is directory)' $f`"
  384. elif test -f "$d"
  385. then
  386. echo "`printf 'failed (%s is already exists)' $d`"
  387. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  388. then
  389. echo "done"
  390. else
  391. echo "failed"
  392. test -f "$d" && rm "$d"
  393. fi
  394. done
  395. }
  396. alias enst="gpg-stream en"
  397. alias dest="gpg-stream de"
  398. showinfo(){
  399. echo "Japanese letters are 表示可能"
  400. __try_exec diskinfo
  401. ! isdarwin && test -n "${DISPLAY}" && {
  402. __try_exec xrandr | \grep --color=never ^Screen
  403. }
  404. iswindows || __try_exec finger $USER
  405. LANG=C __try_exec id
  406. __try_exec xset q
  407. }
  408. x(){
  409. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  410. #mkdir -p ~/.my/log
  411. # nohup startx >~/.my/log/xorg.log 2>&1 &
  412. # exit
  413. exec startx
  414. else
  415. echo "X cant be started! Another X is already running?" 1>&2
  416. fi
  417. }
  418. bak(){
  419. for file in "$@"
  420. do
  421. cp -v ${file} ${file}.bak
  422. done
  423. }
  424. di(){
  425. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  426. then
  427. local diffcmd=colordiff
  428. else
  429. local diffcmd=diff
  430. fi
  431. ${diffcmd} -u "$@" | ${PAGER}
  432. }
  433. tb(){
  434. local tb="$HOME/.my/tb"
  435. mkdir -p "$tb"
  436. for file in "$@"
  437. do
  438. mv -t "$tb" "$file"
  439. done
  440. }
  441. mkcd(){
  442. if test -d "$1"
  443. then
  444. echo "Dir \"$1\" already exists."
  445. else
  446. mkdir -p "$1"
  447. echo "Dir \"$1\" created."
  448. fi
  449. cd "$1"
  450. }
  451. if test -n "$TMUX" && null type reattach-to-user-namespace
  452. then
  453. alias pbpaste="reattach-to-user-namespace pbpaste"
  454. alias pbcopy="reattach-to-user-namespace pbcopy"
  455. fi
  456. catclip(){
  457. if iswindows
  458. then
  459. cat /dev/clipboard | tr -d \\r
  460. elif isdarwin
  461. then
  462. pbpaste
  463. else
  464. xclip -o -selection "clipboard"
  465. fi
  466. }
  467. setclip(){
  468. if test $# -eq 0
  469. then
  470. exec 3<&0
  471. else
  472. exec 3<<__EOF__
  473. `cat "$@"`
  474. __EOF__
  475. fi
  476. if iswindows
  477. then
  478. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  479. elif isdarwin
  480. then
  481. pbcopy 0<&3
  482. else
  483. 0<&3 xclip -i -f -selection "primary" | \
  484. xclip -i -f -selection "clipboard"
  485. fi
  486. exec 3<&-
  487. }
  488. open_file(){
  489. if iswindows
  490. then
  491. cmd.exe //c start "" "$@"
  492. elif isdarwin
  493. then
  494. touch "$@"
  495. open "$@"
  496. elif islinux
  497. then
  498. touch "$@"
  499. if null type pcmanfm; then
  500. LC_MESSAGES= pcmanfm "$@"
  501. else
  502. LC_MESSAGES= xdg-open "$@" &
  503. fi
  504. else
  505. cat "$@"
  506. fi
  507. }
  508. o(){
  509. if test $# -eq 0
  510. then
  511. open_file .
  512. else
  513. for f in "$@"
  514. do
  515. open_file "$(realpath "$f")"
  516. done
  517. fi
  518. }
  519. convmv-sjis2utf8-test(){
  520. convmv -r -f sjis -t utf8 *
  521. }
  522. convmv-sjis2utf8-notest(){
  523. convmv -r -f sjis -t utf8 * --notest
  524. }
  525. #Change ANSI Colors
  526. _chengecolors(){
  527. echo -e \
  528. "\e]P0000000" \
  529. "\e]P1cd0000" \
  530. "\e]P200cd00" \
  531. "\e]P3cdcd00" \
  532. "\e]P41e90ff" \
  533. "\e]P5cd00cd" \
  534. "\e]P600cdcd" \
  535. "\e]P7353535" \
  536. "\e]P8666666" \
  537. "\e]P9ff9999" \
  538. "\e]Pa99ff99" \
  539. "\e]Pbffff99" \
  540. "\e]Pc9999ff" \
  541. "\e]Pdff99ff" \
  542. "\e]Pe99ffff" \
  543. "\e]Pfffffff"
  544. }
  545. # printf "\e]P7353535" \
  546. _colors(){
  547. echo -e \
  548. "\e[30mBlack" \
  549. "\e[31mRed" \
  550. "\e[32mGreen" \
  551. "\e[33mYellow" \
  552. "\e[34mBlue" \
  553. "\e[35mMagenta" \
  554. "\e[36mCyan" \
  555. "\e[37mWhite"
  556. echo -e \
  557. "\e[30;1mBright Black" \
  558. "\e[31;1mBright Red" \
  559. "\e[32;1mBright Green" \
  560. "\e[33;1mBright Yellow" \
  561. "\e[34;1mBright Blue" \
  562. "\e[35;1mBright Magenta" \
  563. "\e[36;1mBright Cyan" \
  564. "\e[37;1mBright White\n" \
  565. "\e[0m"
  566. }
  567. winln(){
  568. # for windose make link (actually junction)
  569. if test $# -eq 0
  570. then
  571. {
  572. echo "usage: winln TARGET LINK_NAME"
  573. echo "Create a link to TARGET with the name LINK_NAME \
  574. (that is, TARGET must already exist)."
  575. echo "About other features run 'junction'."
  576. } 1>&2
  577. return 1
  578. else
  579. junction "$2" "$1"
  580. fi
  581. }
  582. __my_moc_state(){
  583. type mocp >/dev/null 2>&1 || return
  584. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  585. printf "$1" "`mocp -Q %title 2>/dev/null`"
  586. }
  587. __my_parse_svn_branch() {
  588. local LANG=C
  589. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  590. local svn_repository_root=$(svn info 2>/dev/null | \
  591. sed -ne 's#^Repository Root: ##p')
  592. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  593. awk '{print $1}'
  594. }
  595. __my_svn_ps1(){
  596. if svn status >/dev/null 2>&1
  597. then
  598. local svn_branch=$(__my_parse_svn_branch)
  599. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  600. fi
  601. }
  602. __my_battery_status(){
  603. local dir=/sys/class/power_supply/BAT0
  604. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  605. test -r $dir/charge_now
  606. then
  607. local st=$(cat $dir/status)
  608. local full=$(cat $dir/charge_full)
  609. local now=$(cat $dir/charge_now)
  610. local rate=$(expr $now \* 100 / $full)
  611. printf "$1" "${st}:${rate}%"
  612. fi
  613. }
  614. alias bat='__my_battery_status %s\\n'
  615. ip-address(){
  616. type ip >/dev/null 2>&1 || return 1
  617. local ip=$(LANG=C ip addr show scope global | \
  618. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  619. test -n "$ip" && printf $1 $ip
  620. }
  621. __my_ps1_script(){
  622. local last=$?
  623. test -n "$SCRIPT" && echo "${__my_c5}SCR${__my_cdef} "
  624. return $last
  625. }
  626. __my_ps1_scale(){
  627. local last=$?
  628. printf "${LINES}x${COLUMNS}"
  629. return $last
  630. }
  631. __my_ps1_tmux(){
  632. local last=$?
  633. null type tmux || return $last
  634. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  635. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  636. return $last
  637. }
  638. __my_ps1_moc(){
  639. local last=$?
  640. __my_moc_state "[MOC:%s]"
  641. return $last
  642. }
  643. for f in /usr/share/git/git-prompt.sh \
  644. /opt/local/share/git-core/git-prompt.sh \
  645. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  646. do
  647. test -r $f && . $f && break
  648. done
  649. GIT_PS1_SHOWDIRTYSTATE=t
  650. GIT_PS1_SHOWUPSTREAM=t
  651. __my_ps1_git(){
  652. local last=$?
  653. null type __git_ps1 || return $last
  654. null __gitdir || return $last
  655. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  656. return $last
  657. }
  658. __my_ps1_ipaddr(){
  659. local last=$?
  660. ! iswindows && ip-address [Addr:%s]
  661. return $last
  662. }
  663. __my_ps1_bttry(){
  664. local last=$?
  665. local bst="${TMP}/batterystatus"
  666. if test -z "$DISPLAY" && ! iswindows
  667. then
  668. test -f $bst && local bstr="$(cat $bst)"
  669. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  670. echo "[Battery:$bstr]"
  671. __my_battery_status %s >$bst &
  672. fi
  673. return $last
  674. }
  675. __my_ps1_dirs(){
  676. dirs | wc -l
  677. }
  678. __my_ps1_jobs(){
  679. jobs | wc -l
  680. }
  681. if test "$TERM" != dumb
  682. then
  683. __my_c1="\[\e[0;33m\]" # color for PWD
  684. __my_c2="\[\e[0;36m\]" # color for user
  685. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  686. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  687. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  688. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  689. else __my_c4="\[\e[1;32m\]" # color for ::
  690. fi
  691. __my_c5="\[\e[30;47m\]" # color for SCR
  692. __my_cdef="\[\e[0m\]"
  693. fi
  694. export _LAST_STATUS=$?
  695. __my_export_last_status(){
  696. export _LAST_STATUS=$?
  697. echo $_LAST_STATUS
  698. return $_LAST_STATUS
  699. }
  700. _PS1="\
  701. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  702. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} $(__my_ps1_script)\$ "
  703. PS1=$_PS1
  704. __my_set_screen_title(){
  705. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  706. then
  707. echo -ne "\033k$1\033\\"
  708. fi
  709. }
  710. __my_set_title(){
  711. case $TERM in
  712. (rxvt*|xterm*|aterm|screen*)
  713. title="$(echo $@)"
  714. test -t 1 &&
  715. test -n "$DISPLAY" &&
  716. test -z "$EMACS" &&
  717. echo -n -e "\033]0;${title}\007"
  718. ;;
  719. esac
  720. }
  721. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  722. __my_set_screen_title \$(basename \"\$PWD\")/"