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

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