|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325 |
- #!/bin/sh
-
- # TODO: decide the naming scheme of variables (global, local, ...)
-
- expr "$-" : '^.*i' >/dev/null || return
-
- ##########################################
-
- __shrc_lock=/tmp/shrc_lock.$USER.$$
- test -f "$__shrc_lock" && return
- touch "$__shrc_lock"
-
-
- ##########################################
- null(){
- "$@" >/dev/null 2>&1
- }
- __safe_run(){
- type $1 >/dev/null 2>&1 && "$@"
- }
-
- __match(){
- # __match str word
- # return 0 if word is found in str
- expr "$1" : ".*$2.*" >/dev/null
- }
-
- __lf=$'\n'
- __cr=$'\r'
-
- test -n "$TMP" || export TMP=/tmp/${USER}-tmp
- mkdir -p "$TMP"
-
- __homerun="$HOME/.var/run"
- mkdir -p "$__homerun"
-
- ##########################
- # system type
-
- gnu_coreutils=false # for mac
- null ls --version && gnu_coreutils=true
-
- inbash=false
- inzsh=false
-
- if test -n "$BASH_VERSION"
- then
- inbash=true
- elif test -n "$ZSH_VERSION"
- then
- inzsh=true
- fi
-
- #################################
- # file pathes:
- # shrc: Path to this file
-
- if $inbash
- then
- __shrc="$BASH_SOURCE"
- elif $inzsh
- then
- __shrc="$0"
- fi
-
- #########################
- # system detection
- __system_shrc="$__homerun/system.shrc"
-
- if test -f "$__system_shrc"
- then
- . "$__system_shrc"
- else
- ismsys=false
- iscygwin=false
- iswindows=false
-
- isdarwin=false
- isfreebsd=false
- isbsd=false
-
- islinux=false
-
- # $OSTYPE is another choice. which is better?
- # NOTE: sh on FreeBSD does not define OSTYPE
- case `uname` in
- MINGW*) ismsys=true ;;
- CYGWIN*) iscygwin=true ;;
- Darwin*) isdarwin=true ;;
- FreeBSD*) isfreebsd=true ;;
- Linux*) islinux=true ;;
- esac
- ($ismsys || $iscygwin) && iswindows=true
- # is this true?
- ($isdarwin || $isfreebsd) && isbsd=true
-
- # dump system detection result
- cat <<__EOC__ >"$__system_shrc"
- #!/bin/sh
-
- # $__system_shrc
-
- ismsys=$ismsys
- iscygwin=$iscygwin
- iswindows=$iswindows
-
- isdarwin=$isdarwin
- isfreebsd=$isfreebsd
- isbsd=$isbsd
-
- islinux=$islinux
- __EOC__
- fi
-
- $ismsys && export HOSTNAME
-
- # adhoc fix for ansible on cygwin
- # http://blog.s-uni.net/2013/08/27/ansible-running-on-cygwin/
- if $iscygwin
- then
- export ANSIBLE_SSH_ARGS="-o ControlMaster=no"
- fi
-
- ##########################
- # Terminal setups
-
- if ! $iswindows && null type stty
- then
- stty stop undef # unbind C-s to stop displaying output
- # stty erase '^h'
- fi
-
- # Zsh specific preferences
- # http://www.clear-code.com/blog/2011/9/5.html
- if $inzsh
- then
- bindkey -e
-
- # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
- autoload compinit; compinit
- # supress cycle by tab
- unsetopt auto_menu
- # unsetopt correct
- setopt complete_aliases
- setopt auto_list
- setopt bash_auto_list
- setopt magic_equal_subst
- setopt list_types
- # what is the difference of these two?
- setopt auto_param_slash
- setopt mark_dirs
- zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
- zstyle ':completion:*' format '%B%d%b'
- zstyle ':completion:*' group-name ''
- zstyle ':completion:*' use-cache yes
- # zstyle ':completion:*:cd:*' tag-order local-directories
- zstyle ':completion:*' completer _complete _bash_completions \
- _history
- # zstyle ':completion:*:*:cd:*' completer
- zstyle ':completion:*' accept-exact-dirs true
- zstyle ':completion:*' special-dirs true
-
- autoload colors; colors
-
- # autoload -Uz promptinit
- # promptinit
- # prompt walters
-
- setopt hist_ignore_dups
- setopt hist_ignore_all_dups
- setopt hist_save_no_dups
- setopt extended_history
- setopt share_history
- setopt append_history
- HISTFILE=$HOME/.zsh-history
- HISTSIZE=100000
- SAVEHIST=100000
-
- setopt prompt_subst
-
- setopt interactive_comments
- fi
-
- ######################
- # Print welcome messages
-
- command -v fortune >/dev/null && {
- if command -v cowsay >/dev/null
- then
- fortune | cowsay
- echo
- fortune -o | cowsay
- echo
- else
- fortune
- echo
- fortune -o
- echo
- fi
- }
-
- uname -a
- $iswindows && alias tty="echo cmd.exe"
- echo TERM $TERM with $(tput colors) colors using $(tty)
- echo umask: `umask -S`
-
- echo
- __safe_run w yuk
-
- # if null type tmux
- # then
- # echo
- # echo tmux sessions:
- # tmux ls 2>/dev/null| sed -e 's/^/ /g'
- # echo
- # fi
-
- # if test -n "$TMUX"
- # then
- # tmux display -p \
- # 'TMUX #S:#I:#W.#P, client TERM: #{client_termname}' \
- # 2>/dev/null
- # echo
- # fi
-
- if test -n "$__MY_DTACH"
- then
- echo
- echo DSession: Current Session: "`basename "$__MY_DTACH"`"
- echo DSession: Press C-^ to Exit.
- true
- fi
-
- i(){
- w
- ss -lnp
- ps aufx
- df -h
- top -b -d 1 -n 1
- top -b -d 1 -n 1 -a
- dstat -taf 1 10
- }
-
- ###################################
- # some aliases and functions
-
- # __func_name: never used interactively
- # _func_name: usually not used interactively
-
- __safe_alias(){
- # __safe_alias <name>=<command>
- _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
- test -n "$_bin" && \
- null type $_bin && \
- alias "$1"
- }
-
- $gnu_coreutils && _timeoption=" --time-style=long-iso"
- # color prefs
- if $gnu_coreutils
- then
- # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
- # gnu coreutils LS_COLORS is used
- null type dircolors && eval `dircolors`
- _coloroption=" --color=auto"
- else
- # export LSCOLORS=gxfxcxdxbxegedabagacad
- export LSCOLORS=DxGxcxdxCxegedabagacad
- export CLICOLOR=1
- fi
- alias ls="ls -hCF${_coloroption}${_timeoption}"
-
- _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
- _timeformat_rfc2822="%a, %d %b %Y %T %z"
- _timeformat_num="%Y%m%d%H%M%S"
- alias datenum="date +$_timeformat_num"
-
- # export GREP_OPTIONS=""
- alias gr="grep -n --color=always"
- $iswindows && alias gr="grep -n"
- alias less="less -F"
- __safe_alias em="emacs -nw"
- test -n "$_VI_ALT" && alias vi=$_VI_ALT
- alias pstree="LANG=C pstree"
- alias cp="cp -v"
- alias mv="mv -v"
- alias rm="rm -v"
- alias mkdir="mkdir -v"
- $gnu_coreutils && alias rmdir="rmdir -v"
- alias psaux="ps auxww"
- alias modest="ionice -c 2 -n 7 nice -n 19"
- alias e=exec
- alias q=exit
- __safe_alias e3=e3em
- #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
- alias po=popd
- alias pu=pushd
- __safe_alias sudo="sudo " # use aliases through sudo
- __safe_alias sudoe="sudoedit"
- # __safe_alias halt="sudo halt"
- # __safe_alias reboot="sudo reboot"
- null type dbus-send && {
- alias suspend="dbus-send --system --print-reply \
- --dest=org.freedesktop.UPower \
- /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
- alias hibernate="dbus-send --system --print-reply \
- --dest=org.freedesktop.UPower \
- /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
- }
- alias rand="echo \$RANDOM"
- __safe_alias xunp="file-roller -h"
- __safe_alias pc="sudo \paco -D"
- alias pycalc="python -i -c 'from math import *' "
- __safe_alias py3=python3
- __safe_alias py2=python2
- __safe_alias ipy=ipython
- __safe_alias ipy3=ipython3
- __safe_alias ipy2=ipython2
- __safe_alias javac="javac -J-Duser.language=en"
- __safe_alias rl=rlwrap
- __safe_alias igosh=gosh-rl
- # Sometimes SHELL cannot be used. For example, when running bash inside zsh
- # SHELL is set to be /bin/zsh
- if $inbash
- then
- alias _reloadrc="rm '$__shrc_lock'; exec bash"
- elif $inzsh
- then
- alias _reloadrc="rm '$__shrc_lock'; exec zsh"
- else
- alias _reloadrc="rm '$__shrc_lock'; exec $SHELL"
- fi
- # alias mytime="date +%Y%m%d-%H%M%S"
- alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
-
- # some extra utilities
- # type trash >/dev/null 2>&1 && alias rm=trash
- __safe_alias mpg123="mpg123 -C -v --title"
- __safe_alias xm="xmms2"
- #export PLAYER="mpg123 -C -v --title"
-
- __safe_alias screen="screen -e^z^z"
- #alias zcd="cd \`zenity --file-selection --directory\`"
- __safe_alias gtags="gtags --verbose"
- __safe_alias htags="htags --xhtml --symbol --line-number \
- --frame --alphabet --verbose"
- __safe_alias au=aunpack
- # __safe_alias lv="lv|less"
- __safe_alias rs="rsync --progress --itemize-changes --compress"
- __safe_alias vagr=vagrant
-
- __safe_alias ztw='twitter set "`zenity --entry --title ztw --text Status:`"'
-
- if $iscygwin
- then
- __my_wget_options=" --no-check-certificate"
- __safe_alias wget="wget $__my_wget_options"
- fi
-
- if $isdarwin
- then
- alias updatedb="LC_ALL=C updatedb"
- # do not use locate installed by macports
- test -x /usr/bin/locate && alias locate="/usr/bin/locate"
- fi
-
- # somehow not work
- # typeset -ga chpwd_functions
- # chpwd_functions+=pwd
-
- # chpwd(){
- # pwd
- # }
- cd(){
- builtin cd "$@" && pwd
- }
-
- root(){
- if test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = true
- then
- cd "`git rev-parse --show-toplevel`"
- else
- cd /
- fi
- }
-
- # pad
- alias pad=notepad
- __safe_alias pad=gedit
- __safe_alias pad=leafpad
- $isdarwin && alias pad="open -e"
-
- __find_latest_vimdir(){
- vimdir=/usr/share/vim
- if test -d "$vimdir"
- then
- find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
- else
- echo ""
- fi
- }
-
- for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
- do
- test -n "$f" || continue
- f="$f/macros/less.sh"
- test -f $f && alias vl=$f && break
- done
-
- __safe_alias pa="pacman --color=always"
- __safe_alias pa="pacmatic --color=always"
- __safe_alias pa="pacapt"
-
- __safe_alias yt=yaourt
- # variable for yaourt
- null type pacmatic && export PACMAN="pacmatic"
-
- __safe_alias cower="cower --color=auto"
-
- __my_pacman_update_mirrorlist_with_reflector(){
- f=/etc/pacman.d/mirrorlist
- cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
- if test -z "$cmd"
- then
- cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
- fi
- echo ">>> $cmd ..." 1>&2
- sudo sh -c "$cmd" && cat $f
- }
- null type reflector && test -d /etc/pacman.d && \
- alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
-
- null type apt-get && {
- alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
- apt-get update --yes && apt-get upgrade --yes'"
- alias aptin="apt-get install"
- alias aptsearch="apt-cache search"
- alias aptshow="apt-cache show"
- }
-
- null type port && {
- alias port="port -v"
- alias pupgrade="sudo sh -xc 'port -v selfupdate && \
- port -v upgrade outdated'"
- }
-
- if $iscygwin
- then
- null type windate || \
- alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
- # alias cygsu="cygstart /cygwinsetup.exe"
- fi
-
- mkpatch(){
- if test $# -eq 0
- then
- echo "usage: mkpatch <olddir> <newdir>"
- elif ! test -d "$1"
- then
- echo "mkpatch: $1 is not a directory"
- elif ! test -d "$2"
- then
- echo "mkpatch: $2 is not a directory"
- else
- diff -Naur "$1" "$2"
- fi
- }
-
- __sdcv_less(){
- command sdcv -n "$@" | less --quit-if-one-screen
- }
-
- command -v sdcv >/dev/null && alias dict=__sdcv_less
-
- g(){
- if test $# -eq 0 && null type git-info
- then
- git info
- else
- git "$@"
- fi
- }
-
- $isdarwin && . /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash
- if null type _git && $inbash
- then
- # enable programmable completion for g
- complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
- || complete -o default -o nospace -F _git g
- fi
- #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
-
- __safe_alias m=gitmemo
-
- alias setup.py3="sudo python3 setup.py install --record files.txt"
-
- randomstr(){
- len=$1
- test -z "$len" && len=8
- uuidgen | tr -d - | cut -c 1-len
- }
-
- datestr(){
- # datestr YYYYMMDDhhmmss
- if test -z "$1" || test "$1" = "-h"
- then
- echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
- return 1
- fi
-
- # actual format for date command
- dfmt="`echo "$1" | \
- sed -e 's/YYYY/%Y/g' -e 's/yyyy/%Y/g' -e 's/YY/%y/g' -e 's/yy/%y/g' |\
- sed -e 's/MM/%m/g' -e 's/DD/%d/g' -e 's/dd/%d/g' | \
- 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'`"
- date +"$dfmt"
- }
-
- # ssh(){
- # # __my_terminal_title ssh
- # command ssh "$@"
- # }
-
- __ssh_with_cd(){
- # __ssh_with_cd <host> <directory> [<arg> ...]
- if test -z "$2"
- then
- echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
- return 1
- fi
- host="$1"
- shift
- dir="$1"
- shift
- ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
- }
-
- memo(){
- if test -z "$1"
- then
- _memo="memo.txt"
- else
- _memo="$1/memo.txt"
- fi
- $EDITOR "$_memo"
- if test -z "`cat "$_memo"`"
- then
- echo "$_memo is empty. Removing."
- rm "$_memo"
- fi
- }
-
- now(){
- ___tformat="%Y/%m/%d %H:%M:%S %z"
- cal
- REPLY=
- printf "\\r`date "+${___tformat}"`"
- read -t 1
- while test $? -ne 0
- do
- printf "\\r`date "+${___tformat}"`"
- read -t 1
- done
- }
-
- s(){
- if git rev-parse --git-dir >/dev/null 2>&1
- then
- echo ">> git grep -n $@" 1>&2
- git grep -n "$@"
- elif which ag >/dev/null 2>&1
- then
- echo ">> ag --pager=\"$PAGER\" $@" 1>&2
- ag --pager="$PAGER" "$@"
- elif which ack >/dev/null 2>&1
- then
- echo ">> ack --pager=\"$PAGER\" $@" 1>&2
- ack --pager="$PAGER" "$@"
- else
- echo \
- ">> find . " \
- "-path '*/.git' -prune -o" \
- "-path '*/.svn' -prune -o" \
- "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
- if test $# -eq 0
- then
- echo "No search word given." 1>&2
- return 1
- fi
- find . \
- -path '*/.git' -prune -o \
- -path '*/.svn' -prune -o \
- -type -f -exec grep -nH -e --color=always "$@" {} + \
- | $PAGER
- fi
- }
-
- MYMANPATH='/usr/lib/erlang/man'
- if $inbash || $inzsh
- then
- man(){
- env \
- LESS_TERMCAP_mb=$(printf "\e[1;35m") \
- LESS_TERMCAP_md=$(printf "\e[1;31m") \
- LESS_TERMCAP_me=$(printf "\e[0m") \
- LESS_TERMCAP_se=$(printf "\e[0m") \
- LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
- LESS_TERMCAP_ue=$(printf "\e[0m") \
- LESS_TERMCAP_us=$(printf "\e[1;32m") \
- MANPATH="`manpath`:$MYMANPATH" \
- man "$@"
- }
- fi
-
- netwait(){
- while ! ping -c 1 -t 1 example.com
- do
- true
- done
- echo network works.
- }
-
- __realpath(){
- if type realpath >/dev/null 2>&1
- then
- command realpath "$@"
- else
- while ! test -d $1
- do
- shift
- done
- (command cd "$d" && echo "$PWD")
- # local d="$OLDPWD"
- # command cd "$1"
- # echo "$PWD"
- # command cd "$d"
- fi
- }
-
- if $isdarwin
- then
- # I want to use env -u, but darwin env does not provide this option
- null type tmux && alias tmux='SHLVL= tmux'
- else
- null type tmux && alias tmux='env -u SHLVL tmux'
- fi
- tx(){
- tmux start-server
- if test -z "$1"
- then
- echo "usage: tx [<session_name>]"
- tmux list-sessions
- elif test -z "$TMUX"
- then
- # -A: attach if already exists
- # tmux new-session -A -c "$HOME" -s "$1"
- tmux new-session -A -s "$1"
- else
- # create new session if not exists yet
- # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
- tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
- tmux switch-client -t "$1"
- fi
- }
-
- __dsession(){
- # dt [-h] [<name>] [<command ...>]
- __dtach_dir="${TMP}/dtach"
- mkdir -p "${__dtach_dir}"
-
- if test -n "${__MY_DTACH}"
- then
- # if already in dtach session print current session
- soc_name="`basename "$__MY_DTACH"`"
- echo "Current session: ${soc_name}"
- fi
-
- if test -z "$1"
- then
- # if no arg given show list of sessions
- echo "Usage: ds [<session>]"
- echo "Sessions:"
- ls "${__dtach_dir}"
- return 0
- elif test "$1" = "-h"
- then
- echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
- return 1
- fi
-
- # set socket name
- soc_name="${__dtach_dir}/$1"
- shift
-
- if test -n "$__MY_DTACH"
- then
- echo "dtach session cannot be nested." 1>&2
- return 1
- elif test -S "$soc_name"
- then
- dtach -a "$soc_name" -e ^^
- elif test -e "$soc_name"
- then
- echo "dt: File named $soc_name already exists."
- return 1
- elif test -z "$1"
- then
- # if no command given invoke current shell
- __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
- # echo "dt: Socket named $soc_name not exists and no command specified."
- # return 1
- else
- __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
- fi
- }
- null type dtach && alias ds=__dsession
-
- scr(){
- test -n "$1" && pf="${1}-"
- ___tformat="%Y%m%d-%H%M%S%z"
- ___file="${HOME}/${pf}`date +${___tformat}`.script"
- __MY_SCRIPT=${___file} script ${___file} "$@"
- }
-
- dtscr(){
- # dtscr <command ...>
- if test -z "$1"
- then
- echo "dtscr: usage: dtscr <command ...>"
- return 1
- fi
-
- ___cmdstr="`echo $@ | tr ' ' +`"
- ___tformat="%Y%m%d-%H%M%S%z"
- ___name="${pf}`date +${___tformat}`-${___cmdstr}"
-
- ___scr_file="${HOME}/${___name}.script"
-
- ___dt_dir="${TMP}/dtscr"
- mkdir -p "$___dt_dir"
-
- dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
- # echo $_name
- # echo $_file
- }
-
- mcrypt_stream(){
- test $# -eq 2 || return 1
- case $1 in
- en)
- mcrypt --key $2 | base64 ;;
- de)
- base64 -d | mcrypt -d --key $2 ;;
- esac
- }
-
- gpg_stream(){
- test $# -eq 2 || return 1
- case $1 in
- en)
- gpg --passphrase $2 -c --batch |base64 ;;
- de)
- base64 -d|gpg --passphrase $2 -d --batch ;;
- esac
- }
- dgpg(){
- if test "$1" = help || test -z "$2"
- then
- echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
- return
- fi
- ___srcs="$2"
- ___dsts="$3"
- test -z "$___dsts" && ___dsts="${___srcs}.out"
- ___pw=
- echo -n "dgpg pw: "
- read -s ___pw
- echo ""
- test -z "$___pw" && return 1
- for f in *${___srcs}
- do
- ___d="$(basename "$f" "${___srcs}")${___dsts}"
- echo -n "Processing $f to $___d..."
- if test -d "$f"
- then
- echo "`printf 'failed (%s is directory)' $f`"
- elif test -f "$___d"
- then
- echo "`printf 'failed (%s is already exists)' $___d`"
- elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
- then
- echo "done"
- else
- echo "failed"
- test -f "$___d" && rm "$___d"
- fi
- done
- }
-
- alias enst="gpg_stream en"
- alias dest="gpg_stream de"
-
- showinfo(){
- echo "Japanese letters are 表示可能"
-
- __safe_run diskinfo
-
- ! $isdarwin && test -n "${DISPLAY}" && {
- __safe_run xrandr | \grep --color=never ^Screen
- }
-
- $iswindows || __safe_run finger $USER
- LANG=C __safe_runc id
- __safe_run xset q
- }
-
- x(){
- if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
- #mkdir -p ~/.var/log
- # nohup startx >~/.var/log/xorg.log 2>&1 &
- # exit
- exec startx
- else
- echo "X cant be started! Another X is already running?" 1>&2
- fi
- }
-
- bak(){
- for file in "$@"
- do
- cp -v ${file} ${file}.bak
- done
- }
-
- di(){
- if null type git
- then
- git --git-dir= diff "$@"
- ___diffcmd="git --git-dir= diff"
- else
- if type colordiff >/dev/null 2>&1 && test $TERM != dumb
- then
- ___diffcmd=colordiff
- else
- ___diffcmd=diff
- fi
- ${___diffcmd} -u "$@" | ${PAGER}
- fi
- }
-
- tb(){
- __datenum=`date +%Y%m%d-%H%M%S`
- __tb="$HOME/.var/tb/$__datenum"
- mkdir -p "$__tb"
- mv "$@" "$__tb"
- }
-
- mkdd(){
- _d=`date +%Y%m%d-%H%M%S` && \
- mkdir -p "$_d"
- }
-
- mkcd(){
- if test -z "$1"
- then
- echo "mkcd: usage: mkcd <dir>"
- return 1
- elif test -d "$1"
- then
- echo "Dir \"$1\" already exists."
- else
- mkdir -p "$1"
- echo "Dir \"$1\" created."
- fi
- cd "$1"
- }
-
- mkcdd(){
- # make and change date directory
- _d=`date +%Y%m%d-%H%M%S` && \
- mkcd "$_d"
- }
-
- if test -n "$TMUX" && null type reattach-to-user-namespace
- then
- alias pbpaste="reattach-to-user-namespace pbpaste"
- alias pbcopy="reattach-to-user-namespace pbcopy"
- fi
-
- catclip(){
- if $iswindows
- then
- cat /dev/clipboard | tr -d \\r
- elif $isdarwin
- then
- pbpaste
- else
- xclip -o -selection "clipboard"
- fi
- }
-
- setclip(){
- if test $# -eq 0
- then
- exec 3<&0
- else
- exec 3<<__EOF__
- `cat "$@"`
- __EOF__
- fi
- if $iswindows
- then
- 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
- elif $isdarwin
- then
- pbcopy 0<&3
- else
- 0<&3 xclip -i -f -selection "primary" | \
- xclip -i -f -selection "clipboard"
- fi
- exec 3<&-
- }
-
- open_file(){
- if $iscygwin
- then
- cygstart "$@"
- elif $ismsys
- then
- cmd.exe //c start "" "$@"
- elif $isdarwin
- then
- touch "$@"
- open "$@"
- elif $islinux
- then
- touch "$@"
- if null type pcmanfm; then
- LC_MESSAGES= pcmanfm "$@"
- else
- LC_MESSAGES= xdg-open "$@" &
- fi
- else
- cat "$@"
- fi
- }
-
- o(){
- if test $# -eq 0
- then
- open_file .
- else
- for f in "$@"
- do
- open_file "$(realpath "$f")"
- done
- fi
- }
-
- convmv_sjis2utf8_test(){
- convmv -r -f sjis -t utf8 *
- }
-
- convmv_sjis2utf8_notest(){
- convmv -r -f sjis -t utf8 * --notest
- }
-
-
- #################################################
- ## pastebin services
- ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
-
- sprunge(){
- # http://sprunge.us
- if test -z "$1"
- then
- curl -F 'sprunge=<-' http://sprunge.us
- else
- curl http://sprunge.us/$1
- fi
- }
-
- dpaste(){
- # http://dpaste.de
- if test -z "$1"
- then
- curl -F 'content=<-' https://dpaste.de/api/
- echo
- else
- curl https://dpaste.de/$1/raw/
- fi
- }
-
-
- ######################################
- ## Prompt Settings
-
- __my_moc_state(){
- type mocp >/dev/null 2>&1 || return
- test "`mocp -Q %state 2>/dev/null`" = PLAY || return
- printf "$1" "`mocp -Q %title 2>/dev/null`"
- }
-
- __my_parse_svn_branch() {
- ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
- ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
- sed -ne 's#^Repository Root: ##p')
- echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
- awk '{print $1}'
- }
-
- __my_svn_ps1(){
- if svn status >/dev/null 2>&1
- then
- ___svn_branch=$(__my_parse_svn_branch)
- test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
- fi
- }
-
- __my_battery_status(){
- ___dir=/sys/class/power_supply/BAT0
- if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
- test -r $___dir/charge_now
- then
- ___st=$(cat $___dir/status)
- ___full=$(cat $___dir/charge_full)
- ___now=$(cat $___dir/charge_now)
- ___rate=$(expr "$___now" \* 100 / "$___full")
- printf "$1" "${___st}:${___rate}%"
- fi
- }
- alias bat='__my_battery_status %s\\n'
-
- __my_ps1_scale(){
- if null type stty && ! $ismsys
- then
- echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
- fi
- }
-
- __my_ps1_tmux(){
- null type tmux || return $last
- ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
- test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
- }
-
- __my_ps1_moc(){
- __my_moc_state "[MOC:%s]"
- }
-
- for f in /usr/share/git/git-prompt.sh \
- /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-prompt.sh \
- /usr/local/opt/bash-git-prompt/share/gitprompt.sh \
- "`brew --prefix 2>/dev/null`"/etc/bash_completion.d/git-prompt.sh \
- /usr/share/git-core/contrib/completion/git-prompt.sh \
- /usr/local/share/git-core/contrib/completion/git-prompt.sh \
- /etc/bash_completion.d/git-prompt \
- /opt/local/share/git-core/git-prompt.sh \
- /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
- do
- test -r "$f" && ($inbash || $inzsh) && . "$f" && break
- done
- export GIT_PS1_SHOWDIRTYSTATE=t
- export GIT_PS1_SHOWUPSTREAM=t
- __my_ps1_git(){
- null type __git_ps1 || return $last
- null git rev-parse --git-dir >/dev/null 2>&1 || return $last
- __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
- }
-
- __printf_ipaddr(){
- # ipaddress <fmt>
- type ip >/dev/null 2>&1 || return 1
- ___ip=$(LANG=C ip addr show scope global | \
- \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
- test -n "$___ip" && printf "$1" $___ip
- }
-
- alias addr="__printf_ipaddr '%s'"
-
- __my_ps1_ipaddr(){
- ! $iswindows && __printf_ipaddr '[Addr:%s]'
- }
-
- __my_ps1_bttry(){
- ___bst="$__homerun/batterystatus"
- if test -z "$DISPLAY" && ! $iswindows
- then
- test -f $___bst && ___bstr="$(cat $___bst)"
- test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
- echo "[Battery:${___bstr}]"
- __my_battery_status %s >$___bst &
- fi
- }
-
- __my_ps1_memo(){
- test -f memo.txt && echo "m:`du -b memo.txt|cut -f 1`"
- }
-
- __my_ps1_dirs(){
- dirs | wc -l
- }
-
- __my_ps1_jobs(){
- # __my_ps1_jobs [<num>]
- if test -n "$1"
- then
- jobs="$1"
- else
- jobs="`jobs | wc -l`"
- fi
- if test "$jobs" -gt 0
- then
- echo "JOBS:$jobs"
- fi
- }
-
- __my_ps1_dollar(){
- if test -z "$SHLVL"
- then
- printf "$1"
- else
- perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' $SHLVL "$1"
- fi
- }
-
- __my_alert_fail(){
- test $laststatus -eq 0 || \
- echo "STATUS:${laststatus}"
- }
-
- # About ansi escape sequences
- # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
- # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
-
-
- if $inbash || $inzsh
- then
- if $inzsh
- then
- __attr_beg=$'%{\033['
- __attr_end='m%}'
- else
- __attr_beg='\[\033['
- __attr_end='m\]'
- fi
-
- __color_default="${__attr_beg}0${__attr_end}"
-
- __color_black="${__attr_beg}0;30${__attr_end}"
- __color_red="${__attr_beg}0;31${__attr_end}"
- __color_green="${__attr_beg}0;32${__attr_end}"
- __color_yellow="${__attr_beg}0;33${__attr_end}"
- __color_blue="${__attr_beg}0;34${__attr_end}"
- __color_magenta="${__attr_beg}0;35${__attr_end}"
- __color_cyan="${__attr_beg}0;36${__attr_end}"
- __color_white="${__attr_beg}0;37${__attr_end}"
-
- __color_light_black="${__attr_beg}1;30${__attr_end}"
- __color_light_red="${__attr_beg}1;31${__attr_end}"
- __color_light_green="${__attr_beg}1;32${__attr_end}"
- __color_light_yellow="${__attr_beg}1;33${__attr_end}"
- __color_light_blue="${__attr_beg}1;34${__attr_end}"
- __color_light_magenta="${__attr_beg}1;35${__attr_end}"
- __color_light_cyan="${__attr_beg}1;36${__attr_end}"
- __color_light_white="${__attr_beg}1;37${__attr_end}"
-
- __color_bg_black="${__attr_beg}40${__attr_end}"
- __color_bg_red="${__attr_beg}41${__attr_end}"
- __color_bg_green="${__attr_beg}42${__attr_end}"
- __color_bg_yellow="${__attr_beg}43${__attr_end}"
- __color_bg_blue="${__attr_beg}44${__attr_end}"
- __color_bg_magenta="${__attr_beg}45${__attr_end}"
- __color_bg_cyan="${__attr_beg}46${__attr_end}"
- __color_bg_white="${__attr_beg}47${__attr_end}"
-
- __attr_underline="${__attr_beg}4${__attr_end}"
- __attr_reverse="${__attr_beg}7${__attr_end}"
- __attr_bold="${__attr_beg}1${__attr_end}"
- fi
-
- # NOTE: tput is another easy way to set colors and background
- # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
- # color blue.
- # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
-
- if test "$TERM" != dumb
- then
- __my_c1="$__attr_bold$__attr_underline" # color for PWD
- __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
- # color for ::
- if test -z "$_HOSTCOLOR_1"
- then
- # default color used by tmux status line
- _HOSTCOLOR_1=green
- fi
- eval "__my_c4=\${__color_light_${_HOSTCOLOR_1}}"
- __my_c5="$__color_black$__color_bg_white" # color for SCR
- __my_cdef="$__color_default"
- fi
-
- if $inbash
- then
- __my_ps1_sh="[BASH:$BASH_VERSION]"
- elif $inzsh
- then
- __my_ps1_sh="[ZSH:$ZSH_VERSION]"
- fi
-
- __my_ps1_info1(){
- # first line of PS1
- echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
- }
-
- test -n "$__MY_SCRIPT" && \
- __my_ps1_str_scr="SCR"
-
- test -n "$SSH_CONNECTION" && \
- __my_ps1_str_ssh="SSH"
-
- test -n "$__MY_DTACH" && \
- __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
-
- __my_ps1_info2(){
- # second line of PS1
- echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
- ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
- | sed -e 's/ /|/g'
- }
-
- __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
-
- __my_ps1_save_pos="\[\033[s\]"
- __my_ps1_restore_pos="\[\033[u\]"
- __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
- __my_ps1_move_15left="\[\033[15D\]"
- # collapse when command line is too long and try to write over this string
- # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
- # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
-
- if $inzsh
- then
- PROMPT="\
- ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
- ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %#) "
- RPROMPT="%D{%Y/%m/%d %H:%M}"
- elif $inbash
- then
- PS1="\
- ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
- ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$) "
- else
- true
- # PS1="$(printf $(whoami)@$(hostname)$ )"
- fi
-
- ###################################
- # set header and titles
-
- __my_set_header_line(){
- # save current position
- printf "\033[s"
- # move to 0,0
- printf "\033[0;0H"
- # clear curent to eol
- printf "\033[K"
- # inverse color
- printf "\033[7m"
- printf "$1"
- # restore color
- printf "\033[0m"
- # restore saved position
- printf "\033[u"
- }
-
- __my_set_screen_name(){
- # set window name
- if test -n "$TMUX" && test -z "$INSIDE_EMACS"
- then
- echo -ne "\033k$1\033\\"
- fi
- }
-
- __my_set_title(){
- case $TERM in
- (rxvt*|xterm*|aterm|screen*)
- test -t 1 &&
- test -z "$EMACS" &&
- echo -n -e "\033]0;$1\007"
- ;;
- esac
- }
-
- if test -n "$TMUX"
- then
- # running tmux locally
- __terminal_title="\$(basename \${PWD})"
- elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
- then
- # ssh connect from tmux terminal
- __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
- else
- __terminal_title="`whoami`@`hostname`:\${PWD}"
- fi
-
- if $inzsh
- then
- precmd(){
- laststatus=$?
- eval __my_set_title ${__terminal_title}
- }
- else
- if test -n "$PROMPT_COMMAND" && ! expr "$PROMPT_COMMAND" : '.*; *$' >/dev/null
- then
- PROMPT_COMMAND="${PROMPT_COMMAND};"
- fi
-
- if test -n "$PROMPT_COMMAND"
- then
- PROMPT_COMMAND="laststatus=\$?;${PROMPT_COMMAND}__my_set_title \"${__terminal_title}\";"
- else
- PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";"
- fi
- fi
- laststatus=0
|