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.
 
 
 
 
 
 

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