Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

1223 рядки
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. __safe_add_path_r(){
  23. test -d "$1" && PATH="${PATH}:$1"
  24. }
  25. __safe_add_path_l(){
  26. test -d "$1" && PATH="$1:${PATH}"
  27. }
  28. __safe_add_path_l "$HOME/.local/bin"
  29. __safe_add_path_l "$HOME/.local/lib/gems/bin"
  30. __safe_add_path_r "/c/mingw/bin"
  31. __safe_add_path_r "/c/mingw/msys/1.0/bin"
  32. # macports coreutils
  33. # isdarwin cannot be used it is not defined yet
  34. __safe_add_path_l "/opt/local/bin"
  35. __safe_add_path_l "/opt/local/sbin"
  36. __safe_add_path_l "/opt/local/libexec/gnubin"
  37. __safe_add_path_l \
  38. "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/bin"
  39. test -f "${__dotdir}/rc.py" && export PYTHONSTARTUP="${__dotdir}/rc.py"
  40. test -d "$HOME/.local/lib/python/site-packages" && \
  41. export PYTHONPATH="${PYTHONPATH}:${HOME}/.local/lib/python/site-packages"
  42. export GEM_HOME="$HOME/.local/lib/gems"
  43. export RUBYLIB="$RUBYLIB:$HOME/.local/lib/gems/lib"
  44. # it is not so good
  45. # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  46. # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  47. # without this ENV i cannot run tmux. another way is to use --disable-shared
  48. # when building tmux
  49. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  50. then
  51. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  52. fi
  53. # in my environment powerdown does not work
  54. test -z "$SSH_CONNECTION" && \
  55. type setterm >/dev/null 2>&1 && \
  56. setterm -blank 30 -powersave on # -powerdown 10
  57. ##########################
  58. # system type
  59. # aliases:
  60. # ismsys, iscygwin iswindows, isdarwin, islinux,
  61. # with_coreutils, inbash, inzsh
  62. alias ismsys=false
  63. alias iscygwin=false
  64. alias iswindows=false
  65. alias isdarwin=false
  66. alias islinux=false
  67. alias with_coreutils=false # for mac
  68. case `uname` in
  69. (MINGW*) alias ismsys=true ;;
  70. (CYGWIN*) alias iscygwin=true ;;
  71. (Darwin*) alias isdarwin=true ;;
  72. (Linux*) alias islinux=true ;;
  73. esac
  74. null ls --version && alias with_coreutils=true
  75. ( ismsys || iscygwin ) && alias iswindows=true
  76. alias inbash=false
  77. alias inzsh=false
  78. if test -n "$BASH_VERSION"
  79. then
  80. alias inbash=true
  81. elif test -n "$ZSH_VERSION"
  82. then
  83. alias inzsh=true
  84. fi
  85. alias isinteractive=false
  86. __match "$-" i >/dev/null && alias isinteractive=true
  87. # alias issourced=true
  88. # expr "$0" : "^.*shrc$" >/dev/null && alias issourced=false # executed
  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. iscygwin && null type wget && alias wget="wget --no-check-certificate"
  371. isdarwin && alias updatedb="LC_ALL=C updatedb"
  372. # do not use locate installed by macports
  373. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  374. # pad
  375. alias pad=notepad
  376. null type gedit && alias pad=gedit
  377. null type leafpad && alias pad=leafpad
  378. isdarwin && alias pad="open -e"
  379. null type wicd-curses && alias wic=wicd-curses
  380. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  381. #alias wicn="wicd-cli -y -c -n"
  382. wicn(){
  383. if test $# -eq 0
  384. then
  385. local num
  386. wicd-cli -y -l | head
  387. echo -n "input num: "
  388. read num
  389. test -n "$num" && wicd-cli -y -c -n $num
  390. else
  391. wicd-cli -y -c -n $1
  392. fi
  393. }
  394. __find_latest_vimdir(){
  395. vimdir=/usr/share/vim
  396. if test -d "$vimdir"
  397. then
  398. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  399. else
  400. echo ""
  401. fi
  402. }
  403. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  404. do
  405. test -n "$f" || continue
  406. f="$f/macros/less.sh"
  407. test -f $f && alias vl=$f && break
  408. done
  409. alias pa=pacapt
  410. null type yaourt && alias yt=yaourt
  411. null type cower && alias cower="cower --color=auto"
  412. null type pacmatic && {
  413. alias pacman="pacmatic"
  414. export PACMAN="pacmatic"
  415. }
  416. _pacman_update_mirrorlist_with_reflector(){
  417. ml=/etc/pacman.d/mirrorlist
  418. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  419. if test -z "$cmd"
  420. then
  421. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  422. fi
  423. echo "Running $cmd ..." 1>&2
  424. sudo $cmd
  425. }
  426. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  427. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  428. null type apt-get && {
  429. alias aupgrade="sudo apt-get autoremove --yes && \
  430. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  431. alias aptin="apt-get install"
  432. alias aptsearch="apt-cache search"
  433. alias aptshow="apt-cache show"
  434. }
  435. null type port && {
  436. alias port="port -v"
  437. alias pupgrade="sudo port -v selfupdate && \
  438. { sudo port -v upgrade outdated; }"
  439. }
  440. if iscygwin; then
  441. null type windate || \
  442. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  443. # alias cygsu="cygstart /cygwinsetup.exe"
  444. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  445. fi
  446. g(){
  447. if test $# -eq 0 && null type git-info
  448. then
  449. git info
  450. else
  451. git -c color.ui=always "$@"
  452. fi
  453. }
  454. if null type _git && inbash
  455. then
  456. # enable programmable completion for g
  457. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  458. || complete -o default -o nospace -F _git g
  459. fi
  460. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  461. null type gitmemo && alias m=gitmemo
  462. alias setup.py="sudo python3 setup.py install --record files.txt"
  463. randomstr(){
  464. len=$1
  465. test -z "$len" && len=8
  466. uuidgen | tr -d - | cut -c 1-len
  467. }
  468. datestr(){
  469. # datestr yyyyMMdd-hhmmss
  470. if test -z "$1" || test "$1" == "-h"
  471. then
  472. echo "datestr: usage: datestr <yyyyMMddhhmmss>"
  473. return 1
  474. fi
  475. dfmt= # actual format for date command
  476. while test -n "$1"
  477. do
  478. fmt="$1"
  479. while test -n "$fmt"
  480. do
  481. case "$fmt" in
  482. yyyy*) # year
  483. dfmt="${dfmt}%Y"
  484. fmt="`echo "$fmt" | cut -c 5-`"
  485. ;;
  486. yy*) # last two digits of year
  487. dfmt="${dfmt}%y"
  488. fmt="`echo "$fmt" | cut -c 3-`"
  489. ;;
  490. MM*) # month (01..12)
  491. dfmt="${dfmt}%m"
  492. fmt="`echo "$fmt" | cut -c 3-`"
  493. ;;
  494. dd*) # day of month (01..12)
  495. dfmt="${dfmt}%d"
  496. fmt="`echo "$fmt" | cut -c 3-`"
  497. ;;
  498. HH* | hh*) # hour (00..23)
  499. dfmt="${dfmt}%H"
  500. fmt="`echo "$fmt" | cut -c 3-`"
  501. ;;
  502. mm*) # minute (00..59)
  503. dfmt="${dfmt}%M"
  504. fmt="`echo "$fmt" | cut -c 3-`"
  505. ;;
  506. ss*) # second (00..60)
  507. dfmt="${dfmt}%S"
  508. fmt="`echo "$fmt" | cut -c 3-`"
  509. ;;
  510. *)
  511. char=`echo "$fmt" | cut -c 1`
  512. dfmt="${dfmt}${char}"
  513. fmt="`echo "$fmt" | cut -c 2-`"
  514. ;;
  515. esac
  516. done
  517. shift
  518. done
  519. date +"$dfmt"
  520. }
  521. ssh(){
  522. __my_set_screen_title ssh
  523. command ssh "$@"
  524. }
  525. __ssh_with_cd(){
  526. # __ssh_with_cd <host> <directory> [<arg> ...]
  527. if test -z "$2"
  528. then
  529. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  530. return 1
  531. fi
  532. host="$1"
  533. shift
  534. dir="$1"
  535. shift
  536. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  537. }
  538. memo(){
  539. if test -z "$1"
  540. then
  541. $EDITOR memo.txt
  542. else
  543. $EDITOR "$1/memo.txt"
  544. fi
  545. }
  546. now(){
  547. local tformat="%Y/%m/%d %H:%M:%S %z"
  548. cal
  549. REPLY=
  550. printf "\\r`date "+${tformat}"`"
  551. read -t 1
  552. while test $? -ne 0
  553. do
  554. printf "\\r`date "+${tformat}"`"
  555. read -t 1
  556. done
  557. }
  558. s(){
  559. if git rev-parse --git-dir >/dev/null 2>&1
  560. then
  561. echo ">> git grep -n $@" 1>&2
  562. git grep -n "$@"
  563. elif which ag >/dev/null 2>&1
  564. then
  565. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  566. ag --pager="$PAGER" "$@"
  567. elif which ack >/dev/null 2>&1
  568. then
  569. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  570. ack --pager="$PAGER" "$@"
  571. else
  572. echo \
  573. ">> find . " \
  574. "-path '*/.git' -prune -o" \
  575. "-path '*/.svn' -prune -o" \
  576. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  577. if test $# -eq 0
  578. then
  579. echo "No search word given." 1>&2
  580. return 1
  581. fi
  582. find . \
  583. -path '*/.git' -prune -o \
  584. -path '*/.svn' -prune -o \
  585. -type -f -exec grep -nH -e --color=always "$@" {} + \
  586. | $PAGER
  587. fi
  588. }
  589. man(){
  590. env \
  591. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  592. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  593. LESS_TERMCAP_me=$(printf "\e[0m") \
  594. LESS_TERMCAP_se=$(printf "\e[0m") \
  595. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  596. LESS_TERMCAP_ue=$(printf "\e[0m") \
  597. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  598. man "$@"
  599. }
  600. netwait(){
  601. while ! ping -c 1 -t 1 example.com
  602. do
  603. true
  604. done
  605. echo network works.
  606. }
  607. __realpath(){
  608. if type realpath >/dev/null 2>&1
  609. then
  610. command realpath "$@"
  611. else
  612. while ! test -d $1
  613. do
  614. shift
  615. done
  616. (command cd "$d" && echo "$PWD")
  617. # local d="$OLDPWD"
  618. # command cd "$1"
  619. # echo "$PWD"
  620. # command cd "$d"
  621. fi
  622. }
  623. tx(){
  624. if test $# -eq 0
  625. then
  626. echo ":: tx <session> to attach."
  627. tmux ls
  628. elif tmux has -t "$1"
  629. then
  630. tmux attach -t "$1"
  631. else
  632. tmux new -s "$1"
  633. fi
  634. }
  635. _tmux_prefs(){
  636. null type tmux || return 1
  637. tmux set -g mode-keys vi
  638. }
  639. dt(){
  640. # dt [<name>] [<command ...>]
  641. __dtach_dir="${TMP}/dtach"
  642. install -d "${__dtach_dir}"
  643. if test -n "${__MY_DTACH}"
  644. then
  645. echo "Current session: ${__MY_DTACH}"
  646. fi
  647. if test -z "$1"
  648. then
  649. echo "Sessions:"
  650. ls "${__dtach_dir}"
  651. return 0
  652. elif test "$1" = "-h"
  653. then
  654. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  655. return 1
  656. fi
  657. soc_name="${__dtach_dir}/$1"
  658. shift
  659. if test -n "$__MY_DTACH"
  660. then
  661. echo "dtach session cannot be nested." 1>&2
  662. return 1
  663. elif test -S "$soc_name"
  664. then
  665. dtach -a "$soc_name" -e ^^
  666. elif test -e "$soc_name"
  667. then
  668. echo "dt: File named $soc_name already exists."
  669. return 1
  670. elif test -z "$1"
  671. then
  672. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  673. # echo "dt: Socket named $soc_name not exists and no command specified."
  674. # return 1
  675. else
  676. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  677. fi
  678. }
  679. scr(){
  680. test -n "$1" && pf="${1}-"
  681. local _tformat="%Y%m%d-%H%M%S%z"
  682. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  683. __MY_SCRIPT=${_file} script ${_file} "$@"
  684. }
  685. dtscr(){
  686. # dtscr <command ...>
  687. if test -z "$1"
  688. then
  689. echo "dtscr: usage: dtscr <command ...>"
  690. return 1
  691. fi
  692. local _cmdstr="`echo $@ | tr ' ' +`"
  693. local _tformat="%Y%m%d-%H%M%S%z"
  694. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  695. local _scr_file="${HOME}/${_name}.script"
  696. local _dt_dir="${TMP}/dtscr"
  697. install -d "$_dt_dir"
  698. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  699. # echo $_name
  700. # echo $_file
  701. }
  702. mcrypt_stream(){
  703. test $# -eq 2 || return 1
  704. case $1 in
  705. en)
  706. mcrypt --key $2 | base64 ;;
  707. de)
  708. base64 -d | mcrypt -d --key $2 ;;
  709. esac
  710. }
  711. gpg_stream(){
  712. test $# -eq 2 || return 1
  713. case $1 in
  714. en)
  715. gpg --passphrase $2 -c --batch |base64 ;;
  716. de)
  717. base64 -d|gpg --passphrase $2 -d --batch ;;
  718. esac
  719. }
  720. dgpg(){
  721. if test "$1" = help || test -z "$2"
  722. then
  723. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  724. return
  725. fi
  726. local srcs="$2"
  727. local dsts="$3"
  728. test -z "$dsts" && dsts="${srcs}.out"
  729. local pw
  730. echo -n "dgpg pw: "
  731. read -s pw
  732. echo ""
  733. test -z "$pw" && return 1
  734. for f in *${srcs}
  735. do
  736. local d="$(basename "$f" "${srcs}")${dsts}"
  737. echo -n "Processing $f to $d..."
  738. if test -d "$f"
  739. then
  740. echo "`printf 'failed (%s is directory)' $f`"
  741. elif test -f "$d"
  742. then
  743. echo "`printf 'failed (%s is already exists)' $d`"
  744. elif <"$f" gpg_stream $1 $pw >"$d" 2>/dev/null
  745. then
  746. echo "done"
  747. else
  748. echo "failed"
  749. test -f "$d" && rm "$d"
  750. fi
  751. done
  752. }
  753. alias enst="gpg_stream en"
  754. alias dest="gpg_stream de"
  755. showinfo(){
  756. echo "Japanese letters are 表示可能"
  757. __safe_run diskinfo
  758. ! isdarwin && test -n "${DISPLAY}" && {
  759. __safe_run xrandr | \grep --color=never ^Screen
  760. }
  761. iswindows || __safe_run finger $USER
  762. LANG=C __safe_runc id
  763. __safe_run xset q
  764. }
  765. x(){
  766. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  767. #mkdir -p ~/.var/log
  768. # nohup startx >~/.var/log/xorg.log 2>&1 &
  769. # exit
  770. exec startx
  771. else
  772. echo "X cant be started! Another X is already running?" 1>&2
  773. fi
  774. }
  775. bak(){
  776. for file in "$@"
  777. do
  778. cp -v ${file} ${file}.bak
  779. done
  780. }
  781. di(){
  782. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  783. then
  784. local diffcmd=colordiff
  785. else
  786. local diffcmd=diff
  787. fi
  788. ${diffcmd} -u "$@" | ${PAGER}
  789. }
  790. tb(){
  791. __datenum=`date +%Y%m%d-%H%M%S`
  792. __tb="$HOME/.var/tb/$__datenum"
  793. install -d "$__tb"
  794. for file in "$@"
  795. do
  796. mv -t "$__tb" "$file"
  797. done
  798. }
  799. mkcd(){
  800. if test -z "$1"
  801. then
  802. echo "mkcd: usage: mkcd <dir>"
  803. return 1
  804. elif test -d "$1"
  805. then
  806. echo "Dir \"$1\" already exists."
  807. else
  808. install -d "$1"
  809. echo "Dir \"$1\" created."
  810. fi
  811. cd "$1"
  812. }
  813. mkcdd(){
  814. # make and change date directory
  815. _d=`date +%Y%m%d-%H%M%S`
  816. mkcd "$_d"
  817. }
  818. if test -n "$TMUX" && null type reattach-to-user-namespace
  819. then
  820. alias pbpaste="reattach-to-user-namespace pbpaste"
  821. alias pbcopy="reattach-to-user-namespace pbcopy"
  822. fi
  823. catclip(){
  824. if iswindows
  825. then
  826. cat /dev/clipboard | tr -d \\r
  827. elif isdarwin
  828. then
  829. pbpaste
  830. else
  831. xclip -o -selection "clipboard"
  832. fi
  833. }
  834. setclip(){
  835. if test $# -eq 0
  836. then
  837. exec 3<&0
  838. else
  839. exec 3<<__EOF__
  840. `cat "$@"`
  841. __EOF__
  842. fi
  843. if iswindows
  844. then
  845. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  846. elif isdarwin
  847. then
  848. pbcopy 0<&3
  849. else
  850. 0<&3 xclip -i -f -selection "primary" | \
  851. xclip -i -f -selection "clipboard"
  852. fi
  853. exec 3<&-
  854. }
  855. open_file(){
  856. if iscygwin
  857. then
  858. cygstart "$@"
  859. elif ismsys
  860. then
  861. cmd.exe //c start "" "$@"
  862. elif isdarwin
  863. then
  864. touch "$@"
  865. open "$@"
  866. elif islinux
  867. then
  868. touch "$@"
  869. if null type pcmanfm; then
  870. LC_MESSAGES= pcmanfm "$@"
  871. else
  872. LC_MESSAGES= xdg-open "$@" &
  873. fi
  874. else
  875. cat "$@"
  876. fi
  877. }
  878. o(){
  879. if test $# -eq 0
  880. then
  881. open_file .
  882. else
  883. for f in "$@"
  884. do
  885. open_file "$(realpath "$f")"
  886. done
  887. fi
  888. }
  889. convmv_sjis2utf8_test(){
  890. convmv -r -f sjis -t utf8 *
  891. }
  892. convmv_sjis2utf8_notest(){
  893. convmv -r -f sjis -t utf8 * --notest
  894. }
  895. #################################################
  896. ## pastebin services
  897. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  898. sprunge(){
  899. # http://sprunge.us
  900. if test -z "$1"
  901. then
  902. curl -F 'sprunge=<-' http://sprunge.us
  903. else
  904. curl http://sprunge.us/$1
  905. fi
  906. }
  907. dpaste(){
  908. # http://dpaste.de
  909. if test -z "$1"
  910. then
  911. curl -F 'content=<-' https://dpaste.de/api/
  912. echo
  913. else
  914. curl https://dpaste.de/$1/raw/
  915. fi
  916. }
  917. ##########################
  918. # Zsh specific preferences
  919. if inzsh
  920. then
  921. bindkey -e
  922. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  923. autoload compinit; compinit
  924. unsetopt auto_menu
  925. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  926. setopt bash_auto_list
  927. autoload colors; colors
  928. autoload -Uz promptinit
  929. promptinit
  930. prompt walters
  931. fi
  932. ######################################
  933. ## Prompt Settings
  934. __my_moc_state(){
  935. type mocp >/dev/null 2>&1 || return
  936. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  937. printf "$1" "`mocp -Q %title 2>/dev/null`"
  938. }
  939. __my_parse_svn_branch() {
  940. local LANG=C
  941. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  942. local svn_repository_root=$(svn info 2>/dev/null | \
  943. sed -ne 's#^Repository Root: ##p')
  944. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  945. awk '{print $1}'
  946. }
  947. __my_svn_ps1(){
  948. if svn status >/dev/null 2>&1
  949. then
  950. local svn_branch=$(__my_parse_svn_branch)
  951. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  952. fi
  953. }
  954. __my_battery_status(){
  955. local dir=/sys/class/power_supply/BAT0
  956. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  957. test -r $dir/charge_now
  958. then
  959. local st=$(cat $dir/status)
  960. local full=$(cat $dir/charge_full)
  961. local now=$(cat $dir/charge_now)
  962. local rate=$(expr $now \* 100 / $full)
  963. printf "$1" "${st}:${rate}%"
  964. fi
  965. }
  966. alias bat='__my_battery_status %s\\n'
  967. ipaddress(){
  968. type ip >/dev/null 2>&1 || return 1
  969. local ip=$(LANG=C ip addr show scope global | \
  970. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  971. test -n "$ip" && printf $1 $ip
  972. }
  973. __my_ps1_str=""
  974. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  975. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  976. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  977. __my_ps1_scale(){
  978. if null type stty && ! ismsys
  979. then
  980. stty size | tr -d $'\n' | tr " " x
  981. printf " "
  982. fi
  983. }
  984. __my_ps1_tmux(){
  985. null type tmux || return $last
  986. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  987. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  988. }
  989. __my_ps1_moc(){
  990. __my_moc_state "[MOC:%s]"
  991. }
  992. for f in /usr/share/git/git-prompt.sh \
  993. /opt/local/share/git-core/git-prompt.sh \
  994. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  995. do
  996. test -r "$f" && inbash && . "$f" && break
  997. done
  998. GIT_PS1_SHOWDIRTYSTATE=t
  999. GIT_PS1_SHOWUPSTREAM=t
  1000. __my_ps1_git(){
  1001. null type __git_ps1 || return $last
  1002. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  1003. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  1004. }
  1005. __my_ps1_ipaddr(){
  1006. ! iswindows && ipaddress [Addr:%s]
  1007. }
  1008. __my_ps1_bttry(){
  1009. local bst="${TMP}/batterystatus"
  1010. if test -z "$DISPLAY" && ! iswindows
  1011. then
  1012. test -f $bst && local bstr="$(cat $bst)"
  1013. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  1014. echo "[Battery:$bstr]"
  1015. __my_battery_status %s >$bst &
  1016. fi
  1017. }
  1018. __my_ps1_dirs(){
  1019. dirs | wc -l
  1020. }
  1021. __my_ps1_jobs(){
  1022. jobs | wc -l
  1023. }
  1024. __my_alert_fail(){
  1025. test $laststatus -eq 0 || echo '!!! '
  1026. }
  1027. if test "$TERM" != dumb
  1028. then
  1029. __my_c1="\[\e[0;33m\]" # color for PWD
  1030. __my_c2="\[\e[0;36m\]" # color for user
  1031. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  1032. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  1033. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  1034. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  1035. else __my_c4="\[\e[1;32m\]" # color for ::
  1036. fi
  1037. __my_c5="\[\e[30;47m\]" # color for SCR
  1038. __my_cdef="\[\e[0m\]"
  1039. fi
  1040. _ps1_bash="\
  1041. ${__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\
  1042. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$laststatus $(__my_ps1_scale)\D{%T} ${__my_ps1_str}\$(__my_alert_fail)\$ "
  1043. inbash && PS1=$_ps1_bash
  1044. _ps1_zsh="$_ps1_bash"
  1045. #inzsh && PS1="$_ps1_zsh"
  1046. __my_set_screen_title(){
  1047. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1048. then
  1049. echo -ne "\033k$1\033\\"
  1050. fi
  1051. }
  1052. __my_set_title(){
  1053. case $TERM in
  1054. (rxvt*|xterm*|aterm|screen*)
  1055. title="$(echo $@)"
  1056. test -t 1 &&
  1057. test -n "$DISPLAY" &&
  1058. test -z "$EMACS" &&
  1059. echo -n -e "\033]0;${title}\007"
  1060. ;;
  1061. esac
  1062. }
  1063. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  1064. __my_set_screen_title \$(basename \"\$PWD\")/"
  1065. PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
  1066. laststatus=0