|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- #!/bin/bash
-
- # TODO: use tput
-
- # If not running interactively, don't do anything
- [[ $- != *i* ]] && return
-
- ##########################################
- null(){
- "$@" >/dev/null 2>&1
- }
- __try_exec(){
- type $1 >/dev/null 2>&1 && "$@"
- }
-
- ##########################
- # system type
-
- alias ismsys=false
- alias iscygwin=false
- alias iswindows=false
- alias isdarwin=false
- alias islinux=false
- alias with_coreutils=false # for mac
-
- case `uname` in
- (MINGW*) alias ismsys=true ;;
- (CYGWIN*) alias iscygwin=true ;;
- (Darwin*) alias isdarwin=true ;;
- (Linux*) alias islinux=true ;;
- esac
- null ls --version && alias with_coreutils=true
-
- ( ismsys || iscygwin ) && alias iswindows=true
-
- alias inbash=false
- alias inzsh=false
-
- if test -n "$BASH_VERSION"
- then
- alias inbash=true
- elif test -n "$ZSH_VERSION"
- then
- alias inzsh=true
- fi
-
- #################################
-
- __match(){
- # __match str1 str2
- # return 0 if str2 is found in str1
- expr "$1" : ".*$2.*" >/dev/null
- }
-
- if ! __match "$PATH" "$HOME/.local/bin"
- then
- if test -d "$HOME/.local/bin"
- then
- PATH="${HOME}/.local/bin:${PATH}"
- fi
- if test -d "$HOME/.local/lib/gems/bin"
- then
- PATH="$HOME/.local/lib/gems/bin:${PATH}"
- fi
- fi
-
- if ismsys && ! __match "$PATH" /c/mingw/bin
- then
- PATH="$PATH:/c/mingw/bin:/c/mingw/msys/1.0/bin"
- fi
-
- # # it is not so good
- # # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
- # # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
- # without this ENV i cannot run tmux. another way is to use --disable-shared
- # when building tmux
- if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
- then
- export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
- fi
-
- ##################################
- export LANG=ja_JP.UTF-8
- export LC_MESSAGES=C
-
- export TERMCAP="${TERMCAP}:vb="
- ismsys && export HOSTNAME
- export ENV=~/.shrc
-
- if false iswindows
- then
- export PAGER='tr -d \\r | less'
- else
- export PAGER="less"
- fi
- export LESS="-iRMX"
-
- # Style for lesspipe is defined in esc.style
- _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
- for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
- do
- test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
- done
- test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
-
- if null type vim
- then
- export EDITOR=vim
- else
- export EDITOR=vi
- fi
- # export CDPATH=".:~"
- export VISUAL="$EDITOR"
-
- export GIT_PAGER="less -FS"
- export GIT_EDITOR="$EDITOR"
- export GIT_MERGE_AUTOEDIT=no
-
- if test -n "$TMUX" && \
- echo $TERM | grep screen >/dev/null 2>&1 && \
- tmux display -p '#{client_termname}' | grep 256color >/dev/null 2>&1
- then
- TERM=screen-256color
- fi
-
- if test -z "$TMP"
- then
- if test -n "$TMPDIR"
- then
- export TMP=$TMPDIR
- elif test -n "$TEMP"
- then
- export TMP="$TEMP"
- else
- export TMP=/tmp
- fi
- fi
- __match "$TMP" "${USER}-tmp" >/dev/null || export TMP="${TMP}/${USER}-tmp"
- export TEMP="$TMP"
- mkdir -p "$TMP"
-
- ! iswindows && null type stty && {
- stty stop undef # unbind C-s to stop displaying output
- # stty erase '^h'
- }
-
- if iswindows; then
- export USER=$USERNAME
- fi
-
- _tmux_prefs(){
- null type tmux || return 1
- tmux set -g mode-keys vi
- }
-
- if test -d ~/dbx
- then
- export CHIT_PATH="$HOME/dbx/.chit"
- fi
-
- if inzsh
- then
- # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
- autoload -U compinit; compinit
- unsetopt auto_menu
- setopt bash_auto_list
- bindkey -e
- fi
-
- #######################
-
- iswindows && alias tty="echo cmd.exe"
- type fortune >/dev/null 2>&1 && {
- fortune
- echo
- fortune -o
- echo
- }
- uname -a
- echo TERM $TERM $(tput colors) colors connected to $(tty), \
- running $BASH $BASH_VERSION
- if test -n "$TMUX"
- then
- tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
- 2>/dev/null
- echo
- fi
-
- ###################################
- # some aliases and functions
-
- ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
- _coloroption=" --color=auto"
- ( ! with_coreutils && isdarwin ) || iswindows || \
- _timeoption=" --time-style=long-iso"
- ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
-
- _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 ls="ls -hCF${_coloroption}${_timeoption}"
- if ! with_coreutils
- then
- export LSCOLORS=gxfxcxdxbxegedabagacad
- alias ls="ls -G"
- fi
- # export GREP_OPTIONS=""
- alias gr="grep -n --color=always"
- iswindows && alias grep="grep -n"
- # alias ll="ls -l"
- # alias la="ls -A"
- # alias lla="ls -Al"
- alias less="less -F"
- null type emacs && alias em="emacs -nw"
- null type vim && alias vi=vim
- alias pstree="LANG=C pstree"
- alias cp="cp -v"
- alias mv="mv -v"
- alias rm="rm -v"
- alias psaux="ps auxww"
- alias q=exit
- null type e3em && alias e3=e3em
- #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
- alias po=popd
- alias pu=pushd
- null type sudo && alias sudo="sudo " # use aliases through sudo
- null type sudoedit && alias sudoe="sudoedit"
- null type halt && alias halt="sudo halt"
- null type reboot && 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"
- null type file-roller && alias xunp="file-roller -h"
- null type paco && alias pc="sudo \paco -D"
- alias pycalc="python -i -c 'from math import *' "
- null type python3 && alias py3=python3
- null type python2 && alias py2=python2
- alias _reloadrc="exec \"$SHELL\""
- # alias mytime="date +%Y%m%d-%H%M%S"
- alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
- # type trash >/dev/null 2>&1 && alias rm=trash
- null type mpg123 && alias mpg123="mpg123 -C -v --title"
- null type xmms2 && alias xm="xmms2"
- #export PLAYER="mpg123 -C -v --title"
-
- null type screen && alias screen="screen -e^z^z"
- #alias zcd="cd \`zenity --file-selection --directory\`"
- null type gtags && alias gtags="gtags --verbose"
- null type htags && alias htags="htags --xhtml --symbol --line-number \
- --frame --alphabet --verbose"
- null type aunpack && alias au=aunpack
- null type lv && alias lv="lv|less"
- null type rsync && alias rs="rsync --progress --itemize-changes --compress"
-
- isdarwin && alias updatedb="LC_ALL=C updatedb"
- # do not use locate installed by macports
- isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
-
- # pad
- alias pad=notepad
- null type gedit && alias pad=gedit
- null type leafpad && alias pad=leafpad
- isdarwin && alias pad="open -e"
-
- null type wicd-curses && alias wic=wicd-curses
- null type wicd-cli && alias wil="wicd-cli -y -l | head"
- #alias wicn="wicd-cli -y -c -n"
- wicn(){
- if test $# -eq 0
- then
- local num
- wicd-cli -y -l | head
- echo -n "input num: "
- read num
- test -n "$num" && wicd-cli -y -c -n $num
- else
- wicd-cli -y -c -n $1
- 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
-
- alias pa=pacapt
-
- null type yaourt && alias yt=yaourt
- null type cower && alias cower="cower --color=auto"
- null type pacmatic && {
- alias pacman="pacmatic"
- export PACMAN="pacmatic"
- }
-
- _pacman_update_mirrorlist_with_reflector(){
- ml=/etc/pacman.d/mirrorlist
- cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
- if test -z "$cmd"
- then
- cmd="reflector --verbose -l 5 --sort rate --save $ml"
- fi
- echo "Running $cmd ..." 1>&2
- sudo $cmd
- }
- null type reflector && test -f /etc/pacman.d/mirrorlist && \
- alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
-
- null type apt-get && {
- alias aupgrade="sudo apt-get autoremove --yes && \
- sudo apt-get update --yes && sudo 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 port -v selfupdate && \
- { sudo port -v upgrade outdated; }"
- }
-
- if iscygwin; then
- null type windate || \
- alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
- # alias cygsu="cygstart /cygwinsetup.exe"
- # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
- fi
-
- g(){
- if test $# -eq 0 && null type git-info
- then
- git info
- else
- git -c color.ui=always "$@"
- fi
- }
- 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"
-
- null type gitmemo && alias m=gitmemo
-
- alias setup.py="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 yyyyMMdd-hhmmss
- if test -z "$1" || test "$1" == "-h"
- then
- echo "datestr: usage: datestr <yyyyMMddhhmmss>"
- return 1
- fi
-
- dfmt= # actual format for date command
- while test -n "$1"
- do
- fmt="$1"
- while test -n "$fmt"
- do
- case "$fmt" in
- yyyy*) # year
- dfmt="${dfmt}%Y"
- fmt="`echo "$fmt" | cut -c 5-`"
- ;;
- yy*) # last two digits of year
- dfmt="${dfmt}%y"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- MM*) # month (01..12)
- dfmt="${dfmt}%m"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- dd*) # day of month (01..12)
- dfmt="${dfmt}%d"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- HH* | hh*) # hour (00..23)
- dfmt="${dfmt}%H"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- mm*) # minute (00..59)
- dfmt="${dfmt}%M"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- ss*) # second (00..60)
- dfmt="${dfmt}%S"
- fmt="`echo "$fmt" | cut -c 3-`"
- ;;
- *)
- char=`echo "$fmt" | cut -c 1`
- dfmt="${dfmt}${char}"
- fmt="`echo "$fmt" | cut -c 2-`"
- ;;
- esac
- done
- shift
- done
- date +"$dfmt"
- }
-
- ssh(){
- __my_set_screen_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
- $EDITOR memo.txt
- else
- $EDITOR "$1/memo.txt"
- fi
- }
-
- clk(){
- local 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
- }
-
- 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") \
- man "$@"
- }
-
- netwait(){
- while ! ping -c 1 -t 1 example.com
- do
- true
- done
- echo network works.
- }
-
- cd(){
- if test $# -eq 0
- then
- pushd ~/ >/dev/null
- elif test "$1" = -
- then
- local pwd="$PWD"
- command cd "$OLDPWD"
- pushd -n "$pwd" >/dev/null # stack last dir
- elif ! test -d "$1"
- then
- echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
- return 1
- else
- pushd "$1" >/dev/null
- fi
- __dirs_rm_dup "$PWD"
- echo "$PWD"
- }
-
- __dirs_rm_dup(){
- for d in "$@"
- do
- local next="$(__realpath --strip "$d")"
- for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
- \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
- do
- popd +$l -n >/dev/null
- done
- done
- }
-
- __realpath(){
- if type realpath >/dev/null 2>&1
- then
- command realpath "$@"
- else
- while ! test -d $1
- do
- shift
- done
- local d="$OLDPWD"
- command cd "$1"
- echo "$PWD"
- command cd "$d"
- fi
- }
-
- dh(){
- if test $# -eq 0
- then
- dirs -v -l
- else
- local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
- cd "$dir"
- fi
- }
- input(){
- local foo
- stty -echo
- read foo
- stty echo
- echo $foo
- }
-
- # tmux(){
- # if test $# -eq 0
- # then
- # (cd ~; command tmux start;)
- # if command tmux has -t main
- # then
- # command tmux attach -t main
- # else
- # (cd ~; command tmux new -s main;)
- # fi
- # else
- # command tmux "$@"
- # fi
- # }
- tx(){
- if test $# -eq 0
- then
- tmux ls
- echo "tx <session> to attach."
- elif tmux has -t "$1"
- then
- tmux attach -t "$1"
- else
- tmux new -s "$1"
- fi
- }
-
- dt(){
- # dt [<name>] [<command ...>]
- __dtach_dir="${TMP}/dtach"
- install -d "${__dtach_dir}"
-
- if test -n "${__MY_DTACH}"
- then
- echo "Current session: ${__MY_DTACH}"
- fi
-
- if test -z "$1"
- then
- echo "Sessions:"
- ls "${__dtach_dir}"
- return 0
- elif test "$1" = "-h"
- then
- echo "dt: usage: dt <name> [<command ...>]" 1>&2
- return 1
- fi
-
- 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
- __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
- }
-
- scr(){
- test -n "$1" && pf="${1}-"
- local _tformat="%Y%m%d-%H%M%S%z"
- local _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
-
- local _cmdstr="`echo $@ | tr ' ' +`"
- local _tformat="%Y%m%d-%H%M%S%z"
- local _name="${pf}`date +${_tformat}`-${_cmdstr}"
-
- local _scr_file="${HOME}/${_name}.script"
-
- local _dt_dir="${TMP}/dtscr"
- install -d "$_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
- local srcs="$2"
- local dsts="$3"
- test -z "$dsts" && dsts="${srcs}.out"
- local pw
- echo -n "dgpg pw: "
- read -s pw
- echo ""
- test -z "$pw" && return 1
- for f in *${srcs}
- do
- local 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 表示可能"
-
- __try_exec diskinfo
-
- ! isdarwin && test -n "${DISPLAY}" && {
- __try_exec xrandr | \grep --color=never ^Screen
- }
-
- iswindows || __try_exec finger $USER
- LANG=C __try_exec id
- __try_exec xset q
- }
-
- x(){
- if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
- #mkdir -p ~/.my/log
- # nohup startx >~/.my/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 type colordiff >/dev/null 2>&1 && test $TERM != dumb
- then
- local diffcmd=colordiff
- else
- local diffcmd=diff
- fi
- ${diffcmd} -u "$@" | ${PAGER}
- }
-
- tb(){
- local datenum=`date +%Y%m%d-%H%M%S`
- local tb="$HOME/.my/tb/$datenum"
- mkdir -p "$tb"
- for file in "$@"
- do
- mv -t "$tb" "$file"
- done
- }
-
- 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 iswindows
- 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
- }
-
- winln(){
- # for windose make link (actually junction)
- if test $# -eq 0
- then
- {
- echo "usage: winln TARGET LINK_NAME"
- echo "Create a link to TARGET with the name LINK_NAME \
- (that is, TARGET must already exist)."
- echo "About other features run 'junction'."
- } 1>&2
- return 1
- else
- junction "$2" "$1"
- fi
- }
-
-
-
- #################################################
- ## 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
-
- __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() {
- local LANG=C
- local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
- local svn_repository_root=$(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
- local svn_branch=$(__my_parse_svn_branch)
- test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
- fi
- }
-
- __my_battery_status(){
- local 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
- local st=$(cat $dir/status)
- local full=$(cat $dir/charge_full)
- local now=$(cat $dir/charge_now)
- local rate=$(expr $now \* 100 / $full)
- printf "$1" "${st}:${rate}%"
- fi
- }
- alias bat='__my_battery_status %s\\n'
-
- ipaddress(){
- type ip >/dev/null 2>&1 || return 1
- local ip=$(LANG=C ip addr show scope global | \
- \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
- test -n "$ip" && printf $1 $ip
- }
-
- __my_ps1_str=""
-
- test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
-
- test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
-
- test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
-
- __my_ps1_scale(){
- if null type stty && ! ismsys
- then
- stty size | tr -d $'\n' | tr " " x
- printf " "
- fi
- }
-
- __my_ps1_tmux(){
- null type tmux || return $last
- local 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 \
- /opt/local/share/git-core/git-prompt.sh \
- /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
- do
- test -r "$f" && . "$f" && break
- done
- GIT_PS1_SHOWDIRTYSTATE=t
- 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:$(__try_exec git config --get user.name):%s]"
- }
-
- __my_ps1_ipaddr(){
- ! iswindows && ipaddress [Addr:%s]
- }
-
- __my_ps1_bttry(){
- local bst="${TMP}/batterystatus"
- if test -z "$DISPLAY" && ! iswindows
- then
- test -f $bst && local 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_dirs(){
- dirs | wc -l
- }
-
- __my_ps1_jobs(){
- jobs | wc -l
- }
-
- __my_alert_fail(){
- test $laststatus -eq 0 || echo '!!! '
- }
-
- if test "$TERM" != dumb
- then
- __my_c1="\[\e[0;33m\]" # color for PWD
- __my_c2="\[\e[0;36m\]" # color for user
- __my_c3="\[\e[1;30m\]" # color for OLDPWD
- if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
- elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
- elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
- else __my_c4="\[\e[1;32m\]" # color for ::
- fi
- __my_c5="\[\e[30;47m\]" # color for SCR
- __my_cdef="\[\e[0m\]"
- fi
-
- # export _LAST_STATUS=a
- # __my_export_last_status(){
- # local last=$?
- # echo $last
- # export _LAST_STATUS=$last
- # echo $_LAST_STATUS
- # return $last
- # }
-
- _ps1_bash="\
- ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
- ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$laststatus $(__my_ps1_scale)\D{%T} ${__my_ps1_str}\$(__my_alert_fail)\$ "
- inbash && PS1=$_ps1_bash
-
- __my_set_screen_title(){
- 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*)
- title="$(echo $@)"
- test -t 1 &&
- test -n "$DISPLAY" &&
- test -z "$EMACS" &&
- echo -n -e "\033]0;${title}\007"
- ;;
- esac
- }
- PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
- __my_set_screen_title \$(basename \"\$PWD\")/"
- PROMPT_COMMAND="laststatus=\$?;$PROMPT_COMMAND"
|