Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

1310 řádky
31 KiB

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