Você não pode selecionar mais de 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.
 
 
 
 
 
 

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