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