Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

1255 rindas
29 KiB

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