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

1096 рядки
25 KiB

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