You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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