|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251 |
- #!/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'
-
- # For normal cases TMP is set in ./profile
- 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
-
- if $inbash
- then
- # https://unix.stackexchange.com/questions/1288/preserve-bash-history-in-multiple-terminal-windows
- # Avoid duplicates
- HISTCONTROL=ignoredups:erasedups
- # When the shell exits, append to the history file instead of overwriting it
- shopt -s histappend
- 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
-
- if command -v lolcat >/dev/null
- then
- alias __safe_lolcat=lolcat
- else
- alias __safe_lolcat=cat
- fi
- if command -v cowsay >/dev/null
- then
- alias __safe_cowsay=cowsay
- else
- alias __safe_cowsay=cat
- fi
-
- command -v fortune >/dev/null && {
- fortune | __safe_cowsay | __safe_lolcat
- echo
- fortune -o | __safe_cowsay | __safe_lolcat
- echo
- }
-
- uname -a
- $iswindows && alias tty="echo cmd.exe"
- echo TERM $TERM with $(tput colors) colors using $(tty)
- echo umask: `umask -S`
-
- # if null type figlet && null type lolcat
- # then
- # echo `whoami`@`hostname` | figlet | lolcat
- # fi
- # echo
-
- if command -v neofetch >/dev/null
- then
- neofetch
- elif command -v screenfetch >/dev/null
- then
- screenfetch
- fi
-
- __safe_run id `whoami`
- # echo
- ## A bit heavy command
- # finger `whoami`
- echo
- __safe_run w `whoami`
-
- # 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"
-
- alias less="less -FX"
- __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
- __safe_alias e3=e3em
- #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
- alias po=popd
- alias pu=pushd
- __safe_alias sudoe="sudoedit"
-
- __safe_alias ge="git exec"
- __safe_alias make="nice make"
- __safe_alias brew="nice brew"
- __safe_alias gcc="nice gcc"
- __safe_alias go="nice go"
- __safe_alias git="nice git"
- __safe_alias javac="nice javac -J-Duser.language=en"
- __safe_alias ansible-playbook="nice ansible-playbook"
-
- alias rand="echo \$RANDOM"
- 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 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
- __safe_alias mpg123="mpg123 -C -v --title"
- #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
- }
-
- __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
-
- __safe_alias g="git "
-
- 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
- }
-
- scr(){
- test -n "$1" && pf="${1}-"
- ___tformat="%Y%m%d-%H%M%S%z"
- ___file="${PWD}/${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_run 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
- }
-
- __trash_func(){
- __datenum=`date +%Y%m%d-%H%M%S`
- __tb="$HOME/.var/tb/$__datenum"
- mkdir -p "$__tb"
- mv "$@" "$__tb"
- }
- __safe_alias tb=__trash_func
- __safe_alias tb=trash
-
-
- 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"
- }
-
- __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<&-
- }
-
- # Now Homebrew 10sr/pkg/bin provides these commands:
- # Fallback to aliases if commands are not installed yet
- which catclip >/dev/null || alias catclip=__catclip
- which setclip >/dev/null || alias setclip=__setclip
-
- 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
- }
-
- __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
- if test -r "$f" && ($inbash || $inzsh)
- then
- . "$f"
- break
- fi
- 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:$(echo $(wc -c memo.txt) | cut -f 1 -d " ")"
- }
-
- __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
|