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.
 
 
 
 
 
 

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