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

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