25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1289 lines
30 KiB

  1. #!/bin/sh
  2. # TODO: decide the naming scheme of variables (global, local, ...)
  3. expr "$-" : '^.*i' >/dev/null || return
  4. ##########################################
  5. __shrc_lock=/tmp/shrc_lock.$USER.$$
  6. test -f "$__shrc_lock" && return
  7. touch "$__shrc_lock"
  8. ##########################################
  9. null(){
  10. "$@" >/dev/null 2>&1
  11. }
  12. __safe_run(){
  13. type $1 >/dev/null 2>&1 && "$@"
  14. }
  15. __match(){
  16. # __match str word
  17. # return 0 if word is found in str
  18. expr "$1" : ".*$2.*" >/dev/null
  19. }
  20. __lf=$'\n'
  21. __cr=$'\r'
  22. test -n "$TMP" || export TMP=/tmp/${USER}-tmp
  23. mkdir -p "$TMP"
  24. __homerun="$HOME/.var/run"
  25. mkdir -p "$__homerun"
  26. ##########################
  27. # system type
  28. gnu_coreutils=false # for mac
  29. null ls --version && gnu_coreutils=true
  30. inbash=false
  31. inzsh=false
  32. if test -n "$BASH_VERSION"
  33. then
  34. inbash=true
  35. elif test -n "$ZSH_VERSION"
  36. then
  37. inzsh=true
  38. fi
  39. #################################
  40. # file pathes:
  41. # shrc: Path to this file
  42. if $inbash
  43. then
  44. __shrc="$BASH_SOURCE"
  45. elif $inzsh
  46. then
  47. __shrc="$0"
  48. fi
  49. #########################
  50. # system detection
  51. __system_shrc="$__homerun/system.shrc"
  52. if test -f "$__system_shrc"
  53. then
  54. . "$__system_shrc"
  55. else
  56. ismsys=false
  57. iscygwin=false
  58. iswindows=false
  59. isdarwin=false
  60. isfreebsd=false
  61. isbsd=false
  62. islinux=false
  63. # $OSTYPE is another choice. which is better?
  64. # NOTE: sh on FreeBSD does not define OSTYPE
  65. case `uname` in
  66. MINGW*) ismsys=true ;;
  67. CYGWIN*) iscygwin=true ;;
  68. Darwin*) isdarwin=true ;;
  69. FreeBSD*) isfreebsd=true ;;
  70. Linux*) islinux=true ;;
  71. esac
  72. ($ismsys || $iscygwin) && iswindows=true
  73. # is this true?
  74. ($isdarwin || $isfreebsd) && isbsd=true
  75. # dump system detection result
  76. cat <<__EOC__ >"$__system_shrc"
  77. #!/bin/sh
  78. # $__system_shrc
  79. ismsys=$ismsys
  80. iscygwin=$iscygwin
  81. iswindows=$iswindows
  82. isdarwin=$isdarwin
  83. isfreebsd=$isfreebsd
  84. isbsd=$isbsd
  85. islinux=$islinux
  86. __EOC__
  87. fi
  88. $ismsys && export HOSTNAME
  89. # adhoc fix for ansible on cygwin
  90. # http://blog.s-uni.net/2013/08/27/ansible-running-on-cygwin/
  91. if $iscygwin
  92. then
  93. export ANSIBLE_SSH_ARGS="-o ControlMaster=no"
  94. fi
  95. ##########################
  96. # Terminal setups
  97. if ! $iswindows && null type stty
  98. then
  99. stty stop undef # unbind C-s to stop displaying output
  100. # stty erase '^h'
  101. fi
  102. # Zsh specific preferences
  103. # http://www.clear-code.com/blog/2011/9/5.html
  104. if $inzsh
  105. then
  106. bindkey -e
  107. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  108. autoload compinit; compinit
  109. # supress cycle by tab
  110. unsetopt auto_menu
  111. # unsetopt correct
  112. setopt complete_aliases
  113. setopt auto_list
  114. setopt bash_auto_list
  115. setopt magic_equal_subst
  116. setopt list_types
  117. # what is the difference of these two?
  118. setopt auto_param_slash
  119. setopt mark_dirs
  120. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  121. zstyle ':completion:*' format '%B%d%b'
  122. zstyle ':completion:*' group-name ''
  123. zstyle ':completion:*' use-cache yes
  124. # zstyle ':completion:*:cd:*' tag-order local-directories
  125. zstyle ':completion:*' completer _complete _bash_completions \
  126. _history
  127. # zstyle ':completion:*:*:cd:*' completer
  128. zstyle ':completion:*' accept-exact-dirs true
  129. zstyle ':completion:*' special-dirs true
  130. autoload colors; colors
  131. # autoload -Uz promptinit
  132. # promptinit
  133. # prompt walters
  134. setopt hist_ignore_dups
  135. setopt hist_ignore_all_dups
  136. setopt hist_save_no_dups
  137. setopt extended_history
  138. setopt share_history
  139. setopt append_history
  140. HISTFILE=$HOME/.zsh-history
  141. HISTSIZE=100000
  142. SAVEHIST=100000
  143. setopt prompt_subst
  144. setopt interactive_comments
  145. fi
  146. ######################
  147. # Print welcome messages
  148. if command -v lolcat >/dev/null
  149. then
  150. alias __safe_lolcat=lolcat
  151. else
  152. alias __safe_lolcat=cat
  153. fi
  154. command -v fortune >/dev/null && {
  155. if command -v cowsay >/dev/null
  156. then
  157. fortune | cowsay | __safe_lolcat
  158. echo
  159. fortune -o | cowsay | __safe_lolcat
  160. echo
  161. else
  162. fortune | __safe_lolcat
  163. echo
  164. fortune -o | __safe_lolcat
  165. echo
  166. fi
  167. }
  168. if null type figlet && null type lolcat
  169. then
  170. hostname -s | figlet | lolcat
  171. fi
  172. uname -a
  173. $iswindows && alias tty="echo cmd.exe"
  174. echo TERM $TERM with $(tput colors) colors using $(tty)
  175. echo umask: `umask -S`
  176. echo
  177. __safe_run w yuk
  178. # if null type tmux
  179. # then
  180. # echo
  181. # echo tmux sessions:
  182. # tmux ls 2>/dev/null| sed -e 's/^/ /g'
  183. # echo
  184. # fi
  185. # if test -n "$TMUX"
  186. # then
  187. # tmux display -p \
  188. # 'TMUX #S:#I:#W.#P, client TERM: #{client_termname}' \
  189. # 2>/dev/null
  190. # echo
  191. # fi
  192. if test -n "$__MY_DTACH"
  193. then
  194. echo
  195. echo DSession: Current Session: "`basename "$__MY_DTACH"`"
  196. echo DSession: Press C-^ to Exit.
  197. true
  198. fi
  199. i(){
  200. w
  201. ss -lnp
  202. ps aufx
  203. df -h
  204. top -b -d 1 -n 1
  205. top -b -d 1 -n 1 -a
  206. dstat -taf 1 10
  207. }
  208. ###################################
  209. # some aliases and functions
  210. # __func_name: never used interactively
  211. # _func_name: usually not used interactively
  212. __safe_alias(){
  213. # __safe_alias <name>=<command>
  214. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  215. test -n "$_bin" && \
  216. null type $_bin && \
  217. alias "$1"
  218. }
  219. $gnu_coreutils && _timeoption=" --time-style=long-iso"
  220. # color prefs
  221. if $gnu_coreutils
  222. then
  223. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  224. # gnu coreutils LS_COLORS is used
  225. null type dircolors && eval `dircolors`
  226. _coloroption=" --color=auto"
  227. else
  228. # export LSCOLORS=gxfxcxdxbxegedabagacad
  229. export LSCOLORS=DxGxcxdxCxegedabagacad
  230. export CLICOLOR=1
  231. fi
  232. alias ls="ls -hCF${_coloroption}${_timeoption}"
  233. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  234. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  235. _timeformat_num="%Y%m%d%H%M%S"
  236. alias datenum="date +$_timeformat_num"
  237. alias less="less -FX"
  238. __safe_alias em="emacs -nw"
  239. test -n "$_VI_ALT" && alias vi=$_VI_ALT
  240. alias pstree="LANG=C pstree"
  241. alias cp="cp -v"
  242. alias mv="mv -v"
  243. alias rm="rm -v"
  244. alias mkdir="mkdir -v"
  245. $gnu_coreutils && alias rmdir="rmdir -v"
  246. alias psaux="ps auxww"
  247. alias modest="ionice -c 2 -n 7 nice -n 19"
  248. alias e=exec
  249. __safe_alias e3=e3em
  250. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  251. alias po=popd
  252. alias pu=pushd
  253. __safe_alias sudoe="sudoedit"
  254. __safe_alias ge="git exec"
  255. __safe_alias make="nice make"
  256. __safe_alias brew="nice brew"
  257. __safe_alias gcc="nice gcc"
  258. __safe_alias go="nice go"
  259. __safe_alias git="nice git"
  260. __safe_alias javac="nice javac -J-Duser.language=en"
  261. __safe_alias ansible-playbook="nice ansible-playbook"
  262. alias rand="echo \$RANDOM"
  263. alias pycalc="python -i -c 'from math import *' "
  264. __safe_alias py3=python3
  265. __safe_alias py2=python2
  266. __safe_alias ipy=ipython
  267. __safe_alias ipy3=ipython3
  268. __safe_alias ipy2=ipython2
  269. __safe_alias rl=rlwrap
  270. __safe_alias igosh=gosh-rl
  271. # Sometimes SHELL cannot be used. For example, when running bash inside zsh
  272. # SHELL is set to be /bin/zsh
  273. if $inbash
  274. then
  275. alias _reloadrc="rm '$__shrc_lock'; exec bash"
  276. elif $inzsh
  277. then
  278. alias _reloadrc="rm '$__shrc_lock'; exec zsh"
  279. else
  280. alias _reloadrc="rm '$__shrc_lock'; exec $SHELL"
  281. fi
  282. # alias mytime="date +%Y%m%d-%H%M%S"
  283. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  284. # some extra utilities
  285. __safe_alias mpg123="mpg123 -C -v --title"
  286. #export PLAYER="mpg123 -C -v --title"
  287. __safe_alias screen="screen -e^z^z"
  288. #alias zcd="cd \`zenity --file-selection --directory\`"
  289. __safe_alias gtags="gtags --verbose"
  290. __safe_alias htags="htags --xhtml --symbol --line-number \
  291. --frame --alphabet --verbose"
  292. __safe_alias au=aunpack
  293. # __safe_alias lv="lv|less"
  294. __safe_alias rs="rsync --progress --itemize-changes --compress"
  295. __safe_alias vagr=vagrant
  296. __safe_alias ztw='twitter set "`zenity --entry --title ztw --text Status:`"'
  297. if $iscygwin
  298. then
  299. __my_wget_options=" --no-check-certificate"
  300. __safe_alias wget="wget $__my_wget_options"
  301. fi
  302. if $isdarwin
  303. then
  304. alias updatedb="LC_ALL=C updatedb"
  305. # do not use locate installed by macports
  306. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  307. fi
  308. # somehow not work
  309. # typeset -ga chpwd_functions
  310. # chpwd_functions+=pwd
  311. # chpwd(){
  312. # pwd
  313. # }
  314. cd(){
  315. builtin cd "$@" && pwd
  316. }
  317. root(){
  318. if test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = true
  319. then
  320. cd "`git rev-parse --show-toplevel`"
  321. else
  322. cd /
  323. fi
  324. }
  325. __find_latest_vimdir(){
  326. vimdir=/usr/share/vim
  327. if test -d "$vimdir"
  328. then
  329. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  330. else
  331. echo ""
  332. fi
  333. }
  334. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  335. do
  336. test -n "$f" || continue
  337. f="$f/macros/less.sh"
  338. test -f $f && alias vl=$f && break
  339. done
  340. __safe_alias pa="pacman --color=always"
  341. __safe_alias pa="pacmatic --color=always"
  342. __safe_alias pa="pacapt"
  343. __safe_alias yt=yaourt
  344. # variable for yaourt
  345. null type pacmatic && export PACMAN="pacmatic"
  346. __safe_alias cower="cower --color=auto"
  347. __my_pacman_update_mirrorlist_with_reflector(){
  348. f=/etc/pacman.d/mirrorlist
  349. cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
  350. if test -z "$cmd"
  351. then
  352. cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
  353. fi
  354. echo ">>> $cmd ..." 1>&2
  355. sudo sh -c "$cmd" && cat $f
  356. }
  357. null type reflector && test -d /etc/pacman.d && \
  358. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  359. null type apt-get && {
  360. alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
  361. apt-get update --yes && apt-get upgrade --yes'"
  362. alias aptin="apt-get install"
  363. alias aptsearch="apt-cache search"
  364. alias aptshow="apt-cache show"
  365. }
  366. null type port && {
  367. alias port="port -v"
  368. alias pupgrade="sudo sh -xc 'port -v selfupdate && \
  369. port -v upgrade outdated'"
  370. }
  371. if $iscygwin
  372. then
  373. null type windate || \
  374. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  375. # alias cygsu="cygstart /cygwinsetup.exe"
  376. fi
  377. mkpatch(){
  378. if test $# -eq 0
  379. then
  380. echo "usage: mkpatch <olddir> <newdir>"
  381. elif ! test -d "$1"
  382. then
  383. echo "mkpatch: $1 is not a directory"
  384. elif ! test -d "$2"
  385. then
  386. echo "mkpatch: $2 is not a directory"
  387. else
  388. diff -Naur "$1" "$2"
  389. fi
  390. }
  391. __sdcv_less(){
  392. command sdcv -n "$@" | less --quit-if-one-screen
  393. }
  394. command -v sdcv >/dev/null && alias dict=__sdcv_less
  395. __safe_alias g="git "
  396. if null type _git && $inbash
  397. then
  398. # enable programmable completion for g
  399. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  400. || complete -o default -o nospace -F _git g
  401. fi
  402. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  403. __safe_alias m=gitmemo
  404. #alias setup.py3="sudo python3 setup.py install --record files.txt"
  405. randomstr(){
  406. len=$1
  407. test -z "$len" && len=8
  408. uuidgen | tr -d - | cut -c 1-len
  409. }
  410. datestr(){
  411. # datestr YYYYMMDDhhmmss
  412. if test -z "$1" || test "$1" = "-h"
  413. then
  414. echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
  415. return 1
  416. fi
  417. # actual format for date command
  418. dfmt="`echo "$1" | \
  419. sed -e 's/YYYY/%Y/g' -e 's/yyyy/%Y/g' -e 's/YY/%y/g' -e 's/yy/%y/g' |\
  420. sed -e 's/MM/%m/g' -e 's/DD/%d/g' -e 's/dd/%d/g' | \
  421. sed -e 's/HH/%H/g' -e 's/hh/%H/g' -e 's/mm/%M/g' -e 's/SS/%S/g' -e 's/ss/%S/g'`"
  422. date +"$dfmt"
  423. }
  424. # ssh(){
  425. # # __my_terminal_title ssh
  426. # command ssh "$@"
  427. # }
  428. __ssh_with_cd(){
  429. # __ssh_with_cd <host> <directory> [<arg> ...]
  430. if test -z "$2"
  431. then
  432. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  433. return 1
  434. fi
  435. host="$1"
  436. shift
  437. dir="$1"
  438. shift
  439. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  440. }
  441. memo(){
  442. if test -z "$1"
  443. then
  444. _memo="memo.txt"
  445. else
  446. _memo="$1/memo.txt"
  447. fi
  448. $EDITOR "$_memo"
  449. if test -z "`cat "$_memo"`"
  450. then
  451. echo "$_memo is empty. Removing."
  452. rm "$_memo"
  453. fi
  454. }
  455. now(){
  456. ___tformat="%Y/%m/%d %H:%M:%S %z"
  457. cal
  458. REPLY=
  459. printf "\\r`date "+${___tformat}"`"
  460. read -t 1
  461. while test $? -ne 0
  462. do
  463. printf "\\r`date "+${___tformat}"`"
  464. read -t 1
  465. done
  466. }
  467. s(){
  468. if git rev-parse --git-dir >/dev/null 2>&1
  469. then
  470. echo ">> git grep -n $@" 1>&2
  471. git grep -n "$@"
  472. elif which ag >/dev/null 2>&1
  473. then
  474. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  475. ag --pager="$PAGER" "$@"
  476. elif which ack >/dev/null 2>&1
  477. then
  478. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  479. ack --pager="$PAGER" "$@"
  480. else
  481. echo \
  482. ">> find . " \
  483. "-path '*/.git' -prune -o" \
  484. "-path '*/.svn' -prune -o" \
  485. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  486. if test $# -eq 0
  487. then
  488. echo "No search word given." 1>&2
  489. return 1
  490. fi
  491. find . \
  492. -path '*/.git' -prune -o \
  493. -path '*/.svn' -prune -o \
  494. -type -f -exec grep -nH -e --color=always "$@" {} + \
  495. | $PAGER
  496. fi
  497. }
  498. MYMANPATH='/usr/lib/erlang/man'
  499. if $inbash || $inzsh
  500. then
  501. man(){
  502. env \
  503. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  504. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  505. LESS_TERMCAP_me=$(printf "\e[0m") \
  506. LESS_TERMCAP_se=$(printf "\e[0m") \
  507. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  508. LESS_TERMCAP_ue=$(printf "\e[0m") \
  509. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  510. MANPATH="`manpath`:$MYMANPATH" \
  511. man "$@"
  512. }
  513. fi
  514. netwait(){
  515. while ! ping -c 1 -t 1 example.com
  516. do
  517. true
  518. done
  519. echo network works.
  520. }
  521. __realpath(){
  522. if type realpath >/dev/null 2>&1
  523. then
  524. command realpath "$@"
  525. else
  526. while ! test -d $1
  527. do
  528. shift
  529. done
  530. (command cd "$d" && echo "$PWD")
  531. # local d="$OLDPWD"
  532. # command cd "$1"
  533. # echo "$PWD"
  534. # command cd "$d"
  535. fi
  536. }
  537. if $isdarwin
  538. then
  539. # I want to use env -u, but darwin env does not provide this option
  540. null type tmux && alias tmux='SHLVL= tmux'
  541. else
  542. null type tmux && alias tmux='env -u SHLVL tmux'
  543. fi
  544. tx(){
  545. tmux start-server
  546. if test -z "$1"
  547. then
  548. echo "usage: tx [<session_name>]"
  549. tmux list-sessions
  550. elif test -z "$TMUX"
  551. then
  552. # -A: attach if already exists
  553. # tmux new-session -A -c "$HOME" -s "$1"
  554. tmux new-session -A -s "$1"
  555. else
  556. # create new session if not exists yet
  557. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  558. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  559. tmux switch-client -t "$1"
  560. fi
  561. }
  562. __dsession(){
  563. # dt [-h] [<name>] [<command ...>]
  564. __dtach_dir="${TMP}/dtach"
  565. mkdir -p "${__dtach_dir}"
  566. if test -n "${__MY_DTACH}"
  567. then
  568. # if already in dtach session print current session
  569. soc_name="`basename "$__MY_DTACH"`"
  570. echo "Current session: ${soc_name}"
  571. fi
  572. if test -z "$1"
  573. then
  574. # if no arg given show list of sessions
  575. echo "Usage: ds [<session>]"
  576. echo "Sessions:"
  577. ls "${__dtach_dir}"
  578. return 0
  579. elif test "$1" = "-h"
  580. then
  581. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  582. return 1
  583. fi
  584. # set socket name
  585. soc_name="${__dtach_dir}/$1"
  586. shift
  587. if test -n "$__MY_DTACH"
  588. then
  589. echo "dtach session cannot be nested." 1>&2
  590. return 1
  591. elif test -S "$soc_name"
  592. then
  593. dtach -a "$soc_name" -e ^^
  594. elif test -e "$soc_name"
  595. then
  596. echo "dt: File named $soc_name already exists."
  597. return 1
  598. elif test -z "$1"
  599. then
  600. # if no command given invoke current shell
  601. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  602. # echo "dt: Socket named $soc_name not exists and no command specified."
  603. # return 1
  604. else
  605. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  606. fi
  607. }
  608. null type dtach && alias ds=__dsession
  609. scr(){
  610. test -n "$1" && pf="${1}-"
  611. ___tformat="%Y%m%d-%H%M%S%z"
  612. ___file="${PWD}/${pf}`date +${___tformat}`.script"
  613. __MY_SCRIPT=${___file} script ${___file} "$@"
  614. }
  615. dtscr(){
  616. # dtscr <command ...>
  617. if test -z "$1"
  618. then
  619. echo "dtscr: usage: dtscr <command ...>"
  620. return 1
  621. fi
  622. ___cmdstr="`echo $@ | tr ' ' +`"
  623. ___tformat="%Y%m%d-%H%M%S%z"
  624. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  625. ___scr_file="${HOME}/${___name}.script"
  626. ___dt_dir="${TMP}/dtscr"
  627. mkdir -p "$___dt_dir"
  628. dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
  629. # echo $_name
  630. # echo $_file
  631. }
  632. mcrypt_stream(){
  633. test $# -eq 2 || return 1
  634. case $1 in
  635. en)
  636. mcrypt --key $2 | base64 ;;
  637. de)
  638. base64 -d | mcrypt -d --key $2 ;;
  639. esac
  640. }
  641. gpg_stream(){
  642. test $# -eq 2 || return 1
  643. case $1 in
  644. en)
  645. gpg --passphrase $2 -c --batch |base64 ;;
  646. de)
  647. base64 -d|gpg --passphrase $2 -d --batch ;;
  648. esac
  649. }
  650. dgpg(){
  651. if test "$1" = help || test -z "$2"
  652. then
  653. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  654. return
  655. fi
  656. ___srcs="$2"
  657. ___dsts="$3"
  658. test -z "$___dsts" && ___dsts="${___srcs}.out"
  659. ___pw=
  660. echo -n "dgpg pw: "
  661. read -s ___pw
  662. echo ""
  663. test -z "$___pw" && return 1
  664. for f in *${___srcs}
  665. do
  666. ___d="$(basename "$f" "${___srcs}")${___dsts}"
  667. echo -n "Processing $f to $___d..."
  668. if test -d "$f"
  669. then
  670. echo "`printf 'failed (%s is directory)' $f`"
  671. elif test -f "$___d"
  672. then
  673. echo "`printf 'failed (%s is already exists)' $___d`"
  674. elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
  675. then
  676. echo "done"
  677. else
  678. echo "failed"
  679. test -f "$___d" && rm "$___d"
  680. fi
  681. done
  682. }
  683. alias enst="gpg_stream en"
  684. alias dest="gpg_stream de"
  685. showinfo(){
  686. echo "Japanese letters are 表示可能"
  687. __safe_run diskinfo
  688. ! $isdarwin && test -n "${DISPLAY}" && {
  689. __safe_run xrandr | \grep --color=never ^Screen
  690. }
  691. $iswindows || __safe_run finger $USER
  692. LANG=C __safe_runc id
  693. __safe_run xset q
  694. }
  695. x(){
  696. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  697. #mkdir -p ~/.var/log
  698. # nohup startx >~/.var/log/xorg.log 2>&1 &
  699. # exit
  700. exec startx
  701. else
  702. echo "X cant be started! Another X is already running?" 1>&2
  703. fi
  704. }
  705. bak(){
  706. for file in "$@"
  707. do
  708. cp -v ${file} ${file}.bak
  709. done
  710. }
  711. di(){
  712. if null type git
  713. then
  714. git --git-dir= diff "$@"
  715. ___diffcmd="git --git-dir= diff"
  716. else
  717. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  718. then
  719. ___diffcmd=colordiff
  720. else
  721. ___diffcmd=diff
  722. fi
  723. ${___diffcmd} -u "$@" | ${PAGER}
  724. fi
  725. }
  726. __trash_func(){
  727. __datenum=`date +%Y%m%d-%H%M%S`
  728. __tb="$HOME/.var/tb/$__datenum"
  729. mkdir -p "$__tb"
  730. mv "$@" "$__tb"
  731. }
  732. __safe_alias tb=__trash_func
  733. __safe_alias tb=trash
  734. mkdd(){
  735. _d=`date +%Y%m%d-%H%M%S` && \
  736. mkdir -p "$_d"
  737. }
  738. mkcd(){
  739. if test -z "$1"
  740. then
  741. echo "mkcd: usage: mkcd <dir>"
  742. return 1
  743. elif test -d "$1"
  744. then
  745. echo "Dir \"$1\" already exists."
  746. else
  747. mkdir -p "$1"
  748. echo "Dir \"$1\" created."
  749. fi
  750. cd "$1"
  751. }
  752. mkcdd(){
  753. # make and change date directory
  754. _d=`date +%Y%m%d-%H%M%S` && \
  755. mkcd "$_d"
  756. }
  757. if test -n "$TMUX" && null type reattach-to-user-namespace
  758. then
  759. alias pbpaste="reattach-to-user-namespace pbpaste"
  760. alias pbcopy="reattach-to-user-namespace pbcopy"
  761. fi
  762. __catclip(){
  763. if $iswindows
  764. then
  765. cat /dev/clipboard | tr -d \\r
  766. elif $isdarwin
  767. then
  768. pbpaste
  769. else
  770. xclip -o -selection "clipboard"
  771. fi
  772. }
  773. __setclip(){
  774. if test $# -eq 0
  775. then
  776. exec 3<&0
  777. else
  778. exec 3<<__EOF__
  779. `cat "$@"`
  780. __EOF__
  781. fi
  782. if $iswindows
  783. then
  784. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  785. elif $isdarwin
  786. then
  787. pbcopy 0<&3
  788. else
  789. 0<&3 xclip -i -f -selection "primary" | \
  790. xclip -i -f -selection "clipboard"
  791. fi
  792. exec 3<&-
  793. }
  794. # Now Homebrew 10sr/pkg/bin provides these commands:
  795. # Fallback to aliases if commands are not installed yet
  796. which catclip >/dev/null || alias catclip=__catclip
  797. which setclip >/dev/null || alias setclip=__setclip
  798. convmv_sjis2utf8_test(){
  799. convmv -r -f sjis -t utf8 *
  800. }
  801. convmv_sjis2utf8_notest(){
  802. convmv -r -f sjis -t utf8 * --notest
  803. }
  804. #################################################
  805. ## pastebin services
  806. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  807. sprunge(){
  808. # http://sprunge.us
  809. if test -z "$1"
  810. then
  811. curl -F 'sprunge=<-' http://sprunge.us
  812. else
  813. curl http://sprunge.us/$1
  814. fi
  815. }
  816. dpaste(){
  817. # http://dpaste.de
  818. if test -z "$1"
  819. then
  820. curl -F 'content=<-' https://dpaste.de/api/
  821. echo
  822. else
  823. curl https://dpaste.de/$1/raw/
  824. fi
  825. }
  826. ######################################
  827. ## Prompt Settings
  828. __my_moc_state(){
  829. type mocp >/dev/null 2>&1 || return
  830. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  831. printf "$1" "`mocp -Q %title 2>/dev/null`"
  832. }
  833. __my_parse_svn_branch() {
  834. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  835. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  836. sed -ne 's#^Repository Root: ##p')
  837. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  838. awk '{print $1}'
  839. }
  840. __my_svn_ps1(){
  841. if svn status >/dev/null 2>&1
  842. then
  843. ___svn_branch=$(__my_parse_svn_branch)
  844. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  845. fi
  846. }
  847. __my_battery_status(){
  848. ___dir=/sys/class/power_supply/BAT0
  849. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  850. test -r $___dir/charge_now
  851. then
  852. ___st=$(cat $___dir/status)
  853. ___full=$(cat $___dir/charge_full)
  854. ___now=$(cat $___dir/charge_now)
  855. ___rate=$(expr "$___now" \* 100 / "$___full")
  856. printf "$1" "${___st}:${___rate}%"
  857. fi
  858. }
  859. __my_ps1_scale(){
  860. if null type stty && ! $ismsys
  861. then
  862. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  863. fi
  864. }
  865. __my_ps1_tmux(){
  866. null type tmux || return $last
  867. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  868. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  869. }
  870. __my_ps1_moc(){
  871. __my_moc_state "[MOC:%s]"
  872. }
  873. for f in /usr/share/git/git-prompt.sh \
  874. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh \
  875. /usr/local/opt/bash-git-prompt/share/gitprompt.sh \
  876. "`brew --prefix 2>/dev/null`"/etc/bash_completion.d/git-prompt.sh \
  877. /usr/share/git-core/contrib/completion/git-prompt.sh \
  878. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  879. /etc/bash_completion.d/git-prompt \
  880. /opt/local/share/git-core/git-prompt.sh \
  881. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  882. do
  883. if test -r "$f" && ($inbash || $inzsh)
  884. then
  885. . "$f"
  886. break
  887. fi
  888. done
  889. export GIT_PS1_SHOWDIRTYSTATE=t
  890. export GIT_PS1_SHOWUPSTREAM=t
  891. __my_ps1_git(){
  892. null type __git_ps1 || return $last
  893. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  894. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  895. }
  896. __printf_ipaddr(){
  897. # ipaddress <fmt>
  898. type ip >/dev/null 2>&1 || return 1
  899. ___ip=$(LANG=C ip addr show scope global | \
  900. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  901. test -n "$___ip" && printf "$1" $___ip
  902. }
  903. alias addr="__printf_ipaddr '%s'"
  904. __my_ps1_ipaddr(){
  905. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  906. }
  907. __my_ps1_bttry(){
  908. ___bst="$__homerun/batterystatus"
  909. if test -z "$DISPLAY" && ! $iswindows
  910. then
  911. test -f $___bst && ___bstr="$(cat $___bst)"
  912. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  913. echo "[Battery:${___bstr}]"
  914. __my_battery_status %s >$___bst &
  915. fi
  916. }
  917. __my_ps1_memo(){
  918. test -f memo.txt && echo "m:$(echo $(wc -c memo.txt) | cut -f 1 -d " ")"
  919. }
  920. __my_ps1_dirs(){
  921. dirs | wc -l
  922. }
  923. __my_ps1_jobs(){
  924. # __my_ps1_jobs [<num>]
  925. if test -n "$1"
  926. then
  927. jobs="$1"
  928. else
  929. jobs="`jobs | wc -l`"
  930. fi
  931. if test "$jobs" -gt 0
  932. then
  933. echo "JOBS:$jobs"
  934. fi
  935. }
  936. __my_ps1_dollar(){
  937. if test -z "$SHLVL"
  938. then
  939. printf "$1"
  940. else
  941. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' $SHLVL "$1"
  942. fi
  943. }
  944. __my_alert_fail(){
  945. test $laststatus -eq 0 || \
  946. echo "STATUS:${laststatus}"
  947. }
  948. # About ansi escape sequences
  949. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  950. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  951. if $inbash || $inzsh
  952. then
  953. if $inzsh
  954. then
  955. __attr_beg=$'%{\033['
  956. __attr_end='m%}'
  957. else
  958. __attr_beg='\[\033['
  959. __attr_end='m\]'
  960. fi
  961. __color_default="${__attr_beg}0${__attr_end}"
  962. __color_black="${__attr_beg}0;30${__attr_end}"
  963. __color_red="${__attr_beg}0;31${__attr_end}"
  964. __color_green="${__attr_beg}0;32${__attr_end}"
  965. __color_yellow="${__attr_beg}0;33${__attr_end}"
  966. __color_blue="${__attr_beg}0;34${__attr_end}"
  967. __color_magenta="${__attr_beg}0;35${__attr_end}"
  968. __color_cyan="${__attr_beg}0;36${__attr_end}"
  969. __color_white="${__attr_beg}0;37${__attr_end}"
  970. __color_light_black="${__attr_beg}1;30${__attr_end}"
  971. __color_light_red="${__attr_beg}1;31${__attr_end}"
  972. __color_light_green="${__attr_beg}1;32${__attr_end}"
  973. __color_light_yellow="${__attr_beg}1;33${__attr_end}"
  974. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  975. __color_light_magenta="${__attr_beg}1;35${__attr_end}"
  976. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  977. __color_light_white="${__attr_beg}1;37${__attr_end}"
  978. __color_bg_black="${__attr_beg}40${__attr_end}"
  979. __color_bg_red="${__attr_beg}41${__attr_end}"
  980. __color_bg_green="${__attr_beg}42${__attr_end}"
  981. __color_bg_yellow="${__attr_beg}43${__attr_end}"
  982. __color_bg_blue="${__attr_beg}44${__attr_end}"
  983. __color_bg_magenta="${__attr_beg}45${__attr_end}"
  984. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  985. __color_bg_white="${__attr_beg}47${__attr_end}"
  986. __attr_underline="${__attr_beg}4${__attr_end}"
  987. __attr_reverse="${__attr_beg}7${__attr_end}"
  988. __attr_bold="${__attr_beg}1${__attr_end}"
  989. fi
  990. # NOTE: tput is another easy way to set colors and background
  991. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  992. # color blue.
  993. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  994. if test "$TERM" != dumb
  995. then
  996. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  997. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  998. # color for ::
  999. if test -z "$_HOSTCOLOR_1"
  1000. then
  1001. # default color used by tmux status line
  1002. _HOSTCOLOR_1=green
  1003. fi
  1004. eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
  1005. __my_c5="$__color_black$__color_bg_white" # color for SCR
  1006. __my_cdef="$__color_default"
  1007. fi
  1008. if $inbash
  1009. then
  1010. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1011. elif $inzsh
  1012. then
  1013. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1014. fi
  1015. __my_ps1_info1(){
  1016. # first line of PS1
  1017. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
  1018. }
  1019. test -n "$__MY_SCRIPT" && \
  1020. __my_ps1_str_scr="SCR"
  1021. test -n "$SSH_CONNECTION" && \
  1022. __my_ps1_str_ssh="SSH"
  1023. test -n "$__MY_DTACH" && \
  1024. __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
  1025. __my_ps1_info2(){
  1026. # second line of PS1
  1027. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1028. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1029. | sed -e 's/ /|/g'
  1030. }
  1031. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1032. __my_ps1_save_pos="\[\033[s\]"
  1033. __my_ps1_restore_pos="\[\033[u\]"
  1034. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1035. __my_ps1_move_15left="\[\033[15D\]"
  1036. # collapse when command line is too long and try to write over this string
  1037. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1038. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1039. if $inzsh
  1040. then
  1041. PROMPT="\
  1042. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1043. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %#) "
  1044. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1045. elif $inbash
  1046. then
  1047. PS1="\
  1048. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1049. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$) "
  1050. else
  1051. true
  1052. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1053. fi
  1054. ###################################
  1055. # set header and titles
  1056. __my_set_header_line(){
  1057. # save current position
  1058. printf "\033[s"
  1059. # move to 0,0
  1060. printf "\033[0;0H"
  1061. # clear curent to eol
  1062. printf "\033[K"
  1063. # inverse color
  1064. printf "\033[7m"
  1065. printf "$1"
  1066. # restore color
  1067. printf "\033[0m"
  1068. # restore saved position
  1069. printf "\033[u"
  1070. }
  1071. __my_set_screen_name(){
  1072. # set window name
  1073. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1074. then
  1075. echo -ne "\033k$1\033\\"
  1076. fi
  1077. }
  1078. __my_set_title(){
  1079. case $TERM in
  1080. (rxvt*|xterm*|aterm|screen*)
  1081. test -t 1 &&
  1082. test -z "$EMACS" &&
  1083. echo -n -e "\033]0;$1\007"
  1084. ;;
  1085. esac
  1086. }
  1087. if test -n "$TMUX"
  1088. then
  1089. # running tmux locally
  1090. __terminal_title="\$(basename \${PWD})"
  1091. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1092. then
  1093. # ssh connect from tmux terminal
  1094. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1095. else
  1096. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1097. fi
  1098. if $inzsh
  1099. then
  1100. precmd(){
  1101. laststatus=$?
  1102. eval __my_set_title ${__terminal_title}
  1103. }
  1104. else
  1105. if test -n "$PROMPT_COMMAND" && ! expr "$PROMPT_COMMAND" : '.*; *$' >/dev/null
  1106. then
  1107. PROMPT_COMMAND="${PROMPT_COMMAND};"
  1108. fi
  1109. if test -n "$PROMPT_COMMAND"
  1110. then
  1111. PROMPT_COMMAND="laststatus=\$?;${PROMPT_COMMAND}__my_set_title \"${__terminal_title}\";"
  1112. else
  1113. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";"
  1114. fi
  1115. fi
  1116. laststatus=0