Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

1125 wiersze
26 KiB

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