Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

1263 righe
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 pat
  17. # return 0 if pat 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. if test -f /Applications/Vivaldi.app/Contents/MacOS/Vivaldi && null type snowsql
  770. then
  771. # Use Vivaldi for snowsql external auth login
  772. alias snowsql="BROWSER=/Applications/Vivaldi.app/Contents/MacOS/Vivaldi snowsql"
  773. fi
  774. #################################################
  775. ## pastebin services
  776. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  777. sprunge(){
  778. # http://sprunge.us
  779. if test -z "$1"
  780. then
  781. curl -F 'sprunge=<-' http://sprunge.us
  782. else
  783. curl http://sprunge.us/$1
  784. fi
  785. }
  786. dpaste(){
  787. # http://dpaste.de
  788. if test -z "$1"
  789. then
  790. curl -F 'content=<-' https://dpaste.de/api/
  791. echo
  792. else
  793. curl https://dpaste.de/$1/raw/
  794. fi
  795. }
  796. ######################################
  797. ## Prompt Settings
  798. __my_moc_state(){
  799. type mocp >/dev/null 2>&1 || return
  800. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  801. printf "$1" "`mocp -Q %title 2>/dev/null`"
  802. }
  803. __my_parse_svn_branch() {
  804. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  805. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  806. sed -ne 's#^Repository Root: ##p')
  807. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  808. awk '{print $1}'
  809. }
  810. __my_svn_ps1(){
  811. if svn status >/dev/null 2>&1
  812. then
  813. ___svn_branch=$(__my_parse_svn_branch)
  814. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  815. fi
  816. }
  817. __my_battery_status(){
  818. ___dir=/sys/class/power_supply/BAT0
  819. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  820. test -r $___dir/charge_now
  821. then
  822. ___st=$(cat $___dir/status)
  823. ___full=$(cat $___dir/charge_full)
  824. ___now=$(cat $___dir/charge_now)
  825. ___rate=$(expr "$___now" \* 100 / "$___full")
  826. printf "$1" "${___st}:${___rate}%"
  827. fi
  828. }
  829. __my_ps1_scale(){
  830. if null type stty && ! $ismsys
  831. then
  832. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  833. fi
  834. }
  835. __my_ps1_tmux(){
  836. null type tmux || return $last
  837. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  838. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  839. }
  840. __my_ps1_moc(){
  841. __my_moc_state "[MOC:%s]"
  842. }
  843. for f in /usr/share/git/git-prompt.sh \
  844. "`brew --prefix 2>/dev/null`"/etc/bash_completion.d/git-prompt.sh \
  845. /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh \
  846. /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh \
  847. /usr/local/opt/bash-git-prompt/share/gitprompt.sh \
  848. /usr/share/git-core/contrib/completion/git-prompt.sh \
  849. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  850. /etc/bash_completion.d/git-prompt \
  851. /opt/local/share/git-core/git-prompt.sh \
  852. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  853. do
  854. if test -r "$f" && ($inbash || $inzsh)
  855. then
  856. . "$f"
  857. break
  858. fi
  859. done
  860. export GIT_PS1_SHOWDIRTYSTATE=t
  861. export GIT_PS1_SHOWUPSTREAM=t
  862. __my_ps1_git(){
  863. null type __git_ps1 || return $last
  864. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  865. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  866. }
  867. __printf_ipaddr(){
  868. # ipaddress <fmt>
  869. type ip >/dev/null 2>&1 || return 1
  870. ___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. alias addr="__printf_ipaddr '%s'"
  875. __my_ps1_ipaddr(){
  876. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  877. }
  878. __my_ps1_bttry(){
  879. ___bst="$__homerun/batterystatus"
  880. if test -z "$DISPLAY" && ! $iswindows
  881. then
  882. test -f $___bst && ___bstr="$(cat $___bst)"
  883. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  884. echo "[Battery:${___bstr}]"
  885. __my_battery_status %s >$___bst &
  886. fi
  887. }
  888. __my_ps1_memo(){
  889. test -f memo.txt && echo "m:$(echo $(wc -c memo.txt) | cut -f 1 -d " ")"
  890. }
  891. __my_ps1_dirs(){
  892. dirs | wc -l
  893. }
  894. __my_ps1_jobs(){
  895. # __my_ps1_jobs [<num>]
  896. if test -n "$1"
  897. then
  898. jobs="$1"
  899. else
  900. jobs="`jobs | wc -l`"
  901. fi
  902. if test "$jobs" -gt 0
  903. then
  904. echo "JOBS:$jobs"
  905. fi
  906. }
  907. __my_ps1_dollar(){
  908. if test -z "$2"
  909. then
  910. printf "$1"
  911. else
  912. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' "$2" "$1"
  913. fi
  914. }
  915. __my_alert_fail(){
  916. test $laststatus -eq 0 || \
  917. echo "STATUS:${laststatus}"
  918. }
  919. # About ansi escape sequences
  920. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  921. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  922. if $inbash || $inzsh
  923. then
  924. if $inzsh
  925. then
  926. __attr_beg=$'%{\033['
  927. __attr_end='m%}'
  928. else
  929. __attr_beg='\[\033['
  930. __attr_end='m\]'
  931. fi
  932. __color_default="${__attr_beg}0${__attr_end}"
  933. __color_black="${__attr_beg}0;30${__attr_end}"
  934. __color_red="${__attr_beg}0;31${__attr_end}"
  935. __color_green="${__attr_beg}0;32${__attr_end}"
  936. __color_yellow="${__attr_beg}0;33${__attr_end}"
  937. __color_blue="${__attr_beg}0;34${__attr_end}"
  938. __color_magenta="${__attr_beg}0;35${__attr_end}"
  939. __color_cyan="${__attr_beg}0;36${__attr_end}"
  940. __color_white="${__attr_beg}0;37${__attr_end}"
  941. __color_light_black="${__attr_beg}1;30${__attr_end}"
  942. __color_light_red="${__attr_beg}1;31${__attr_end}"
  943. __color_light_green="${__attr_beg}1;32${__attr_end}"
  944. __color_light_yellow="${__attr_beg}1;33${__attr_end}"
  945. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  946. __color_light_magenta="${__attr_beg}1;35${__attr_end}"
  947. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  948. __color_light_white="${__attr_beg}1;37${__attr_end}"
  949. __color_bg_black="${__attr_beg}40${__attr_end}"
  950. __color_bg_red="${__attr_beg}41${__attr_end}"
  951. __color_bg_green="${__attr_beg}42${__attr_end}"
  952. __color_bg_yellow="${__attr_beg}43${__attr_end}"
  953. __color_bg_blue="${__attr_beg}44${__attr_end}"
  954. __color_bg_magenta="${__attr_beg}45${__attr_end}"
  955. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  956. __color_bg_white="${__attr_beg}47${__attr_end}"
  957. __attr_underline="${__attr_beg}4${__attr_end}"
  958. __attr_reverse="${__attr_beg}7${__attr_end}"
  959. __attr_bold="${__attr_beg}1${__attr_end}"
  960. fi
  961. # NOTE: tput is another easy way to set colors and background
  962. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  963. # color blue.
  964. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  965. if test "$TERM" != dumb
  966. then
  967. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  968. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  969. # color for ::
  970. if test -z "$_HOSTCOLOR_1"
  971. then
  972. # default color used by tmux status line
  973. _HOSTCOLOR_1=green
  974. fi
  975. eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
  976. __my_c5="$__color_black$__color_bg_white" # color for SCR
  977. __my_cdef="$__color_default"
  978. fi
  979. if $inbash
  980. then
  981. __my_ps1_sh="[BASH:$BASH_VERSION]"
  982. elif $inzsh
  983. then
  984. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  985. fi
  986. __my_ps1_info1(){
  987. # first line of PS1
  988. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)"
  989. }
  990. test -n "$__MY_SCRIPT" && \
  991. __my_ps1_str_scr="SCR"
  992. test -n "$SSH_CONNECTION" && \
  993. __my_ps1_str_ssh="SSH"
  994. test -n "$__MY_DTACH" && \
  995. __my_ps1_str_dt="DT:`basename "$__MY_DTACH"`"
  996. __my_ps1_info2(){
  997. # second line of PS1
  998. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  999. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1000. | sed -e 's/ /|/g'
  1001. }
  1002. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1003. __my_ps1_save_pos="\[\033[s\]"
  1004. __my_ps1_restore_pos="\[\033[u\]"
  1005. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1006. __my_ps1_move_15left="\[\033[15D\]"
  1007. # collapse when command line is too long and try to write over this string
  1008. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1009. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1010. if $inzsh
  1011. then
  1012. PROMPT="\
  1013. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1014. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %# $SHLVL) "
  1015. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1016. elif $inbash
  1017. then
  1018. PS1="\
  1019. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1020. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$ $SHLVL) "
  1021. else
  1022. true
  1023. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1024. fi
  1025. ###################################
  1026. # set header and titles
  1027. __my_set_header_line(){
  1028. # save current position
  1029. printf "\033[s"
  1030. # move to 0,0
  1031. printf "\033[0;0H"
  1032. # clear curent to eol
  1033. printf "\033[K"
  1034. # inverse color
  1035. printf "\033[7m"
  1036. printf "$1"
  1037. # restore color
  1038. printf "\033[0m"
  1039. # restore saved position
  1040. printf "\033[u"
  1041. }
  1042. __my_set_screen_name(){
  1043. # set window name
  1044. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1045. then
  1046. echo -ne "\033k$1\033\\"
  1047. fi
  1048. }
  1049. __my_set_title(){
  1050. case $TERM in
  1051. (rxvt*|xterm*|aterm|screen*)
  1052. test -t 1 &&
  1053. test -z "$EMACS" &&
  1054. echo -n -e "\033]0;$1\007"
  1055. ;;
  1056. esac
  1057. }
  1058. if test -n "$TMUX"
  1059. then
  1060. # running tmux locally
  1061. __terminal_title="\$(basename \${PWD})"
  1062. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1063. then
  1064. # ssh connect from tmux terminal
  1065. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1066. else
  1067. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1068. fi
  1069. if $inzsh
  1070. then
  1071. precmd(){
  1072. laststatus=$?
  1073. eval __my_set_title ${__terminal_title}
  1074. }
  1075. else
  1076. if test -n "$PROMPT_COMMAND" && ! expr "$PROMPT_COMMAND" : '.*; *$' >/dev/null
  1077. then
  1078. PROMPT_COMMAND="${PROMPT_COMMAND};"
  1079. fi
  1080. if test -n "$PROMPT_COMMAND"
  1081. then
  1082. PROMPT_COMMAND="laststatus=\$?;${PROMPT_COMMAND}__my_set_title \"${__terminal_title}\";"
  1083. else
  1084. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";"
  1085. fi
  1086. fi
  1087. laststatus=0