Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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