Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

975 righe
22 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. __match(){
  39. # __match str1 str2
  40. # return 0 if str2 is found in str1
  41. expr "$1" : ".*$2.*" >/dev/null
  42. }
  43. if ! __match "$PATH" "$HOME/.local/bin"
  44. then
  45. PATH="${PATH}:${HOME}/.local/bin:$HOME/.local/lib/gems/bin"
  46. fi
  47. if ismsys && ! __match "$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 ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  57. then
  58. export 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 -FS"
  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. __match "$TMP" "${USER}-tmp" >/dev/null || 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="exec \"$SHELL\""
  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 git rev-parse --git-dir >/dev/null 2>&1
  310. then
  311. echo ">> git grep -n $@" 1>&2
  312. git grep -n "$@"
  313. elif which ag >/dev/null 2>&1
  314. then
  315. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  316. ag --pager="$PAGER" "$@"
  317. elif which ack >/dev/null 2>&1
  318. then
  319. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  320. ack --pager="$PAGER" "$@"
  321. else
  322. echo \
  323. ">> find . " \
  324. "-path '*/.git' -prune -o" \
  325. "-path '*/.svn' -prune -o" \
  326. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  327. if test $# -eq 0
  328. then
  329. echo "No search word given." 1>&2
  330. return 1
  331. fi
  332. find . \
  333. -path '*/.git' -prune -o \
  334. -path '*/.svn' -prune -o \
  335. -type -f -exec grep -nH -e --color=always "$@" {} + \
  336. | $PAGER
  337. fi
  338. }
  339. man(){
  340. env \
  341. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  342. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  343. LESS_TERMCAP_me=$(printf "\e[0m") \
  344. LESS_TERMCAP_se=$(printf "\e[0m") \
  345. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  346. LESS_TERMCAP_ue=$(printf "\e[0m") \
  347. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  348. man "$@"
  349. }
  350. netwait(){
  351. while ! ping -c 1 -t 1 example.com
  352. do
  353. true
  354. done
  355. echo network works.
  356. }
  357. cd(){
  358. if test $# -eq 0
  359. then
  360. pushd ~/ >/dev/null
  361. elif test "$1" = -
  362. then
  363. local pwd="$PWD"
  364. command cd "$OLDPWD"
  365. pushd -n "$pwd" >/dev/null # stack last dir
  366. elif ! test -d "$1"
  367. then
  368. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  369. return 1
  370. else
  371. pushd "$1" >/dev/null
  372. fi
  373. __dirs_rm_dup "$PWD"
  374. echo "$PWD"
  375. }
  376. __dirs_rm_dup(){
  377. for d in "$@"
  378. do
  379. local next="$(__realpath --strip "$d")"
  380. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  381. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  382. do
  383. popd +$l -n >/dev/null
  384. done
  385. done
  386. }
  387. __realpath(){
  388. if type realpath >/dev/null 2>&1
  389. then
  390. command realpath "$@"
  391. else
  392. while ! test -d $1
  393. do
  394. shift
  395. done
  396. local d="$OLDPWD"
  397. command cd "$1"
  398. echo "$PWD"
  399. command cd "$d"
  400. fi
  401. }
  402. dh(){
  403. if test $# -eq 0
  404. then
  405. dirs -v -l
  406. else
  407. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  408. cd "$dir"
  409. fi
  410. }
  411. input(){
  412. local foo
  413. stty -echo
  414. read foo
  415. stty echo
  416. echo $foo
  417. }
  418. # tmux(){
  419. # if test $# -eq 0
  420. # then
  421. # (cd ~; command tmux start;)
  422. # if command tmux has -t main
  423. # then
  424. # command tmux attach -t main
  425. # else
  426. # (cd ~; command tmux new -s main;)
  427. # fi
  428. # else
  429. # command tmux "$@"
  430. # fi
  431. # }
  432. tx(){
  433. if test $# -eq 0
  434. then
  435. tmux ls
  436. echo "tx <session> to attach."
  437. elif tmux has -t "$1"
  438. then
  439. tmux attach -t "$1"
  440. else
  441. tmux new -s "$1"
  442. fi
  443. }
  444. dt(){
  445. # dt [<name>] [<command ...>]
  446. __dtach_dir="${TMP}/dtach"
  447. install -d "${__dtach_dir}"
  448. if test -n "${__MY_DTACH}"
  449. then
  450. echo "Current session: ${__MY_DTACH}"
  451. fi
  452. if test -z "$1"
  453. then
  454. echo "Sessions:"
  455. ls "${__dtach_dir}"
  456. return 0
  457. elif test "$1" = "-h"
  458. then
  459. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  460. return 1
  461. fi
  462. soc_name="${__dtach_dir}/$1"
  463. shift
  464. if test -n "$__MY_DTACH"
  465. then
  466. echo "dtach session cannot be nested." 1>&2
  467. return 1
  468. elif test -S "$soc_name"
  469. then
  470. dtach -a "$soc_name" -e ^^
  471. elif test -e "$soc_name"
  472. then
  473. echo "dt: File named $soc_name already exists."
  474. return 1
  475. elif test -z "$1"
  476. then
  477. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  478. # echo "dt: Socket named $soc_name not exists and no command specified."
  479. # return 1
  480. else
  481. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  482. fi
  483. }
  484. scr(){
  485. test -n "$1" && pf="${1}-"
  486. local _tformat="%Y%m%d-%H%M%S%z"
  487. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  488. __MY_SCRIPT=${_file} script ${_file} "$@"
  489. }
  490. mcrypt-stream(){
  491. test $# -eq 2 || return 1
  492. case $1 in
  493. en)
  494. mcrypt --key $2 | base64 ;;
  495. de)
  496. base64 -d | mcrypt -d --key $2 ;;
  497. esac
  498. }
  499. gpg-stream(){
  500. test $# -eq 2 || return 1
  501. case $1 in
  502. en)
  503. gpg --passphrase $2 -c --batch |base64 ;;
  504. de)
  505. base64 -d|gpg --passphrase $2 -d --batch ;;
  506. esac
  507. }
  508. dgpg(){
  509. if test "$1" = help || test -z "$2"
  510. then
  511. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  512. return
  513. fi
  514. local srcs="$2"
  515. local dsts="$3"
  516. test -z "$dsts" && dsts="${srcs}.out"
  517. local pw
  518. echo -n "dgpg pw: "
  519. read -s pw
  520. echo ""
  521. test -z "$pw" && return 1
  522. for f in *${srcs}
  523. do
  524. local d="$(basename "$f" "${srcs}")${dsts}"
  525. echo -n "Processing $f to $d..."
  526. if test -d "$f"
  527. then
  528. echo "`printf 'failed (%s is directory)' $f`"
  529. elif test -f "$d"
  530. then
  531. echo "`printf 'failed (%s is already exists)' $d`"
  532. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  533. then
  534. echo "done"
  535. else
  536. echo "failed"
  537. test -f "$d" && rm "$d"
  538. fi
  539. done
  540. }
  541. alias enst="gpg-stream en"
  542. alias dest="gpg-stream de"
  543. showinfo(){
  544. echo "Japanese letters are 表示可能"
  545. __try_exec diskinfo
  546. ! isdarwin && test -n "${DISPLAY}" && {
  547. __try_exec xrandr | \grep --color=never ^Screen
  548. }
  549. iswindows || __try_exec finger $USER
  550. LANG=C __try_exec id
  551. __try_exec xset q
  552. }
  553. x(){
  554. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  555. #mkdir -p ~/.my/log
  556. # nohup startx >~/.my/log/xorg.log 2>&1 &
  557. # exit
  558. exec startx
  559. else
  560. echo "X cant be started! Another X is already running?" 1>&2
  561. fi
  562. }
  563. bak(){
  564. for file in "$@"
  565. do
  566. cp -v ${file} ${file}.bak
  567. done
  568. }
  569. di(){
  570. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  571. then
  572. local diffcmd=colordiff
  573. else
  574. local diffcmd=diff
  575. fi
  576. ${diffcmd} -u "$@" | ${PAGER}
  577. }
  578. tb(){
  579. local tb="$HOME/.my/tb"
  580. mkdir -p "$tb"
  581. for file in "$@"
  582. do
  583. mv -t "$tb" "$file"
  584. done
  585. }
  586. mkcd(){
  587. if test -z "$1"
  588. then
  589. echo "mkcd: usage: mkcd <dir>"
  590. return 1
  591. elif test -d "$1"
  592. then
  593. echo "Dir \"$1\" already exists."
  594. else
  595. mkdir -p "$1"
  596. echo "Dir \"$1\" created."
  597. fi
  598. cd "$1"
  599. }
  600. mkcdd(){
  601. # make and change date directory
  602. _d=`date +%Y%m%d-%H%M%S`
  603. mkcd "$_d"
  604. }
  605. if test -n "$TMUX" && null type reattach-to-user-namespace
  606. then
  607. alias pbpaste="reattach-to-user-namespace pbpaste"
  608. alias pbcopy="reattach-to-user-namespace pbcopy"
  609. fi
  610. catclip(){
  611. if iswindows
  612. then
  613. cat /dev/clipboard | tr -d \\r
  614. elif isdarwin
  615. then
  616. pbpaste
  617. else
  618. xclip -o -selection "clipboard"
  619. fi
  620. }
  621. setclip(){
  622. if test $# -eq 0
  623. then
  624. exec 3<&0
  625. else
  626. exec 3<<__EOF__
  627. `cat "$@"`
  628. __EOF__
  629. fi
  630. if iswindows
  631. then
  632. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  633. elif isdarwin
  634. then
  635. pbcopy 0<&3
  636. else
  637. 0<&3 xclip -i -f -selection "primary" | \
  638. xclip -i -f -selection "clipboard"
  639. fi
  640. exec 3<&-
  641. }
  642. open_file(){
  643. if iswindows
  644. then
  645. cmd.exe //c start "" "$@"
  646. elif isdarwin
  647. then
  648. touch "$@"
  649. open "$@"
  650. elif islinux
  651. then
  652. touch "$@"
  653. if null type pcmanfm; then
  654. LC_MESSAGES= pcmanfm "$@"
  655. else
  656. LC_MESSAGES= xdg-open "$@" &
  657. fi
  658. else
  659. cat "$@"
  660. fi
  661. }
  662. o(){
  663. if test $# -eq 0
  664. then
  665. open_file .
  666. else
  667. for f in "$@"
  668. do
  669. open_file "$(realpath "$f")"
  670. done
  671. fi
  672. }
  673. convmv-sjis2utf8-test(){
  674. convmv -r -f sjis -t utf8 *
  675. }
  676. convmv-sjis2utf8-notest(){
  677. convmv -r -f sjis -t utf8 * --notest
  678. }
  679. winln(){
  680. # for windose make link (actually junction)
  681. if test $# -eq 0
  682. then
  683. {
  684. echo "usage: winln TARGET LINK_NAME"
  685. echo "Create a link to TARGET with the name LINK_NAME \
  686. (that is, TARGET must already exist)."
  687. echo "About other features run 'junction'."
  688. } 1>&2
  689. return 1
  690. else
  691. junction "$2" "$1"
  692. fi
  693. }
  694. #################################################
  695. ## pastebin services
  696. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  697. sprunge(){
  698. # http://sprunge.us
  699. if test -z "$1"
  700. then
  701. curl -F 'sprunge=<-' http://sprunge.us
  702. else
  703. curl http://sprunge.us/$1
  704. fi
  705. }
  706. dpaste(){
  707. # http://dpaste.de
  708. if test -z "$1"
  709. then
  710. curl -F 'content=<-' https://dpaste.de/api/
  711. echo
  712. else
  713. curl https://dpaste.de/$1/raw/
  714. fi
  715. }
  716. ######################################
  717. ## Prompt
  718. __my_moc_state(){
  719. type mocp >/dev/null 2>&1 || return
  720. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  721. printf "$1" "`mocp -Q %title 2>/dev/null`"
  722. }
  723. __my_parse_svn_branch() {
  724. local LANG=C
  725. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  726. local svn_repository_root=$(svn info 2>/dev/null | \
  727. sed -ne 's#^Repository Root: ##p')
  728. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  729. awk '{print $1}'
  730. }
  731. __my_svn_ps1(){
  732. if svn status >/dev/null 2>&1
  733. then
  734. local svn_branch=$(__my_parse_svn_branch)
  735. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  736. fi
  737. }
  738. __my_battery_status(){
  739. local dir=/sys/class/power_supply/BAT0
  740. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  741. test -r $dir/charge_now
  742. then
  743. local st=$(cat $dir/status)
  744. local full=$(cat $dir/charge_full)
  745. local now=$(cat $dir/charge_now)
  746. local rate=$(expr $now \* 100 / $full)
  747. printf "$1" "${st}:${rate}%"
  748. fi
  749. }
  750. alias bat='__my_battery_status %s\\n'
  751. ip-address(){
  752. type ip >/dev/null 2>&1 || return 1
  753. local ip=$(LANG=C ip addr show scope global | \
  754. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  755. test -n "$ip" && printf $1 $ip
  756. }
  757. __my_ps1_str=""
  758. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  759. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  760. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  761. __my_ps1_scale(){
  762. local last=$?
  763. if null type stty && ! ismsys
  764. then
  765. stty size | tr -d $'\n' | tr " " x
  766. printf " "
  767. fi
  768. return $last
  769. }
  770. __my_ps1_tmux(){
  771. local last=$?
  772. null type tmux || return $last
  773. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  774. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  775. return $last
  776. }
  777. __my_ps1_moc(){
  778. local last=$?
  779. __my_moc_state "[MOC:%s]"
  780. return $last
  781. }
  782. for f in /usr/share/git/git-prompt.sh \
  783. /opt/local/share/git-core/git-prompt.sh \
  784. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  785. do
  786. test -r "$f" && . "$f" && break
  787. done
  788. GIT_PS1_SHOWDIRTYSTATE=t
  789. GIT_PS1_SHOWUPSTREAM=t
  790. __my_ps1_git(){
  791. local last=$?
  792. null type __git_ps1 || return $last
  793. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  794. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  795. return $last
  796. }
  797. __my_ps1_ipaddr(){
  798. local last=$?
  799. ! iswindows && ip-address [Addr:%s]
  800. return $last
  801. }
  802. __my_ps1_bttry(){
  803. local last=$?
  804. local bst="${TMP}/batterystatus"
  805. if test -z "$DISPLAY" && ! iswindows
  806. then
  807. test -f $bst && local bstr="$(cat $bst)"
  808. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  809. echo "[Battery:$bstr]"
  810. __my_battery_status %s >$bst &
  811. fi
  812. return $last
  813. }
  814. __my_ps1_dirs(){
  815. dirs | wc -l
  816. }
  817. __my_ps1_jobs(){
  818. jobs | wc -l
  819. }
  820. if test "$TERM" != dumb
  821. then
  822. __my_c1="\[\e[0;33m\]" # color for PWD
  823. __my_c2="\[\e[0;36m\]" # color for user
  824. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  825. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  826. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  827. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  828. else __my_c4="\[\e[1;32m\]" # color for ::
  829. fi
  830. __my_c5="\[\e[30;47m\]" # color for SCR
  831. __my_cdef="\[\e[0m\]"
  832. fi
  833. # export _LAST_STATUS=a
  834. # __my_export_last_status(){
  835. # local last=$?
  836. # echo $last
  837. # export _LAST_STATUS=$last
  838. # echo $_LAST_STATUS
  839. # return $last
  840. # }
  841. _ps1_bash="\
  842. ${__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\
  843. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_str}\$ "
  844. inbash && PS1=$_ps1_bash
  845. __my_set_screen_title(){
  846. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  847. then
  848. echo -ne "\033k$1\033\\"
  849. fi
  850. }
  851. __my_set_title(){
  852. case $TERM in
  853. (rxvt*|xterm*|aterm|screen*)
  854. title="$(echo $@)"
  855. test -t 1 &&
  856. test -n "$DISPLAY" &&
  857. test -z "$EMACS" &&
  858. echo -n -e "\033]0;${title}\007"
  859. ;;
  860. esac
  861. }
  862. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  863. __my_set_screen_title \$(basename \"\$PWD\")/"