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.
 
 
 
 
 
 

885 lines
20 KiB

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