25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1222 lines
29 KiB

  1. #!/bin/sh
  2. # TODO: use tput
  3. ##########################################
  4. null(){
  5. "$@" >/dev/null 2>&1
  6. }
  7. __safe_run(){
  8. type $1 >/dev/null 2>&1 && "$@"
  9. }
  10. __match(){
  11. # __match str word
  12. # return 0 if word is found in str
  13. expr "$1" : ".*$2.*" >/dev/null
  14. }
  15. #################################
  16. # profile-like setups
  17. # aliases:
  18. # isinteractive: true if the current session is interactive
  19. # issourced: true if this file is sourced from another file (not so assured)
  20. # __firstload: true if this file is sourced for the first time (not so
  21. # assured)
  22. alias isinteractive=false
  23. __match "$-" i >/dev/null && alias isinteractive=true
  24. # alias issourced=true
  25. # expr "$0" : "^.*shrc$" >/dev/null && alias issourced=false # executed
  26. __safe_add_path_r(){
  27. test -d "$1" && PATH="${PATH}:$1"
  28. }
  29. __safe_add_path_l(){
  30. test -d "$1" && PATH="$1:${PATH}"
  31. }
  32. __safe_add_path_l "$HOME/.local/bin"
  33. __safe_add_path_l "$HOME/.local/lib/gems/bin"
  34. __safe_add_path_r "/c/mingw/bin"
  35. __safe_add_path_r "/c/mingw/msys/1.0/bin"
  36. # macports coreutils
  37. # isdarwin cannot be used it is not defined yet
  38. __safe_add_path_l "/opt/local/bin"
  39. __safe_add_path_l "/opt/local/sbin"
  40. __safe_add_path_l "/opt/local/libexec/gnubin"
  41. __safe_add_path_l \
  42. "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/bin"
  43. test -f "${__dotdir}/rc.py" && export PYTHONSTARTUP="${__dotdir}/rc.py"
  44. test -d "$HOME/.local/lib/python/site-packages" && \
  45. export PYTHONPATH="${PYTHONPATH}:${HOME}/.local/lib/python/site-packages"
  46. export GEM_HOME="$HOME/.local/lib/gems"
  47. export RUBYLIB="$RUBYLIB:$HOME/.local/lib/gems/lib"
  48. # it is not so good
  49. # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  50. # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  51. # without this ENV i cannot run tmux. another way is to use --disable-shared
  52. # when building tmux
  53. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  54. then
  55. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  56. fi
  57. # in my environment powerdown does not work
  58. test -z "$SSH_CONNECTION" && \
  59. type setterm >/dev/null 2>&1 && \
  60. setterm -blank 30 -powersave on # -powerdown 10
  61. ##########################
  62. # system type
  63. # aliases:
  64. # ismsys, iscygwin iswindows, isdarwin, islinux,
  65. # with_coreutils, inbash, inzsh
  66. alias ismsys=false
  67. alias iscygwin=false
  68. alias iswindows=false
  69. alias isdarwin=false
  70. alias islinux=false
  71. alias with_coreutils=false # for mac
  72. case `uname` in
  73. (MINGW*) alias ismsys=true ;;
  74. (CYGWIN*) alias iscygwin=true ;;
  75. (Darwin*) alias isdarwin=true ;;
  76. (Linux*) alias islinux=true ;;
  77. esac
  78. null ls --version && alias with_coreutils=true
  79. ( ismsys || iscygwin ) && alias iswindows=true
  80. alias inbash=false
  81. alias inzsh=false
  82. if test -n "$BASH_VERSION"
  83. then
  84. alias inbash=true
  85. elif test -n "$ZSH_VERSION"
  86. then
  87. alias inzsh=true
  88. fi
  89. #################################
  90. # file pathes:
  91. # shrc: Path to this file
  92. # dotdir: Path to .dotfiles directory
  93. if inbash
  94. then
  95. __shrc="$BASH_SOURCE"
  96. elif inzsh
  97. then
  98. __shrc="$0"
  99. fi
  100. __dotdir="`dirname "$__shrc"`"
  101. __homelocal="$HOME/.local"
  102. __homevar="$HOME/.var"
  103. test -d "$__homelocal" || install -d "$__homelocal"
  104. test -d "$__homevar" || install -d "$__homevar"
  105. ##################################
  106. # EnvVal definitions
  107. export LANG=ja_JP.UTF-8
  108. export LC_MESSAGES=C
  109. export LC_TIME=C
  110. export TERMCAP="${TERMCAP}:vb="
  111. ismsys && export HOSTNAME
  112. # export ENV=~/.shrc
  113. if ! with_coreutils
  114. then
  115. export LSCOLORS=gxfxcxdxbxegedabagacad
  116. else
  117. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  118. null type dircolors && eval `dircolors`
  119. fi
  120. if false iswindows
  121. then
  122. export PAGER='tr -d \\r | less'
  123. else
  124. export PAGER="less"
  125. fi
  126. export LESS="-iRMX"
  127. # Style for lesspipe is defined in esc.style
  128. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  129. for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
  130. do
  131. test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
  132. done
  133. test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
  134. if null type vim
  135. then
  136. export EDITOR=vim
  137. else
  138. export EDITOR=vi
  139. fi
  140. # export CDPATH=".:~"
  141. export VISUAL="$EDITOR"
  142. export GIT_PAGER="less -FS"
  143. export GIT_EDITOR="$EDITOR"
  144. export GIT_MERGE_AUTOEDIT=no
  145. if test -n "$TMUX" && \
  146. __match $TERM screen && \
  147. __match `tmux display -p '#{client_termname}'` 256color
  148. then
  149. TERM=screen-256color
  150. fi
  151. if test -z "$TMP"
  152. then
  153. if test -n "$TMPDIR"
  154. then
  155. export TMP=$TMPDIR
  156. elif test -n "$TEMP"
  157. then
  158. export TMP="$TEMP"
  159. else
  160. export TMP=/tmp
  161. fi
  162. fi
  163. __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
  164. export TEMP="$TMP"
  165. test -d "$TMP" || install -d "$TMP"
  166. ! iswindows && null type stty && {
  167. stty stop undef # unbind C-s to stop displaying output
  168. # stty erase '^h'
  169. }
  170. if iswindows; then
  171. export USER=$USERNAME
  172. fi
  173. if test -d ~/dbx
  174. then
  175. export CHIT_PATH="$HOME/dbx/.chit"
  176. fi
  177. ##########################
  178. # Setups
  179. __download(){
  180. # download <url> <file>
  181. if type wget >/dev/null 2>&1
  182. then
  183. wget "$1" -O "$2"
  184. elif type curl >/dev/null 2>&1
  185. then
  186. curl --url "$1" --output "$2"
  187. fi
  188. }
  189. __mysetup_fetch_script(){
  190. url="$1"
  191. name="$2"
  192. type "$name" >/dev/null 2>&1 || {
  193. __download "$url" "$HOME/.local/bin/$name" &&
  194. chmod u+x "$HOME/.local/bin/$name"
  195. }
  196. }
  197. __mysetup_darwin_set_defaults(){
  198. isdarwin || return 1
  199. # http://appdrill.net/60641/mac-boot-mute.html
  200. #sudo nvram SystemAudioVolume=%80
  201. # add quit entry in menu
  202. defaults write com.apple.finder QuitMenuItem -bool YES
  203. # show full path on titlebar
  204. defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
  205. # do not show desktop icons
  206. defaults write com.apple.finder CreateDesktop -boolean false
  207. killall Finder
  208. # disable dashboard
  209. #defaults write com.apple.dashboard mcx-disabled -bool YES
  210. }
  211. __mysetup_darwin_start_daemon(){
  212. isdarwin || return 1
  213. test "`launchctl getenv LC_ALL`" = C || sudo launchctl setenv LC_ALL C
  214. if ! (launchctl list | grep com.apple.locate) >/dev/null
  215. then
  216. sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  217. fi
  218. }
  219. __mysetup_git_config(){
  220. if ! null type git
  221. then
  222. echo "git not found"
  223. return 1
  224. fi
  225. _gitconfig="git config --global"
  226. $_gitconfig user.name '10sr'
  227. $_gitconfig user.email '8slashes+git@gmail.com'
  228. $_gitconfig core.autocrlf false
  229. $_gitconfig core.excludesfile '~/.gitignore'
  230. $_gitconfig color.ui auto
  231. $_gitconfig status.relativePaths false
  232. $_gitconfig status.showUntrackedFiles normal
  233. $_gitconfig log.date iso
  234. null type xz && \
  235. $_gitconfig tar.txz.command "xz -c"
  236. $_gitconfig push.default current
  237. $_gitconfig alias.graph "log --graph --date-order -C -M --pretty=tformat:\"%C(green)%h%C(reset) %C(white)%ad%C(reset) %C(red)%an%C(reset)%C(yellow)%d%C(reset) %C(white bold)%s%C(reset)\" --all --date=iso -n 499"
  238. $_gitconfig alias.st "status -s -b"
  239. $_gitconfig alias.b "branch"
  240. $_gitconfig alias.sb "show-branch"
  241. $_gitconfig alias.ci "commit --verbose"
  242. $_gitconfig alias.co "checkout"
  243. $_gitconfig alias.cim "commit --verbose -m"
  244. $_gitconfig alias.di "diff --color"
  245. $_gitconfig alias.me "merge --no-ff --stat -v"
  246. $_gitconfig alias.gr "grep -n"
  247. $_gitconfig alias.ls "ls-files"
  248. # $_gitconfig alias.ls "ls-files -v --full-name"
  249. # $_gitconfig alias.ls "status -u -s ."
  250. $_gitconfig alias.sl "!sl"
  251. # $_gitconfig alias.my-ls "ls-files | xargs ls"
  252. # $_gitconfig alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  253. $_gitconfig alias.addi "add -i"
  254. $_gitconfig alias.clean-p "!test -z \"\$(git status -s -uno)\""
  255. $_gitconfig alias.newb "checkout -b"
  256. $_gitconfig alias.endb \
  257. "!sh -cx 'git stash && git checkout master && git merge --no-ff -'"
  258. #$_gitconfig alias.wc "!git ls-files -z | xargs -0 wc"
  259. # $_gitconfig push.default "simple"
  260. if iswindows; then
  261. $_gitconfig core.fileMode false
  262. fi
  263. }
  264. __mysetup_mkdirs(){
  265. install -d "$HOME/.local/bin"
  266. }
  267. __mysetup(){
  268. __mysetup_mkdirs
  269. __mysetup_fetch_script \
  270. https://gist.github.com/10sr/6852317/raw/colortable16.sh colortable16.sh
  271. __mysetup_fetch_script \
  272. https://gist.github.com/10sr/6852331/raw/256colors2.pl 256colors2.pl
  273. if isdarwin
  274. then
  275. __mysetup_darwin_set_defaults
  276. __mysetup_darwin_start_daemon
  277. fi
  278. }
  279. #######################
  280. if ! isinteractive
  281. then
  282. if test "$1" = setup
  283. then
  284. __mysetup
  285. exit 0
  286. fi
  287. # if this file is sourced return, if executed directly exit
  288. return 2>/dev/null || exit
  289. fi
  290. ######################
  291. # Print welcome messages
  292. iswindows && alias tty="echo cmd.exe"
  293. type fortune >/dev/null 2>&1 && {
  294. fortune
  295. echo
  296. fortune -o
  297. echo
  298. }
  299. uname -a
  300. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  301. running $BASH $BASH_VERSION
  302. if test -n "$TMUX"
  303. then
  304. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  305. 2>/dev/null
  306. echo
  307. fi
  308. ###################################
  309. # some aliases and functions
  310. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  311. _coloroption=" --color=auto"
  312. ( ! with_coreutils && isdarwin ) || iswindows || \
  313. _timeoption=" --time-style=long-iso"
  314. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  315. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  316. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  317. _timeformat_num="%Y%m%d%H%M%S"
  318. alias datenum="date +$_timeformat_num"
  319. alias ls="ls -hCF${_coloroption}${_timeoption}"
  320. # export GREP_OPTIONS=""
  321. alias gr="grep -n --color=always"
  322. iswindows && alias grep="grep -n"
  323. # alias ll="ls -l"
  324. # alias la="ls -A"
  325. # alias lla="ls -Al"
  326. alias less="less -F"
  327. null type emacs && alias em="emacs -nw"
  328. null type vim && alias vi=vim
  329. alias pstree="LANG=C pstree"
  330. alias cp="cp -v"
  331. alias mv="mv -v"
  332. alias rm="rm -v"
  333. alias psaux="ps auxww"
  334. alias q=exit
  335. null type e3em && alias e3=e3em
  336. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  337. alias po=popd
  338. alias pu=pushd
  339. null type sudo && alias sudo="sudo " # use aliases through sudo
  340. null type sudoedit && alias sudoe="sudoedit"
  341. null type halt && alias halt="sudo halt"
  342. null type reboot && alias reboot="sudo reboot"
  343. null type dbus-send && {
  344. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  345. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  346. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  347. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  348. }
  349. alias rand="echo \$RANDOM"
  350. null type file-roller && alias xunp="file-roller -h"
  351. null type paco && alias pc="sudo \paco -D"
  352. alias pycalc="python -i -c 'from math import *' "
  353. null type python3 && alias py3=python3
  354. null type python2 && alias py2=python2
  355. alias _reloadrc="exec \"$SHELL\""
  356. # alias mytime="date +%Y%m%d-%H%M%S"
  357. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  358. # type trash >/dev/null 2>&1 && alias rm=trash
  359. null type mpg123 && alias mpg123="mpg123 -C -v --title"
  360. null type xmms2 && alias xm="xmms2"
  361. #export PLAYER="mpg123 -C -v --title"
  362. null type screen && alias screen="screen -e^z^z"
  363. #alias zcd="cd \`zenity --file-selection --directory\`"
  364. null type gtags && alias gtags="gtags --verbose"
  365. null type htags && alias htags="htags --xhtml --symbol --line-number \
  366. --frame --alphabet --verbose"
  367. null type aunpack && alias au=aunpack
  368. null type lv && alias lv="lv|less"
  369. null type rsync && alias rs="rsync --progress --itemize-changes --compress"
  370. isdarwin && alias updatedb="LC_ALL=C updatedb"
  371. # do not use locate installed by macports
  372. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  373. # pad
  374. alias pad=notepad
  375. null type gedit && alias pad=gedit
  376. null type leafpad && alias pad=leafpad
  377. isdarwin && alias pad="open -e"
  378. null type wicd-curses && alias wic=wicd-curses
  379. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  380. #alias wicn="wicd-cli -y -c -n"
  381. wicn(){
  382. if test $# -eq 0
  383. then
  384. local num
  385. wicd-cli -y -l | head
  386. echo -n "input num: "
  387. read num
  388. test -n "$num" && wicd-cli -y -c -n $num
  389. else
  390. wicd-cli -y -c -n $1
  391. fi
  392. }
  393. __find_latest_vimdir(){
  394. vimdir=/usr/share/vim
  395. if test -d "$vimdir"
  396. then
  397. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  398. else
  399. echo ""
  400. fi
  401. }
  402. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  403. do
  404. test -n "$f" || continue
  405. f="$f/macros/less.sh"
  406. test -f $f && alias vl=$f && break
  407. done
  408. alias pa=pacapt
  409. null type yaourt && alias yt=yaourt
  410. null type cower && alias cower="cower --color=auto"
  411. null type pacmatic && {
  412. alias pacman="pacmatic"
  413. export PACMAN="pacmatic"
  414. }
  415. _pacman_update_mirrorlist_with_reflector(){
  416. ml=/etc/pacman.d/mirrorlist
  417. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  418. if test -z "$cmd"
  419. then
  420. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  421. fi
  422. echo "Running $cmd ..." 1>&2
  423. sudo $cmd
  424. }
  425. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  426. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  427. null type apt-get && {
  428. alias aupgrade="sudo apt-get autoremove --yes && \
  429. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  430. alias aptin="apt-get install"
  431. alias aptsearch="apt-cache search"
  432. alias aptshow="apt-cache show"
  433. }
  434. null type port && {
  435. alias port="port -v"
  436. alias pupgrade="sudo port -v selfupdate && \
  437. { sudo port -v upgrade outdated; }"
  438. }
  439. if iscygwin; then
  440. null type windate || \
  441. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  442. # alias cygsu="cygstart /cygwinsetup.exe"
  443. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  444. fi
  445. g(){
  446. if test $# -eq 0 && null type git-info
  447. then
  448. git info
  449. else
  450. git -c color.ui=always "$@"
  451. fi
  452. }
  453. if null type _git && inbash
  454. then
  455. # enable programmable completion for g
  456. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  457. || complete -o default -o nospace -F _git g
  458. fi
  459. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  460. null type gitmemo && alias m=gitmemo
  461. alias setup.py="sudo python3 setup.py install --record files.txt"
  462. randomstr(){
  463. len=$1
  464. test -z "$len" && len=8
  465. uuidgen | tr -d - | cut -c 1-len
  466. }
  467. datestr(){
  468. # datestr yyyyMMdd-hhmmss
  469. if test -z "$1" || test "$1" == "-h"
  470. then
  471. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  472. return 1
  473. fi
  474. dfmt= # actual format for date command
  475. while test -n "$1"
  476. do
  477. fmt="$1"
  478. while test -n "$fmt"
  479. do
  480. case "$fmt" in
  481. yyyy*) # year
  482. dfmt="${dfmt}%Y"
  483. fmt="`echo "$fmt" | cut -c 5-`"
  484. ;;
  485. yy*) # last two digits of year
  486. dfmt="${dfmt}%y"
  487. fmt="`echo "$fmt" | cut -c 3-`"
  488. ;;
  489. MM*) # month (01..12)
  490. dfmt="${dfmt}%m"
  491. fmt="`echo "$fmt" | cut -c 3-`"
  492. ;;
  493. dd*) # day of month (01..12)
  494. dfmt="${dfmt}%d"
  495. fmt="`echo "$fmt" | cut -c 3-`"
  496. ;;
  497. HH* | hh*) # hour (00..23)
  498. dfmt="${dfmt}%H"
  499. fmt="`echo "$fmt" | cut -c 3-`"
  500. ;;
  501. mm*) # minute (00..59)
  502. dfmt="${dfmt}%M"
  503. fmt="`echo "$fmt" | cut -c 3-`"
  504. ;;
  505. ss*) # second (00..60)
  506. dfmt="${dfmt}%S"
  507. fmt="`echo "$fmt" | cut -c 3-`"
  508. ;;
  509. *)
  510. char=`echo "$fmt" | cut -c 1`
  511. dfmt="${dfmt}${char}"
  512. fmt="`echo "$fmt" | cut -c 2-`"
  513. ;;
  514. esac
  515. done
  516. shift
  517. done
  518. date +"$dfmt"
  519. }
  520. ssh(){
  521. __my_set_screen_title ssh
  522. command ssh "$@"
  523. }
  524. __ssh_with_cd(){
  525. # __ssh_with_cd <host> <directory> [<arg> ...]
  526. if test -z "$2"
  527. then
  528. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  529. return 1
  530. fi
  531. host="$1"
  532. shift
  533. dir="$1"
  534. shift
  535. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  536. }
  537. memo(){
  538. if test -z "$1"
  539. then
  540. $EDITOR memo.txt
  541. else
  542. $EDITOR "$1/memo.txt"
  543. fi
  544. }
  545. now(){
  546. local tformat="%Y/%m/%d %H:%M:%S %z"
  547. cal
  548. REPLY=
  549. printf "\\r`date "+${tformat}"`"
  550. read -t 1
  551. while test $? -ne 0
  552. do
  553. printf "\\r`date "+${tformat}"`"
  554. read -t 1
  555. done
  556. }
  557. s(){
  558. if git rev-parse --git-dir >/dev/null 2>&1
  559. then
  560. echo ">> git grep -n $@" 1>&2
  561. git grep -n "$@"
  562. elif which ag >/dev/null 2>&1
  563. then
  564. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  565. ag --pager="$PAGER" "$@"
  566. elif which ack >/dev/null 2>&1
  567. then
  568. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  569. ack --pager="$PAGER" "$@"
  570. else
  571. echo \
  572. ">> find . " \
  573. "-path '*/.git' -prune -o" \
  574. "-path '*/.svn' -prune -o" \
  575. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  576. if test $# -eq 0
  577. then
  578. echo "No search word given." 1>&2
  579. return 1
  580. fi
  581. find . \
  582. -path '*/.git' -prune -o \
  583. -path '*/.svn' -prune -o \
  584. -type -f -exec grep -nH -e --color=always "$@" {} + \
  585. | $PAGER
  586. fi
  587. }
  588. man(){
  589. env \
  590. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  591. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  592. LESS_TERMCAP_me=$(printf "\e[0m") \
  593. LESS_TERMCAP_se=$(printf "\e[0m") \
  594. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  595. LESS_TERMCAP_ue=$(printf "\e[0m") \
  596. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  597. man "$@"
  598. }
  599. netwait(){
  600. while ! ping -c 1 -t 1 example.com
  601. do
  602. true
  603. done
  604. echo network works.
  605. }
  606. __realpath(){
  607. if type realpath >/dev/null 2>&1
  608. then
  609. command realpath "$@"
  610. else
  611. while ! test -d $1
  612. do
  613. shift
  614. done
  615. (command cd "$d" && echo "$PWD")
  616. # local d="$OLDPWD"
  617. # command cd "$1"
  618. # echo "$PWD"
  619. # command cd "$d"
  620. fi
  621. }
  622. tx(){
  623. if test $# -eq 0
  624. then
  625. echo ":: tx <session> to attach."
  626. tmux ls
  627. elif tmux has -t "$1"
  628. then
  629. tmux attach -t "$1"
  630. else
  631. tmux new -s "$1"
  632. fi
  633. }
  634. _tmux_prefs(){
  635. null type tmux || return 1
  636. tmux set -g mode-keys vi
  637. }
  638. dt(){
  639. # dt [<name>] [<command ...>]
  640. __dtach_dir="${TMP}/dtach"
  641. install -d "${__dtach_dir}"
  642. if test -n "${__MY_DTACH}"
  643. then
  644. echo "Current session: ${__MY_DTACH}"
  645. fi
  646. if test -z "$1"
  647. then
  648. echo "Sessions:"
  649. ls "${__dtach_dir}"
  650. return 0
  651. elif test "$1" = "-h"
  652. then
  653. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  654. return 1
  655. fi
  656. soc_name="${__dtach_dir}/$1"
  657. shift
  658. if test -n "$__MY_DTACH"
  659. then
  660. echo "dtach session cannot be nested." 1>&2
  661. return 1
  662. elif test -S "$soc_name"
  663. then
  664. dtach -a "$soc_name" -e ^^
  665. elif test -e "$soc_name"
  666. then
  667. echo "dt: File named $soc_name already exists."
  668. return 1
  669. elif test -z "$1"
  670. then
  671. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  672. # echo "dt: Socket named $soc_name not exists and no command specified."
  673. # return 1
  674. else
  675. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  676. fi
  677. }
  678. scr(){
  679. test -n "$1" && pf="${1}-"
  680. local _tformat="%Y%m%d-%H%M%S%z"
  681. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  682. __MY_SCRIPT=${_file} script ${_file} "$@"
  683. }
  684. dtscr(){
  685. # dtscr <command ...>
  686. if test -z "$1"
  687. then
  688. echo "dtscr: usage: dtscr <command ...>"
  689. return 1
  690. fi
  691. local _cmdstr="`echo $@ | tr ' ' +`"
  692. local _tformat="%Y%m%d-%H%M%S%z"
  693. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  694. local _scr_file="${HOME}/${_name}.script"
  695. local _dt_dir="${TMP}/dtscr"
  696. install -d "$_dt_dir"
  697. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  698. # echo $_name
  699. # echo $_file
  700. }
  701. mcrypt_stream(){
  702. test $# -eq 2 || return 1
  703. case $1 in
  704. en)
  705. mcrypt --key $2 | base64 ;;
  706. de)
  707. base64 -d | mcrypt -d --key $2 ;;
  708. esac
  709. }
  710. gpg_stream(){
  711. test $# -eq 2 || return 1
  712. case $1 in
  713. en)
  714. gpg --passphrase $2 -c --batch |base64 ;;
  715. de)
  716. base64 -d|gpg --passphrase $2 -d --batch ;;
  717. esac
  718. }
  719. dgpg(){
  720. if test "$1" = help || test -z "$2"
  721. then
  722. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  723. return
  724. fi
  725. local srcs="$2"
  726. local dsts="$3"
  727. test -z "$dsts" && dsts="${srcs}.out"
  728. local pw
  729. echo -n "dgpg pw: "
  730. read -s pw
  731. echo ""
  732. test -z "$pw" && return 1
  733. for f in *${srcs}
  734. do
  735. local d="$(basename "$f" "${srcs}")${dsts}"
  736. echo -n "Processing $f to $d..."
  737. if test -d "$f"
  738. then
  739. echo "`printf 'failed (%s is directory)' $f`"
  740. elif test -f "$d"
  741. then
  742. echo "`printf 'failed (%s is already exists)' $d`"
  743. elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
  744. then
  745. echo "done"
  746. else
  747. echo "failed"
  748. test -f "$d" && rm "$d"
  749. fi
  750. done
  751. }
  752. alias enst="gpg_stream en"
  753. alias dest="gpg_stream de"
  754. showinfo(){
  755. echo "Japanese letters are 表示可能"
  756. __safe_run diskinfo
  757. ! isdarwin && test -n "${DISPLAY}" && {
  758. __safe_run xrandr | \grep --color=never ^Screen
  759. }
  760. iswindows || __safe_run finger $USER
  761. LANG=C __safe_runc id
  762. __safe_run xset q
  763. }
  764. x(){
  765. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  766. #mkdir -p ~/.var/log
  767. # nohup startx >~/.var/log/xorg.log 2>&1 &
  768. # exit
  769. exec startx
  770. else
  771. echo "X cant be started! Another X is already running?" 1>&2
  772. fi
  773. }
  774. bak(){
  775. for file in "$@"
  776. do
  777. cp -v ${file} ${file}.bak
  778. done
  779. }
  780. di(){
  781. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  782. then
  783. local diffcmd=colordiff
  784. else
  785. local diffcmd=diff
  786. fi
  787. ${diffcmd} -u "$@" | ${PAGER}
  788. }
  789. tb(){
  790. __datenum=`date +%Y%m%d-%H%M%S`
  791. __tb="$HOME/.var/tb/$__datenum"
  792. install -d "$__tb"
  793. for file in "$@"
  794. do
  795. mv -t "$__tb" "$file"
  796. done
  797. }
  798. mkcd(){
  799. if test -z "$1"
  800. then
  801. echo "mkcd: usage: mkcd <dir>"
  802. return 1
  803. elif test -d "$1"
  804. then
  805. echo "Dir \"$1\" already exists."
  806. else
  807. install -d "$1"
  808. echo "Dir \"$1\" created."
  809. fi
  810. cd "$1"
  811. }
  812. mkcdd(){
  813. # make and change date directory
  814. _d=`date +%Y%m%d-%H%M%S`
  815. mkcd "$_d"
  816. }
  817. if test -n "$TMUX" && null type reattach-to-user-namespace
  818. then
  819. alias pbpaste="reattach-to-user-namespace pbpaste"
  820. alias pbcopy="reattach-to-user-namespace pbcopy"
  821. fi
  822. catclip(){
  823. if iswindows
  824. then
  825. cat /dev/clipboard | tr -d \\r
  826. elif isdarwin
  827. then
  828. pbpaste
  829. else
  830. xclip -o -selection "clipboard"
  831. fi
  832. }
  833. setclip(){
  834. if test $# -eq 0
  835. then
  836. exec 3<&0
  837. else
  838. exec 3<<__EOF__
  839. `cat "$@"`
  840. __EOF__
  841. fi
  842. if iswindows
  843. then
  844. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  845. elif isdarwin
  846. then
  847. pbcopy 0<&3
  848. else
  849. 0<&3 xclip -i -f -selection "primary" | \
  850. xclip -i -f -selection "clipboard"
  851. fi
  852. exec 3<&-
  853. }
  854. open_file(){
  855. if iscygwin
  856. then
  857. cygstart "$@"
  858. elif ismsys
  859. then
  860. cmd.exe //c start "" "$@"
  861. elif isdarwin
  862. then
  863. touch "$@"
  864. open "$@"
  865. elif islinux
  866. then
  867. touch "$@"
  868. if null type pcmanfm; then
  869. LC_MESSAGES= pcmanfm "$@"
  870. else
  871. LC_MESSAGES= xdg-open "$@" &
  872. fi
  873. else
  874. cat "$@"
  875. fi
  876. }
  877. o(){
  878. if test $# -eq 0
  879. then
  880. open_file .
  881. else
  882. for f in "$@"
  883. do
  884. open_file "$(realpath "$f")"
  885. done
  886. fi
  887. }
  888. convmv_sjis2utf8_test(){
  889. convmv -r -f sjis -t utf8 *
  890. }
  891. convmv_sjis2utf8_notest(){
  892. convmv -r -f sjis -t utf8 * --notest
  893. }
  894. #################################################
  895. ## pastebin services
  896. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  897. sprunge(){
  898. # http://sprunge.us
  899. if test -z "$1"
  900. then
  901. curl -F 'sprunge=<-' http://sprunge.us
  902. else
  903. curl http://sprunge.us/$1
  904. fi
  905. }
  906. dpaste(){
  907. # http://dpaste.de
  908. if test -z "$1"
  909. then
  910. curl -F 'content=<-' https://dpaste.de/api/
  911. echo
  912. else
  913. curl https://dpaste.de/$1/raw/
  914. fi
  915. }
  916. ##########################
  917. # Zsh specific preferences
  918. if inzsh
  919. then
  920. bindkey -e
  921. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  922. autoload compinit; compinit
  923. unsetopt auto_menu
  924. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  925. setopt bash_auto_list
  926. autoload colors; colors
  927. autoload -Uz promptinit
  928. promptinit
  929. prompt walters
  930. fi
  931. ######################################
  932. ## Prompt Settings
  933. __my_moc_state(){
  934. type mocp >/dev/null 2>&1 || return
  935. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  936. printf "$1" "`mocp -Q %title 2>/dev/null`"
  937. }
  938. __my_parse_svn_branch() {
  939. local LANG=C
  940. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  941. local svn_repository_root=$(svn info 2>/dev/null | \
  942. sed -ne 's#^Repository Root: ##p')
  943. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  944. awk '{print $1}'
  945. }
  946. __my_svn_ps1(){
  947. if svn status >/dev/null 2>&1
  948. then
  949. local svn_branch=$(__my_parse_svn_branch)
  950. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  951. fi
  952. }
  953. __my_battery_status(){
  954. local dir=/sys/class/power_supply/BAT0
  955. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  956. test -r $dir/charge_now
  957. then
  958. local st=$(cat $dir/status)
  959. local full=$(cat $dir/charge_full)
  960. local now=$(cat $dir/charge_now)
  961. local rate=$(expr $now \* 100 / $full)
  962. printf "$1" "${st}:${rate}%"
  963. fi
  964. }
  965. alias bat='__my_battery_status %s\\n'
  966. ipaddress(){
  967. type ip >/dev/null 2>&1 || return 1
  968. local ip=$(LANG=C ip addr show scope global | \
  969. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  970. test -n "$ip" && printf $1 $ip
  971. }
  972. __my_ps1_str=""
  973. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  974. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  975. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  976. __my_ps1_scale(){
  977. if null type stty && ! ismsys
  978. then
  979. stty size | tr -d $'\n' | tr " " x
  980. printf " "
  981. fi
  982. }
  983. __my_ps1_tmux(){
  984. null type tmux || return $last
  985. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  986. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  987. }
  988. __my_ps1_moc(){
  989. __my_moc_state "[MOC:%s]"
  990. }
  991. for f in /usr/share/git/git-prompt.sh \
  992. /opt/local/share/git-core/git-prompt.sh \
  993. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  994. do
  995. test -r "$f" && inbash && . "$f" && break
  996. done
  997. GIT_PS1_SHOWDIRTYSTATE=t
  998. GIT_PS1_SHOWUPSTREAM=t
  999. __my_ps1_git(){
  1000. null type __git_ps1 || return $last
  1001. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  1002. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  1003. }
  1004. __my_ps1_ipaddr(){
  1005. ! iswindows && ipaddress [Addr:%s]
  1006. }
  1007. __my_ps1_bttry(){
  1008. local bst="${TMP}/batterystatus"
  1009. if test -z "$DISPLAY" && ! iswindows
  1010. then
  1011. test -f $bst && local bstr="$(cat $bst)"
  1012. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  1013. echo "[Battery:$bstr]"
  1014. __my_battery_status %s >$bst &
  1015. fi
  1016. }
  1017. __my_ps1_dirs(){
  1018. dirs | wc -l
  1019. }
  1020. __my_ps1_jobs(){
  1021. jobs | wc -l
  1022. }
  1023. __my_alert_fail(){
  1024. test $laststatus -eq 0 || echo '!!! '
  1025. }
  1026. if test "$TERM" != dumb
  1027. then
  1028. __my_c1="\[\e[0;33m\]" # color for PWD
  1029. __my_c2="\[\e[0;36m\]" # color for user
  1030. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  1031. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  1032. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  1033. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  1034. else __my_c4="\[\e[1;32m\]" # color for ::
  1035. fi
  1036. __my_c5="\[\e[30;47m\]" # color for SCR
  1037. __my_cdef="\[\e[0m\]"
  1038. fi
  1039. _ps1_bash="\
  1040. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  1041. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$laststatus $(__my_ps1_scale)\D{%T} ${__my_ps1_str}\$(__my_alert_fail)\$ "
  1042. inbash && PS1=$_ps1_bash
  1043. _ps1_zsh="$_ps1_bash"
  1044. #inzsh && PS1="$_ps1_zsh"
  1045. __my_set_screen_title(){
  1046. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1047. then
  1048. echo -ne "\033k$1\033\\"
  1049. fi
  1050. }
  1051. __my_set_title(){
  1052. case $TERM in
  1053. (rxvt*|xterm*|aterm|screen*)
  1054. title="$(echo $@)"
  1055. test -t 1 &&
  1056. test -n "$DISPLAY" &&
  1057. test -z "$EMACS" &&
  1058. echo -n -e "\033]0;${title}\007"
  1059. ;;
  1060. esac
  1061. }
  1062. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  1063. __my_set_screen_title \$(basename \"\$PWD\")/"
  1064. PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
  1065. laststatus=0