You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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