Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

1313 righe
30 KiB

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