選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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