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