Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1308 строки
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. command -v fortune >/dev/null && {
  149. if command -v cowsay >/dev/null
  150. then
  151. fortune | cowsay
  152. echo
  153. fortune -o | cowsay
  154. echo
  155. else
  156. fortune
  157. echo
  158. fortune -o
  159. echo
  160. fi
  161. }
  162. if null type figlet && null type lolcat
  163. then
  164. hostname -s | figlet | lolcat
  165. fi
  166. uname -a
  167. $iswindows && alias tty="echo cmd.exe"
  168. echo TERM $TERM with $(tput colors) colors using $(tty)
  169. echo umask: `umask -S`
  170. echo
  171. __safe_run w yuk
  172. # if null type tmux
  173. # then
  174. # echo
  175. # echo tmux sessions:
  176. # tmux ls 2>/dev/null| sed -e 's/^/ /g'
  177. # echo
  178. # fi
  179. # if test -n "$TMUX"
  180. # then
  181. # tmux display -p \
  182. # 'TMUX #S:#I:#W.#P, client TERM: #{client_termname}' \
  183. # 2>/dev/null
  184. # echo
  185. # fi
  186. if test -n "$__MY_DTACH"
  187. then
  188. echo
  189. echo DSession: Current Session: "`basename "$__MY_DTACH"`"
  190. echo DSession: Press C-^ to Exit.
  191. true
  192. fi
  193. i(){
  194. w
  195. ss -lnp
  196. ps aufx
  197. df -h
  198. top -b -d 1 -n 1
  199. top -b -d 1 -n 1 -a
  200. dstat -taf 1 10
  201. }
  202. ###################################
  203. # some aliases and functions
  204. # __func_name: never used interactively
  205. # _func_name: usually not used interactively
  206. __safe_alias(){
  207. # __safe_alias <name>=<command>
  208. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  209. test -n "$_bin" && \
  210. null type $_bin && \
  211. alias "$1"
  212. }
  213. $gnu_coreutils && _timeoption=" --time-style=long-iso"
  214. # color prefs
  215. if $gnu_coreutils
  216. then
  217. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  218. # gnu coreutils LS_COLORS is used
  219. null type dircolors && eval `dircolors`
  220. _coloroption=" --color=auto"
  221. else
  222. # export LSCOLORS=gxfxcxdxbxegedabagacad
  223. export LSCOLORS=DxGxcxdxCxegedabagacad
  224. export CLICOLOR=1
  225. fi
  226. alias ls="ls -hCF${_coloroption}${_timeoption}"
  227. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  228. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  229. _timeformat_num="%Y%m%d%H%M%S"
  230. alias datenum="date +$_timeformat_num"
  231. alias less="less -F"
  232. __safe_alias em="emacs -nw"
  233. test -n "$_VI_ALT" && alias vi=$_VI_ALT
  234. alias pstree="LANG=C pstree"
  235. alias cp="cp -v"
  236. alias mv="mv -v"
  237. alias rm="rm -v"
  238. alias mkdir="mkdir -v"
  239. $gnu_coreutils && alias rmdir="rmdir -v"
  240. alias psaux="ps auxww"
  241. alias modest="ionice -c 2 -n 7 nice -n 19"
  242. alias e=exec
  243. __safe_alias e3=e3em
  244. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  245. alias po=popd
  246. alias pu=pushd
  247. __safe_alias sudo="sudo " # use aliases through sudo
  248. __safe_alias sudoe="sudoedit"
  249. alias rand="echo \$RANDOM"
  250. alias pycalc="python -i -c 'from math import *' "
  251. __safe_alias py3=python3
  252. __safe_alias py2=python2
  253. __safe_alias ipy=ipython
  254. __safe_alias ipy3=ipython3
  255. __safe_alias ipy2=ipython2
  256. __safe_alias javac="javac -J-Duser.language=en"
  257. __safe_alias rl=rlwrap
  258. __safe_alias igosh=gosh-rl
  259. # Sometimes SHELL cannot be used. For example, when running bash inside zsh
  260. # SHELL is set to be /bin/zsh
  261. if $inbash
  262. then
  263. alias _reloadrc="rm '$__shrc_lock'; exec bash"
  264. elif $inzsh
  265. then
  266. alias _reloadrc="rm '$__shrc_lock'; exec zsh"
  267. else
  268. alias _reloadrc="rm '$__shrc_lock'; exec $SHELL"
  269. fi
  270. # alias mytime="date +%Y%m%d-%H%M%S"
  271. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  272. # some extra utilities
  273. __safe_alias mpg123="mpg123 -C -v --title"
  274. #export PLAYER="mpg123 -C -v --title"
  275. __safe_alias screen="screen -e^z^z"
  276. #alias zcd="cd \`zenity --file-selection --directory\`"
  277. __safe_alias gtags="gtags --verbose"
  278. __safe_alias htags="htags --xhtml --symbol --line-number \
  279. --frame --alphabet --verbose"
  280. __safe_alias au=aunpack
  281. # __safe_alias lv="lv|less"
  282. __safe_alias rs="rsync --progress --itemize-changes --compress"
  283. __safe_alias vagr=vagrant
  284. __safe_alias ztw='twitter set "`zenity --entry --title ztw --text Status:`"'
  285. if $iscygwin
  286. then
  287. __my_wget_options=" --no-check-certificate"
  288. __safe_alias wget="wget $__my_wget_options"
  289. fi
  290. if $isdarwin
  291. then
  292. alias updatedb="LC_ALL=C updatedb"
  293. # do not use locate installed by macports
  294. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  295. fi
  296. # somehow not work
  297. # typeset -ga chpwd_functions
  298. # chpwd_functions+=pwd
  299. # chpwd(){
  300. # pwd
  301. # }
  302. cd(){
  303. builtin cd "$@" && pwd
  304. }
  305. root(){
  306. if test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = true
  307. then
  308. cd "`git rev-parse --show-toplevel`"
  309. else
  310. cd /
  311. fi
  312. }
  313. __find_latest_vimdir(){
  314. vimdir=/usr/share/vim
  315. if test -d "$vimdir"
  316. then
  317. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  318. else
  319. echo ""
  320. fi
  321. }
  322. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  323. do
  324. test -n "$f" || continue
  325. f="$f/macros/less.sh"
  326. test -f $f && alias vl=$f && break
  327. done
  328. __safe_alias pa="pacman --color=always"
  329. __safe_alias pa="pacmatic --color=always"
  330. __safe_alias pa="pacapt"
  331. __safe_alias yt=yaourt
  332. # variable for yaourt
  333. null type pacmatic && export PACMAN="pacmatic"
  334. __safe_alias cower="cower --color=auto"
  335. __my_pacman_update_mirrorlist_with_reflector(){
  336. f=/etc/pacman.d/mirrorlist
  337. cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
  338. if test -z "$cmd"
  339. then
  340. cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
  341. fi
  342. echo ">>> $cmd ..." 1>&2
  343. sudo sh -c "$cmd" && cat $f
  344. }
  345. null type reflector && test -d /etc/pacman.d && \
  346. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  347. null type apt-get && {
  348. alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
  349. apt-get update --yes && apt-get upgrade --yes'"
  350. alias aptin="apt-get install"
  351. alias aptsearch="apt-cache search"
  352. alias aptshow="apt-cache show"
  353. }
  354. null type port && {
  355. alias port="port -v"
  356. alias pupgrade="sudo sh -xc 'port -v selfupdate && \
  357. port -v upgrade outdated'"
  358. }
  359. if $iscygwin
  360. then
  361. null type windate || \
  362. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  363. # alias cygsu="cygstart /cygwinsetup.exe"
  364. fi
  365. mkpatch(){
  366. if test $# -eq 0
  367. then
  368. echo "usage: mkpatch <olddir> <newdir>"
  369. elif ! test -d "$1"
  370. then
  371. echo "mkpatch: $1 is not a directory"
  372. elif ! test -d "$2"
  373. then
  374. echo "mkpatch: $2 is not a directory"
  375. else
  376. diff -Naur "$1" "$2"
  377. fi
  378. }
  379. __sdcv_less(){
  380. command sdcv -n "$@" | less --quit-if-one-screen
  381. }
  382. command -v sdcv >/dev/null && alias dict=__sdcv_less
  383. g(){
  384. if test $# -eq 0 && null type git-info
  385. then
  386. git info
  387. else
  388. git "$@"
  389. fi
  390. }
  391. $isdarwin && . /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
  392. if null type _git && $inbash
  393. then
  394. # enable programmable completion for g
  395. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  396. || complete -o default -o nospace -F _git g
  397. fi
  398. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  399. __safe_alias m=gitmemo
  400. #alias setup.py3="sudo python3 setup.py install --record files.txt"
  401. randomstr(){
  402. len=$1
  403. test -z "$len" && len=8
  404. uuidgen | tr -d - | cut -c 1-len
  405. }
  406. datestr(){
  407. # datestr YYYYMMDDhhmmss
  408. if test -z "$1" || test "$1" = "-h"
  409. then
  410. echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
  411. return 1
  412. fi
  413. # actual format for date command
  414. dfmt="`echo "$1" | \
  415. sed -e 's/YYYY/%Y/g' -e 's/yyyy/%Y/g' -e 's/YY/%y/g' -e 's/yy/%y/g' |\
  416. sed -e 's/MM/%m/g' -e 's/DD/%d/g' -e 's/dd/%d/g' | \
  417. 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'`"
  418. date +"$dfmt"
  419. }
  420. # ssh(){
  421. # # __my_terminal_title ssh
  422. # command ssh "$@"
  423. # }
  424. __ssh_with_cd(){
  425. # __ssh_with_cd <host> <directory> [<arg> ...]
  426. if test -z "$2"
  427. then
  428. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  429. return 1
  430. fi
  431. host="$1"
  432. shift
  433. dir="$1"
  434. shift
  435. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  436. }
  437. memo(){
  438. if test -z "$1"
  439. then
  440. _memo="memo.txt"
  441. else
  442. _memo="$1/memo.txt"
  443. fi
  444. $EDITOR "$_memo"
  445. if test -z "`cat "$_memo"`"
  446. then
  447. echo "$_memo is empty. Removing."
  448. rm "$_memo"
  449. fi
  450. }
  451. now(){
  452. ___tformat="%Y/%m/%d %H:%M:%S %z"
  453. cal
  454. REPLY=
  455. printf "\\r`date "+${___tformat}"`"
  456. read -t 1
  457. while test $? -ne 0
  458. do
  459. printf "\\r`date "+${___tformat}"`"
  460. read -t 1
  461. done
  462. }
  463. s(){
  464. if git rev-parse --git-dir >/dev/null 2>&1
  465. then
  466. echo ">> git grep -n $@" 1>&2
  467. git grep -n "$@"
  468. elif which ag >/dev/null 2>&1
  469. then
  470. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  471. ag --pager="$PAGER" "$@"
  472. elif which ack >/dev/null 2>&1
  473. then
  474. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  475. ack --pager="$PAGER" "$@"
  476. else
  477. echo \
  478. ">> find . " \
  479. "-path '*/.git' -prune -o" \
  480. "-path '*/.svn' -prune -o" \
  481. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  482. if test $# -eq 0
  483. then
  484. echo "No search word given." 1>&2
  485. return 1
  486. fi
  487. find . \
  488. -path '*/.git' -prune -o \
  489. -path '*/.svn' -prune -o \
  490. -type -f -exec grep -nH -e --color=always "$@" {} + \
  491. | $PAGER
  492. fi
  493. }
  494. MYMANPATH='/usr/lib/erlang/man'
  495. if $inbash || $inzsh
  496. then
  497. man(){
  498. env \
  499. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  500. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  501. LESS_TERMCAP_me=$(printf "\e[0m") \
  502. LESS_TERMCAP_se=$(printf "\e[0m") \
  503. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  504. LESS_TERMCAP_ue=$(printf "\e[0m") \
  505. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  506. MANPATH="`manpath`:$MYMANPATH" \
  507. man "$@"
  508. }
  509. fi
  510. netwait(){
  511. while ! ping -c 1 -t 1 example.com
  512. do
  513. true
  514. done
  515. echo network works.
  516. }
  517. __realpath(){
  518. if type realpath >/dev/null 2>&1
  519. then
  520. command realpath "$@"
  521. else
  522. while ! test -d $1
  523. do
  524. shift
  525. done
  526. (command cd "$d" && echo "$PWD")
  527. # local d="$OLDPWD"
  528. # command cd "$1"
  529. # echo "$PWD"
  530. # command cd "$d"
  531. fi
  532. }
  533. if $isdarwin
  534. then
  535. # I want to use env -u, but darwin env does not provide this option
  536. null type tmux && alias tmux='SHLVL= tmux'
  537. else
  538. null type tmux && alias tmux='env -u SHLVL tmux'
  539. fi
  540. tx(){
  541. tmux start-server
  542. if test -z "$1"
  543. then
  544. echo "usage: tx [<session_name>]"
  545. tmux list-sessions
  546. elif test -z "$TMUX"
  547. then
  548. # -A: attach if already exists
  549. # tmux new-session -A -c "$HOME" -s "$1"
  550. tmux new-session -A -s "$1"
  551. else
  552. # create new session if not exists yet
  553. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  554. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  555. tmux switch-client -t "$1"
  556. fi
  557. }
  558. __dsession(){
  559. # dt [-h] [<name>] [<command ...>]
  560. __dtach_dir="${TMP}/dtach"
  561. mkdir -p "${__dtach_dir}"
  562. if test -n "${__MY_DTACH}"
  563. then
  564. # if already in dtach session print current session
  565. soc_name="`basename "$__MY_DTACH"`"
  566. echo "Current session: ${soc_name}"
  567. fi
  568. if test -z "$1"
  569. then
  570. # if no arg given show list of sessions
  571. echo "Usage: ds [<session>]"
  572. echo "Sessions:"
  573. ls "${__dtach_dir}"
  574. return 0
  575. elif test "$1" = "-h"
  576. then
  577. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  578. return 1
  579. fi
  580. # set socket name
  581. soc_name="${__dtach_dir}/$1"
  582. shift
  583. if test -n "$__MY_DTACH"
  584. then
  585. echo "dtach session cannot be nested." 1>&2
  586. return 1
  587. elif test -S "$soc_name"
  588. then
  589. dtach -a "$soc_name" -e ^^
  590. elif test -e "$soc_name"
  591. then
  592. echo "dt: File named $soc_name already exists."
  593. return 1
  594. elif test -z "$1"
  595. then
  596. # if no command given invoke current shell
  597. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  598. # echo "dt: Socket named $soc_name not exists and no command specified."
  599. # return 1
  600. else
  601. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  602. fi
  603. }
  604. null type dtach && alias ds=__dsession
  605. scr(){
  606. test -n "$1" && pf="${1}-"
  607. ___tformat="%Y%m%d-%H%M%S%z"
  608. ___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. ___cmdstr="`echo $@ | tr ' ' +`"
  619. ___tformat="%Y%m%d-%H%M%S%z"
  620. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  621. ___scr_file="${HOME}/${___name}.script"
  622. ___dt_dir="${TMP}/dtscr"
  623. mkdir -p "$___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. ___srcs="$2"
  653. ___dsts="$3"
  654. test -z "$___dsts" && ___dsts="${___srcs}.out"
  655. ___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. ___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 ~/.var/log
  694. # nohup startx >~/.var/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 null type git
  709. then
  710. git --git-dir= diff "$@"
  711. ___diffcmd="git --git-dir= diff"
  712. else
  713. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  714. then
  715. ___diffcmd=colordiff
  716. else
  717. ___diffcmd=diff
  718. fi
  719. ${___diffcmd} -u "$@" | ${PAGER}
  720. fi
  721. }
  722. tb(){
  723. __datenum=`date +%Y%m%d-%H%M%S`
  724. __tb="$HOME/.var/tb/$__datenum"
  725. mkdir -p "$__tb"
  726. mv "$@" "$__tb"
  727. }
  728. mkdd(){
  729. _d=`date +%Y%m%d-%H%M%S` && \
  730. mkdir -p "$_d"
  731. }
  732. mkcd(){
  733. if test -z "$1"
  734. then
  735. echo "mkcd: usage: mkcd <dir>"
  736. return 1
  737. elif test -d "$1"
  738. then
  739. echo "Dir \"$1\" already exists."
  740. else
  741. mkdir -p "$1"
  742. echo "Dir \"$1\" created."
  743. fi
  744. cd "$1"
  745. }
  746. mkcdd(){
  747. # make and change date directory
  748. _d=`date +%Y%m%d-%H%M%S` && \
  749. mkcd "$_d"
  750. }
  751. if test -n "$TMUX" && null type reattach-to-user-namespace
  752. then
  753. alias pbpaste="reattach-to-user-namespace pbpaste"
  754. alias pbcopy="reattach-to-user-namespace pbcopy"
  755. fi
  756. catclip(){
  757. if $iswindows
  758. then
  759. cat /dev/clipboard | tr -d \\r
  760. elif $isdarwin
  761. then
  762. pbpaste
  763. else
  764. xclip -o -selection "clipboard"
  765. fi
  766. }
  767. setclip(){
  768. if test $# -eq 0
  769. then
  770. exec 3<&0
  771. else
  772. exec 3<<__EOF__
  773. `cat "$@"`
  774. __EOF__
  775. fi
  776. if $iswindows
  777. then
  778. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  779. elif $isdarwin
  780. then
  781. pbcopy 0<&3
  782. else
  783. 0<&3 xclip -i -f -selection "primary" | \
  784. xclip -i -f -selection "clipboard"
  785. fi
  786. exec 3<&-
  787. }
  788. open_file(){
  789. if $iscygwin
  790. then
  791. cygstart "$@"
  792. elif $ismsys
  793. then
  794. cmd.exe //c start "" "$@"
  795. elif $isdarwin
  796. then
  797. touch "$@"
  798. open "$@"
  799. elif $islinux
  800. then
  801. touch "$@"
  802. if null type pcmanfm; then
  803. LC_MESSAGES= pcmanfm "$@"
  804. else
  805. LC_MESSAGES= xdg-open "$@" &
  806. fi
  807. else
  808. cat "$@"
  809. fi
  810. }
  811. o(){
  812. if test $# -eq 0
  813. then
  814. open_file .
  815. else
  816. for f in "$@"
  817. do
  818. open_file "$(realpath "$f")"
  819. done
  820. fi
  821. }
  822. convmv_sjis2utf8_test(){
  823. convmv -r -f sjis -t utf8 *
  824. }
  825. convmv_sjis2utf8_notest(){
  826. convmv -r -f sjis -t utf8 * --notest
  827. }
  828. #################################################
  829. ## pastebin services
  830. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  831. sprunge(){
  832. # http://sprunge.us
  833. if test -z "$1"
  834. then
  835. curl -F 'sprunge=<-' http://sprunge.us
  836. else
  837. curl http://sprunge.us/$1
  838. fi
  839. }
  840. dpaste(){
  841. # http://dpaste.de
  842. if test -z "$1"
  843. then
  844. curl -F 'content=<-' https://dpaste.de/api/
  845. echo
  846. else
  847. curl https://dpaste.de/$1/raw/
  848. fi
  849. }
  850. ######################################
  851. ## Prompt Settings
  852. __my_moc_state(){
  853. type mocp >/dev/null 2>&1 || return
  854. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  855. printf "$1" "`mocp -Q %title 2>/dev/null`"
  856. }
  857. __my_parse_svn_branch() {
  858. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  859. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  860. sed -ne 's#^Repository Root: ##p')
  861. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  862. awk '{print $1}'
  863. }
  864. __my_svn_ps1(){
  865. if svn status >/dev/null 2>&1
  866. then
  867. ___svn_branch=$(__my_parse_svn_branch)
  868. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  869. fi
  870. }
  871. __my_battery_status(){
  872. ___dir=/sys/class/power_supply/BAT0
  873. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  874. test -r $___dir/charge_now
  875. then
  876. ___st=$(cat $___dir/status)
  877. ___full=$(cat $___dir/charge_full)
  878. ___now=$(cat $___dir/charge_now)
  879. ___rate=$(expr "$___now" \* 100 / "$___full")
  880. printf "$1" "${___st}:${___rate}%"
  881. fi
  882. }
  883. alias bat='__my_battery_status %s\\n'
  884. __my_ps1_scale(){
  885. if null type stty && ! $ismsys
  886. then
  887. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  888. fi
  889. }
  890. __my_ps1_tmux(){
  891. null type tmux || return $last
  892. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  893. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  894. }
  895. __my_ps1_moc(){
  896. __my_moc_state "[MOC:%s]"
  897. }
  898. for f in /usr/share/git/git-prompt.sh \
  899. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh \
  900. /usr/local/opt/bash-git-prompt/share/gitprompt.sh \
  901. "`brew --prefix 2>/dev/null`"/etc/bash_completion.d/git-prompt.sh \
  902. /usr/share/git-core/contrib/completion/git-prompt.sh \
  903. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  904. /etc/bash_completion.d/git-prompt \
  905. /opt/local/share/git-core/git-prompt.sh \
  906. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  907. do
  908. test -r "$f" && ($inbash || $inzsh) && . "$f" && break
  909. done
  910. export GIT_PS1_SHOWDIRTYSTATE=t
  911. export GIT_PS1_SHOWUPSTREAM=t
  912. __my_ps1_git(){
  913. null type __git_ps1 || return $last
  914. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  915. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  916. }
  917. __printf_ipaddr(){
  918. # ipaddress <fmt>
  919. type ip >/dev/null 2>&1 || return 1
  920. ___ip=$(LANG=C ip addr show scope global | \
  921. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  922. test -n "$___ip" && printf "$1" $___ip
  923. }
  924. alias addr="__printf_ipaddr '%s'"
  925. __my_ps1_ipaddr(){
  926. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  927. }
  928. __my_ps1_bttry(){
  929. ___bst="$__homerun/batterystatus"
  930. if test -z "$DISPLAY" && ! $iswindows
  931. then
  932. test -f $___bst && ___bstr="$(cat $___bst)"
  933. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  934. echo "[Battery:${___bstr}]"
  935. __my_battery_status %s >$___bst &
  936. fi
  937. }
  938. __my_ps1_memo(){
  939. test -f memo.txt && echo "m:$(echo $(wc -c memo.txt) | cut -f 1 -d " ")"
  940. }
  941. __my_ps1_dirs(){
  942. dirs | wc -l
  943. }
  944. __my_ps1_jobs(){
  945. # __my_ps1_jobs [<num>]
  946. if test -n "$1"
  947. then
  948. jobs="$1"
  949. else
  950. jobs="`jobs | wc -l`"
  951. fi
  952. if test "$jobs" -gt 0
  953. then
  954. echo "JOBS:$jobs"
  955. fi
  956. }
  957. __my_ps1_dollar(){
  958. if test -z "$SHLVL"
  959. then
  960. printf "$1"
  961. else
  962. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' $SHLVL "$1"
  963. fi
  964. }
  965. __my_alert_fail(){
  966. test $laststatus -eq 0 || \
  967. echo "STATUS:${laststatus}"
  968. }
  969. # About ansi escape sequences
  970. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  971. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  972. if $inbash || $inzsh
  973. then
  974. if $inzsh
  975. then
  976. __attr_beg=$'%{\033['
  977. __attr_end='m%}'
  978. else
  979. __attr_beg='\[\033['
  980. __attr_end='m\]'
  981. fi
  982. __color_default="${__attr_beg}0${__attr_end}"
  983. __color_black="${__attr_beg}0;30${__attr_end}"
  984. __color_red="${__attr_beg}0;31${__attr_end}"
  985. __color_green="${__attr_beg}0;32${__attr_end}"
  986. __color_yellow="${__attr_beg}0;33${__attr_end}"
  987. __color_blue="${__attr_beg}0;34${__attr_end}"
  988. __color_magenta="${__attr_beg}0;35${__attr_end}"
  989. __color_cyan="${__attr_beg}0;36${__attr_end}"
  990. __color_white="${__attr_beg}0;37${__attr_end}"
  991. __color_light_black="${__attr_beg}1;30${__attr_end}"
  992. __color_light_red="${__attr_beg}1;31${__attr_end}"
  993. __color_light_green="${__attr_beg}1;32${__attr_end}"
  994. __color_light_yellow="${__attr_beg}1;33${__attr_end}"
  995. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  996. __color_light_magenta="${__attr_beg}1;35${__attr_end}"
  997. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  998. __color_light_white="${__attr_beg}1;37${__attr_end}"
  999. __color_bg_black="${__attr_beg}40${__attr_end}"
  1000. __color_bg_red="${__attr_beg}41${__attr_end}"
  1001. __color_bg_green="${__attr_beg}42${__attr_end}"
  1002. __color_bg_yellow="${__attr_beg}43${__attr_end}"
  1003. __color_bg_blue="${__attr_beg}44${__attr_end}"
  1004. __color_bg_magenta="${__attr_beg}45${__attr_end}"
  1005. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  1006. __color_bg_white="${__attr_beg}47${__attr_end}"
  1007. __attr_underline="${__attr_beg}4${__attr_end}"
  1008. __attr_reverse="${__attr_beg}7${__attr_end}"
  1009. __attr_bold="${__attr_beg}1${__attr_end}"
  1010. fi
  1011. # NOTE: tput is another easy way to set colors and background
  1012. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  1013. # color blue.
  1014. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  1015. if test "$TERM" != dumb
  1016. then
  1017. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  1018. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1019. # color for ::
  1020. if test -z "$_HOSTCOLOR_1"
  1021. then
  1022. # default color used by tmux status line
  1023. _HOSTCOLOR_1=green
  1024. fi
  1025. eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
  1026. __my_c5="$__color_black$__color_bg_white" # color for SCR
  1027. __my_cdef="$__color_default"
  1028. fi
  1029. if $inbash
  1030. then
  1031. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1032. elif $inzsh
  1033. then
  1034. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1035. fi
  1036. __my_ps1_info1(){
  1037. # first line of PS1
  1038. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
  1039. }
  1040. test -n "$__MY_SCRIPT" && \
  1041. __my_ps1_str_scr="SCR"
  1042. test -n "$SSH_CONNECTION" && \
  1043. __my_ps1_str_ssh="SSH"
  1044. test -n "$__MY_DTACH" && \
  1045. __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
  1046. __my_ps1_info2(){
  1047. # second line of PS1
  1048. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1049. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1050. | sed -e 's/ /|/g'
  1051. }
  1052. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1053. __my_ps1_save_pos="\[\033[s\]"
  1054. __my_ps1_restore_pos="\[\033[u\]"
  1055. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1056. __my_ps1_move_15left="\[\033[15D\]"
  1057. # collapse when command line is too long and try to write over this string
  1058. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1059. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1060. if $inzsh
  1061. then
  1062. PROMPT="\
  1063. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1064. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %#) "
  1065. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1066. elif $inbash
  1067. then
  1068. PS1="\
  1069. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1070. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$) "
  1071. else
  1072. true
  1073. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1074. fi
  1075. ###################################
  1076. # set header and titles
  1077. __my_set_header_line(){
  1078. # save current position
  1079. printf "\033[s"
  1080. # move to 0,0
  1081. printf "\033[0;0H"
  1082. # clear curent to eol
  1083. printf "\033[K"
  1084. # inverse color
  1085. printf "\033[7m"
  1086. printf "$1"
  1087. # restore color
  1088. printf "\033[0m"
  1089. # restore saved position
  1090. printf "\033[u"
  1091. }
  1092. __my_set_screen_name(){
  1093. # set window name
  1094. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1095. then
  1096. echo -ne "\033k$1\033\\"
  1097. fi
  1098. }
  1099. __my_set_title(){
  1100. case $TERM in
  1101. (rxvt*|xterm*|aterm|screen*)
  1102. test -t 1 &&
  1103. test -z "$EMACS" &&
  1104. echo -n -e "\033]0;$1\007"
  1105. ;;
  1106. esac
  1107. }
  1108. if test -n "$TMUX"
  1109. then
  1110. # running tmux locally
  1111. __terminal_title="\$(basename \${PWD})"
  1112. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1113. then
  1114. # ssh connect from tmux terminal
  1115. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1116. else
  1117. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1118. fi
  1119. if $inzsh
  1120. then
  1121. precmd(){
  1122. laststatus=$?
  1123. eval __my_set_title ${__terminal_title}
  1124. }
  1125. else
  1126. if test -n "$PROMPT_COMMAND" && ! expr "$PROMPT_COMMAND" : '.*; *$' >/dev/null
  1127. then
  1128. PROMPT_COMMAND="${PROMPT_COMMAND};"
  1129. fi
  1130. if test -n "$PROMPT_COMMAND"
  1131. then
  1132. PROMPT_COMMAND="laststatus=\$?;${PROMPT_COMMAND}__my_set_title \"${__terminal_title}\";"
  1133. else
  1134. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";"
  1135. fi
  1136. fi
  1137. laststatus=0