Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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