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
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
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
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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. #################################
  28. if echo $PATH | grep "$HOME" >/dev/null 2>&1
  29. then
  30. PATH="${PATH}:${HOME}/.local/bin"
  31. fi
  32. if false iswindows
  33. then
  34. export PAGER='tr -d \\r | less'
  35. else
  36. export PAGER="less"
  37. fi
  38. export LESS="-iRMX"
  39. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  40. if test -n "$_src_hilite_lp_path"
  41. then
  42. export LESSOPEN="| $_src_hilite_lp_path %s"
  43. fi
  44. if null type vim
  45. then
  46. export EDITOR=vim
  47. else
  48. export EDITOR=vi
  49. fi
  50. export LANG=ja_JP.UTF-8
  51. export LC_MESSAGES=C
  52. # export CDPATH=".:~"
  53. export VISUAL="$EDITOR"
  54. export GIT_PAGER="less -F"
  55. export GIT_EDITOR="$EDITOR"
  56. if test -n "$TMUX" && \
  57. echo $TERM | grep screen >/dev/null 2>&1 && \
  58. tmux display -p '#{client_termname}' | grep 256color >/dev/null 2>&1
  59. then
  60. TERM=screen-256color
  61. fi
  62. test -z "$TMP" && export TMP=/tmp/${USER}-tmp
  63. mkdir -p "$TMP"
  64. ! iswindows && null type stty && {
  65. stty stop undef # unbind C-s to stop displaying output
  66. # stty erase '^h'
  67. }
  68. if iswindows; then
  69. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  70. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  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 pacman-color && {
  193. alias pacman=pacman-color
  194. export pacman_program=pacman-color # used by pacmatic
  195. export PACMAN=pacman-color # used by yaourt
  196. }
  197. null type pacmatic && {
  198. alias pacman="pacmatic"
  199. export PACMAN="pacmatic"
  200. }
  201. null type apt-get && {
  202. alias aupgrade="sudo apt-get autoremove --yes && \
  203. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  204. alias aptin="apt-get install"
  205. alias aptsearch="apt-cache search"
  206. alias aptshow="apt-cache show"
  207. }
  208. null type port && {
  209. alias port="port -v"
  210. alias pupgrade="sudo port -v selfupdate && \
  211. { sudo port -v upgrade outdated; }"
  212. }
  213. if iscygwin; then
  214. null type windate || \
  215. alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  216. alias cygsu="cygstart /cygwinsetup.exe"
  217. alias emacs="CYGWIN=tty emacs -nw"
  218. alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  219. fi
  220. g(){
  221. if test $# -eq 0 && null type git-info
  222. then
  223. git info
  224. else
  225. git "$@"
  226. fi
  227. }
  228. if null type _git
  229. then
  230. # enable programmable completion for g
  231. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  232. || complete -o default -o nospace -F _git g
  233. fi
  234. alias setup.py="sudo python3 setup.py install --record files.txt"
  235. ssh(){
  236. __my_set_screen_title ssh
  237. command ssh "$@"
  238. }
  239. clk(){
  240. local tformat="%Y/%m/%d %H:%M:%S %z"
  241. cal
  242. while true
  243. do
  244. printf "\\r`date "+${tformat}"`"
  245. sleep 1
  246. done
  247. }
  248. man(){
  249. env \
  250. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  251. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  252. LESS_TERMCAP_me=$(printf "\e[0m") \
  253. LESS_TERMCAP_se=$(printf "\e[0m") \
  254. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  255. LESS_TERMCAP_ue=$(printf "\e[0m") \
  256. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  257. man "$@"
  258. }
  259. scr(){
  260. test -n "$1" && pf="${1}-"
  261. local _tformat="%Y%m%d-%H%M%S%z"
  262. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  263. SCRIPT=${_file} script ${_file} "$@"
  264. }
  265. netwait(){
  266. while ! ping -c 1 -t 1 example.com
  267. do
  268. true
  269. done
  270. echo network works.
  271. }
  272. cd(){
  273. if test $# -eq 0
  274. then
  275. pushd ~/ >/dev/null
  276. elif test "$1" = -
  277. then
  278. local pwd="$PWD"
  279. command cd "$OLDPWD"
  280. pushd -n "$pwd" >/dev/null # stack last dir
  281. elif ! test -d "$1"
  282. then
  283. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  284. return 1
  285. else
  286. pushd "$1" >/dev/null
  287. fi
  288. __dirs_rm_dup "$PWD"
  289. echo "$PWD"
  290. }
  291. __dirs_rm_dup(){
  292. for d in "$@"
  293. do
  294. local next="$(__realpath --strip "$d")"
  295. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  296. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  297. do
  298. popd +$l -n >/dev/null
  299. done
  300. done
  301. }
  302. __realpath(){
  303. if type realpath >/dev/null 2>&1
  304. then
  305. command realpath "$@"
  306. else
  307. while ! test -d $1
  308. do
  309. shift
  310. done
  311. local d="$OLDPWD"
  312. command cd "$1"
  313. echo "$PWD"
  314. command cd "$d"
  315. fi
  316. }
  317. dh(){
  318. if test $# -eq 0
  319. then
  320. dirs -v -l
  321. else
  322. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  323. cd "$dir"
  324. fi
  325. }
  326. input(){
  327. local foo
  328. stty -echo
  329. read foo
  330. stty echo
  331. echo $foo
  332. }
  333. tmux(){
  334. if test $# -eq 0
  335. then
  336. (cd ~; command tmux start;)
  337. if command tmux has -t main
  338. then
  339. command tmux attach -t main
  340. else
  341. (cd ~; command tmux new -s main;)
  342. fi
  343. else
  344. command tmux "$@"
  345. fi
  346. }
  347. mcrypt-stream(){
  348. test $# -eq 2 || return 1
  349. case $1 in
  350. en)
  351. mcrypt --key $2 | base64 ;;
  352. de)
  353. base64 -d | mcrypt -d --key $2 ;;
  354. esac
  355. }
  356. gpg-stream(){
  357. test $# -eq 2 || return 1
  358. case $1 in
  359. en)
  360. gpg --passphrase $2 -c --batch |base64 ;;
  361. de)
  362. base64 -d|gpg --passphrase $2 -d --batch ;;
  363. esac
  364. }
  365. dgpg(){
  366. if test "$1" = help || test -z "$2"
  367. then
  368. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  369. return
  370. fi
  371. local srcs="$2"
  372. local dsts="$3"
  373. test -z "$dsts" && dsts="${srcs}.out"
  374. local pw
  375. echo -n "dgpg pw: "
  376. read -s pw
  377. echo ""
  378. test -z "$pw" && return 1
  379. for f in *${srcs}
  380. do
  381. local d="$(basename "$f" "${srcs}")${dsts}"
  382. echo -n "Processing $f to $d..."
  383. if test -d "$f"
  384. then
  385. echo "`printf 'failed (%s is directory)' $f`"
  386. elif test -f "$d"
  387. then
  388. echo "`printf 'failed (%s is already exists)' $d`"
  389. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  390. then
  391. echo "done"
  392. else
  393. echo "failed"
  394. test -f "$d" && rm "$d"
  395. fi
  396. done
  397. }
  398. alias enst="gpg-stream en"
  399. alias dest="gpg-stream de"
  400. showinfo(){
  401. echo "Japanese letters are 表示可能"
  402. __try_exec diskinfo
  403. ! isdarwin && test -n "${DISPLAY}" && {
  404. __try_exec xrandr | \grep --color=never ^Screen
  405. }
  406. iswindows || __try_exec finger $USER
  407. LANG=C __try_exec id
  408. __try_exec xset q
  409. }
  410. x(){
  411. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  412. #mkdir -p ~/.my/log
  413. # nohup startx >~/.my/log/xorg.log 2>&1 &
  414. # exit
  415. exec startx
  416. else
  417. echo "X cant be started! Another X is already running?" 1>&2
  418. fi
  419. }
  420. bak(){
  421. for file in "$@"
  422. do
  423. cp -v ${file} ${file}.bak
  424. done
  425. }
  426. di(){
  427. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  428. then
  429. local diffcmd=colordiff
  430. else
  431. local diffcmd=diff
  432. fi
  433. ${diffcmd} -u "$@" | ${PAGER}
  434. }
  435. tb(){
  436. local tb="$HOME/.my/tb"
  437. mkdir -p "$tb"
  438. for file in "$@"
  439. do
  440. mv -t "$tb" "$file"
  441. done
  442. }
  443. mkcd(){
  444. if test -d "$1"
  445. then
  446. echo "Dir \"$1\" already exists."
  447. else
  448. mkdir -p "$1"
  449. echo "Dir \"$1\" created."
  450. fi
  451. cd "$1"
  452. }
  453. if test -n "$TMUX" && null type reattach-to-user-namespace
  454. then
  455. alias pbpaste="reattach-to-user-namespace pbpaste"
  456. alias pbcopy="reattach-to-user-namespace pbcopy"
  457. fi
  458. catclip(){
  459. if iswindows
  460. then
  461. cat /dev/clipboard | tr -d \\r
  462. elif isdarwin
  463. then
  464. pbpaste
  465. else
  466. xclip -o -selection "clipboard"
  467. fi
  468. }
  469. setclip(){
  470. if test $# -eq 0
  471. then
  472. exec 3<&0
  473. else
  474. exec 3<<__EOF__
  475. `cat "$@"`
  476. __EOF__
  477. fi
  478. if iswindows
  479. then
  480. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  481. elif isdarwin
  482. then
  483. pbcopy 0<&3
  484. else
  485. 0<&3 xclip -i -f -selection "primary" | \
  486. xclip -i -f -selection "clipboard"
  487. fi
  488. exec 3<&-
  489. }
  490. open_file(){
  491. if iswindows
  492. then
  493. cmd.exe //c start "" "$@"
  494. elif isdarwin
  495. then
  496. touch "$@"
  497. open "$@"
  498. elif islinux
  499. then
  500. touch "$@"
  501. if null type pcmanfm; then
  502. LC_MESSAGES= pcmanfm "$@"
  503. else
  504. LC_MESSAGES= xdg-open "$@" &
  505. fi
  506. else
  507. cat "$@"
  508. fi
  509. }
  510. o(){
  511. if test $# -eq 0
  512. then
  513. open_file .
  514. else
  515. for f in "$@"
  516. do
  517. open_file "$(realpath "$f")"
  518. done
  519. fi
  520. }
  521. convmv-sjis2utf8-test(){
  522. convmv -r -f sjis -t utf8 *
  523. }
  524. convmv-sjis2utf8-notest(){
  525. convmv -r -f sjis -t utf8 * --notest
  526. }
  527. #Change ANSI Colors
  528. _chengecolors(){
  529. echo -e \
  530. "\e]P0000000" \
  531. "\e]P1cd0000" \
  532. "\e]P200cd00" \
  533. "\e]P3cdcd00" \
  534. "\e]P41e90ff" \
  535. "\e]P5cd00cd" \
  536. "\e]P600cdcd" \
  537. "\e]P7353535" \
  538. "\e]P8666666" \
  539. "\e]P9ff9999" \
  540. "\e]Pa99ff99" \
  541. "\e]Pbffff99" \
  542. "\e]Pc9999ff" \
  543. "\e]Pdff99ff" \
  544. "\e]Pe99ffff" \
  545. "\e]Pfffffff"
  546. }
  547. # printf "\e]P7353535" \
  548. _colors(){
  549. echo -e \
  550. "\e[30mBlack" \
  551. "\e[31mRed" \
  552. "\e[32mGreen" \
  553. "\e[33mYellow" \
  554. "\e[34mBlue" \
  555. "\e[35mMagenta" \
  556. "\e[36mCyan" \
  557. "\e[37mWhite"
  558. echo -e \
  559. "\e[30;1mBright Black" \
  560. "\e[31;1mBright Red" \
  561. "\e[32;1mBright Green" \
  562. "\e[33;1mBright Yellow" \
  563. "\e[34;1mBright Blue" \
  564. "\e[35;1mBright Magenta" \
  565. "\e[36;1mBright Cyan" \
  566. "\e[37;1mBright White\n" \
  567. "\e[0m"
  568. }
  569. winln(){
  570. # for windose make link (actually junction)
  571. if test $# -eq 0
  572. then
  573. {
  574. echo "usage: winln TARGET LINK_NAME"
  575. echo "Create a link to TARGET with the name LINK_NAME \
  576. (that is, TARGET must already exist)."
  577. echo "About other features run 'junction'."
  578. } 1>&2
  579. return 1
  580. else
  581. junction "$2" "$1"
  582. fi
  583. }
  584. __my_moc_state(){
  585. type mocp >/dev/null 2>&1 || return
  586. test "`mocp -Q %state 2>/dev/null`" == PLAY || return
  587. printf "$1" "`mocp -Q %title 2>/dev/null`"
  588. }
  589. __my_parse_svn_branch() {
  590. local LANG=C
  591. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  592. local svn_repository_root=$(svn info 2>/dev/null | \
  593. sed -ne 's#^Repository Root: ##p')
  594. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  595. awk '{print $1}'
  596. }
  597. __my_svn_ps1(){
  598. if svn status >/dev/null 2>&1
  599. then
  600. local svn_branch=$(__my_parse_svn_branch)
  601. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  602. fi
  603. }
  604. __my_battery_status(){
  605. local dir=/sys/class/power_supply/BAT0
  606. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  607. test -r $dir/charge_now
  608. then
  609. local st=$(cat $dir/status)
  610. local full=$(cat $dir/charge_full)
  611. local now=$(cat $dir/charge_now)
  612. local rate=$(expr $now \* 100 / $full)
  613. printf "$1" "${st}:${rate}%"
  614. fi
  615. }
  616. alias bat='__my_battery_status %s\\n'
  617. ip-address(){
  618. type ip >/dev/null 2>&1 || return 1
  619. local ip=$(LANG=C ip addr show scope global | \
  620. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  621. test -n "$ip" && printf $1 $ip
  622. }
  623. __my_ps1_script(){
  624. local last=$?
  625. test -n "$SCRIPT" && echo "${__my_c5}SCR${__my_cdef} "
  626. return $last
  627. }
  628. __my_ps1_scale(){
  629. local last=$?
  630. printf "${LINES}x${COLUMNS}"
  631. return $last
  632. }
  633. __my_ps1_tmux(){
  634. local last=$?
  635. null type tmux || return $last
  636. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  637. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  638. return $last
  639. }
  640. __my_ps1_moc(){
  641. local last=$?
  642. __my_moc_state "[MOC:%s]"
  643. return $last
  644. }
  645. for f in /usr/share/git/git-prompt.sh \
  646. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  647. do
  648. test -r $f && . $f && break
  649. done
  650. GIT_PS1_SHOWDIRTYSTATE=t
  651. GIT_PS1_SHOWUPSTREAM=t
  652. __my_ps1_git(){
  653. local last=$?
  654. null type __git_ps1 || return $last
  655. null __gitdir || return $last
  656. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  657. return $last
  658. }
  659. __my_ps1_ipaddr(){
  660. local last=$?
  661. ! iswindows && ip-address [Addr:%s]
  662. return $last
  663. }
  664. __my_ps1_bttry(){
  665. local last=$?
  666. local bst="${TMP}/batterystatus"
  667. if test -z "$DISPLAY" && ! iswindows
  668. then
  669. test -f $bst && local bstr="$(cat $bst)"
  670. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  671. echo "[Battery:$bstr]"
  672. __my_battery_status %s >$bst &
  673. fi
  674. return $last
  675. }
  676. __my_ps1_dirs(){
  677. dirs | wc -l
  678. }
  679. __my_ps1_jobs(){
  680. jobs | wc -l
  681. }
  682. if test "$TERM" != dumb
  683. then
  684. __my_c1="\[\e[0;33m\]" # color for PWD
  685. __my_c2="\[\e[0;36m\]" # color for user
  686. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  687. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  688. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  689. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  690. else __my_c4="\[\e[1;32m\]" # color for ::
  691. fi
  692. __my_c5="\[\e[30;47m\]" # color for SCR
  693. __my_cdef="\[\e[0m\]"
  694. fi
  695. export _LAST_STATUS=$?
  696. __my_export_last_status(){
  697. export _LAST_STATUS=$?
  698. echo $_LAST_STATUS
  699. return $_LAST_STATUS
  700. }
  701. _PS1="\
  702. ${__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\
  703. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} $(__my_ps1_script)\$ "
  704. PS1=$_PS1
  705. __my_set_screen_title(){
  706. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  707. then
  708. echo -ne "\033k$1\033\\"
  709. fi
  710. }
  711. __my_set_title(){
  712. case $TERM in
  713. (rxvt*|xterm*|aterm|screen*)
  714. title="$(echo $@)"
  715. test -t 1 &&
  716. test -n "$DISPLAY" &&
  717. test -z "$EMACS" &&
  718. echo -n -e "\033]0;${title}\007"
  719. ;;
  720. esac
  721. }
  722. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  723. __my_set_screen_title \$(basename \"\$PWD\")/"