Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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