25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

13 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
12 년 전
11 년 전
11 년 전
11 년 전
12 년 전
12 년 전
11 년 전
13 년 전
10 년 전
10 년 전
10 년 전
12 년 전
12 년 전
12 년 전
12 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
12 년 전
10 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
11 년 전
11 년 전
11 년 전
12 년 전
12 년 전
10 년 전
12 년 전
12 년 전
12 년 전
10 년 전
12 년 전
12 년 전
12 년 전
12 년 전
11 년 전
12 년 전
11 년 전
12 년 전
11 년 전
10 년 전
12 년 전
12 년 전
13 년 전
12 년 전
12 년 전
11 년 전
11 년 전
12 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
12 년 전
12 년 전
12 년 전
12 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
13 년 전
12 년 전
13 년 전
12 년 전
12 년 전
12 년 전
13 년 전
12 년 전
13 년 전
12 년 전
13 년 전
12 년 전
13 년 전
12 년 전
11 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
12 년 전
11 년 전
11 년 전
12 년 전
11 년 전
12 년 전
10 년 전
12 년 전
12 년 전
11 년 전
11 년 전
12 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
11 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. #!/bin/sh
  2. # TODO: decide the naming scheme of variables (global, local, ...)
  3. expr "$-" : '^.*i' >/dev/null || return
  4. ##########################################
  5. __shrc_lock=/tmp/shrc_lock.$USER.$$
  6. test -f "$__shrc_lock" && return
  7. touch "$__shrc_lock"
  8. ##########################################
  9. null(){
  10. "$@" >/dev/null 2>&1
  11. }
  12. __safe_run(){
  13. type $1 >/dev/null 2>&1 && "$@"
  14. }
  15. __match(){
  16. # __match str word
  17. # return 0 if word is found in str
  18. expr "$1" : ".*$2.*" >/dev/null
  19. }
  20. __ln=$'\n'
  21. __cr=$'\r'
  22. #################################
  23. # profile-like setups
  24. __safe_add_path_r(){
  25. # add path to right
  26. test -d "$1" && PATH="${PATH}:$1"
  27. }
  28. __safe_add_path_l(){
  29. # add path to left
  30. test -d "$1" && PATH="$1:${PATH}"
  31. }
  32. __safe_add_path_l "$HOME/.cabal/bin"
  33. __safe_add_path_l "$HOME/.local/lib/gems/bin"
  34. __safe_add_path_l "$HOME/.local/bin"
  35. __safe_add_path_l "$HOME/.gem/ruby/2.1.0/bin"
  36. __safe_add_path_r "/c/mingw/bin"
  37. __safe_add_path_r "/c/mingw/msys/1.0/bin"
  38. # macports coreutils
  39. # $isdarwin cannot be used it is not defined yet
  40. __safe_add_path_l "/opt/local/bin"
  41. __safe_add_path_l "/opt/local/sbin"
  42. __safe_add_path_l "/opt/local/libexec/gnubin"
  43. __safe_add_path_l \
  44. "/opt/local/Library/Frameworks/Python.framework/Versions/3.2/bin"
  45. test -f "${__dotdir}/rc.py" && export PYTHONSTARTUP="${__dotdir}/rc.py"
  46. test -d "$HOME/.local/lib/python/site-packages" && \
  47. export PYTHONPATH="${PYTHONPATH}:${HOME}/.local/lib/python/site-packages"
  48. export GEM_HOME="$HOME/.local/lib/gems"
  49. export RUBYLIB="$RUBYLIB:$HOME/.local/lib/gems/lib"
  50. # it is not so good
  51. # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  52. # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  53. # without this ENV i cannot run tmux. another way is to use --disable-shared
  54. # when building tmux
  55. if ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  56. then
  57. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  58. fi
  59. # in my environment powerdown does not work
  60. test -z "$DISPLAY" && test -z "$SSH_CONNECTION" && \
  61. type setterm >/dev/null 2>&1 && \
  62. setterm -blank 30 -powersave on # -powerdown 10
  63. #########################
  64. # shrc.common
  65. # this variable must consistent with setup.sh
  66. __shrc_common="$HOME/.shrc.common"
  67. if test -f "$__shrc_common"
  68. then
  69. . "$__shrc_common"
  70. else
  71. echo "$__shrc_common not found."
  72. echo "Run setup.sh first."
  73. return
  74. fi
  75. ##########################
  76. # system type
  77. gnu_coreutils=false # for mac
  78. null ls --version && gnu_coreutils=true
  79. inbash=false
  80. inzsh=false
  81. if test -n "$BASH_VERSION"
  82. then
  83. inbash=true
  84. elif test -n "$ZSH_VERSION"
  85. then
  86. inzsh=true
  87. fi
  88. #################################
  89. # file pathes:
  90. # shrc: Path to this file
  91. if $inbash
  92. then
  93. __shrc="$BASH_SOURCE"
  94. elif $inzsh
  95. then
  96. __shrc="$0"
  97. fi
  98. ##################################
  99. # EnvVal definitions
  100. test "$TERM" = linux && export LANG=C
  101. export LC_MESSAGES=C
  102. export LC_TIME=C
  103. export TERMCAP="${TERMCAP}:vb="
  104. $ismsys && export HOSTNAME
  105. # export ENV=~/.shrc
  106. if false $iswindows
  107. then
  108. export PAGER='tr -d \\r | less'
  109. else
  110. export PAGER="less"
  111. fi
  112. export LESS="-iRMX"
  113. # Style for lesspipe is defined in esc.style
  114. _src_hilite_lp_path="`command -v src-hilite-lesspipe.sh 2>/dev/null`"
  115. for f in /usr/share/source-highlight/src-hilite-lesspipe.sh
  116. do
  117. test -z "$_src_hilite_lp_path" && test -e "$f" && _src_hilite_lp_path="$f"
  118. done
  119. test -n "$_src_hilite_lp_path" && export LESSOPEN="| $_src_hilite_lp_path %s"
  120. if null type vim
  121. then
  122. export EDITOR=vim
  123. else
  124. export EDITOR=vi
  125. fi
  126. # export CDPATH=".:~"
  127. export VISUAL="$EDITOR"
  128. export GIT_PAGER="less -F"
  129. export GIT_EDITOR="$EDITOR"
  130. export GIT_MERGE_AUTOEDIT=no
  131. if test -n "$TMUX" && \
  132. __match $TERM screen && \
  133. __match `tmux display -p '#{client_termname}'` 256color
  134. then
  135. TERM=screen-256color
  136. fi
  137. # set TMP, TEMP, TMPDIR
  138. if test -z "$TMP"
  139. then
  140. if test -n "$TMPDIR"
  141. then
  142. export TMP=$TMPDIR
  143. elif test -n "$TEMP"
  144. then
  145. export TMP="$TEMP"
  146. else
  147. export TMP=/tmp
  148. fi
  149. fi
  150. __match "$TMP" "${USER}-tmp" >/dev/null || TMP="${TMP}/${USER}-tmp"
  151. test -d "$TMP" || mkdir -p "$TMP"
  152. export TEMP=$TMP
  153. export TMPDIR=$TMP
  154. if ! $iswindows && null type stty
  155. then
  156. stty stop undef # unbind C-s to stop displaying output
  157. # stty erase '^h'
  158. fi
  159. if $iswindows
  160. then
  161. export USER=$USERNAME
  162. fi
  163. if test -d ~/dbx
  164. then
  165. export CHIT_PATH="$HOME/dbx/.chit"
  166. fi
  167. # adhoc fix for ansible on cygwin
  168. # http://blog.s-uni.net/2013/08/27/ansible-running-on-cygwin/
  169. if $incygwin
  170. then
  171. export ANSIBLE_SSH_ARGS="-o ControlMaster=no"
  172. fi
  173. ##########################
  174. # Zsh specific preferences
  175. # http://www.clear-code.com/blog/2011/9/5.html
  176. if $inzsh
  177. then
  178. bindkey -e
  179. # http://zsh.sourceforge.net/Guide/zshguide06.html#l147
  180. autoload compinit; compinit
  181. # supress cycle by tab
  182. unsetopt auto_menu
  183. # unsetopt correct
  184. setopt complete_aliases
  185. setopt auto_list
  186. setopt bash_auto_list
  187. setopt magic_equal_subst
  188. setopt list_types
  189. # what is the difference of these two?
  190. setopt auto_param_slash
  191. setopt mark_dirs
  192. zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
  193. zstyle ':completion:*' format '%B%d%b'
  194. zstyle ':completion:*' group-name ''
  195. zstyle ':completion:*' use-cache yes
  196. # zstyle ':completion:*:cd:*' tag-order local-directories
  197. zstyle ':completion:*' completer _complete _bash_completions \
  198. _history
  199. # zstyle ':completion:*:*:cd:*' completer
  200. zstyle ':completion:*' accept-exact-dirs true
  201. zstyle ':completion:*' special-dirs true
  202. autoload colors; colors
  203. # autoload -Uz promptinit
  204. # promptinit
  205. # prompt walters
  206. setopt hist_ignore_dups
  207. setopt hist_ignore_all_dups
  208. setopt hist_save_no_dups
  209. setopt extended_history
  210. setopt share_history
  211. setopt append_history
  212. HISTFILE=$HOME/.zsh-history
  213. HISTSIZE=100000
  214. SAVEHIST=100000
  215. setopt prompt_subst
  216. setopt interactive_comments
  217. fi
  218. ######################
  219. # Print welcome messages
  220. command -v fortune >/dev/null && {
  221. if command -v cowsay >/dev/null
  222. then
  223. fortune | cowsay
  224. echo
  225. fortune -o | cowsay
  226. echo
  227. else
  228. fortune
  229. echo
  230. fortune -o
  231. echo
  232. fi
  233. }
  234. uname -a
  235. $iswindows && alias tty="echo cmd.exe"
  236. echo TERM $TERM with $(tput colors) colors using $(tty)
  237. echo
  238. __safe_run w yuk
  239. # if null type tmux
  240. # then
  241. # echo
  242. # echo tmux sessions:
  243. # tmux ls 2>/dev/null| sed -e 's/^/ /g'
  244. # echo
  245. # fi
  246. # if test -n "$TMUX"
  247. # then
  248. # tmux display -p \
  249. # 'TMUX #S:#I:#W.#P, client TERM: #{client_termname}' \
  250. # 2>/dev/null
  251. # echo
  252. # fi
  253. ###################################
  254. # some aliases and functions
  255. # __func_name: never used interactively
  256. # _func_name: usually not used interactively
  257. __safe_alias(){
  258. # __safe_alias <name>=<command>
  259. _bin=`expr "$1" : '^[^=]*=\([^ ]*\)'`
  260. test -n "$_bin" && \
  261. null type $_bin && \
  262. alias "$1"
  263. }
  264. $gnu_coreutils && _timeoption=" --time-style=long-iso"
  265. # color prefs
  266. if $gnu_coreutils
  267. then
  268. # http://qiita.com/yuyuchu3333/items/84fa4e051c3325098be3
  269. # gnu coreutils LS_COLORS is used
  270. null type dircolors && eval `dircolors`
  271. _coloroption=" --color=auto"
  272. else
  273. # export LSCOLORS=gxfxcxdxbxegedabagacad
  274. export LSCOLORS=DxGxcxdxCxegedabagacad
  275. export CLICOLOR=1
  276. fi
  277. alias ls="ls -hCF${_coloroption}${_timeoption}"
  278. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  279. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  280. _timeformat_num="%Y%m%d%H%M%S"
  281. alias datenum="date +$_timeformat_num"
  282. # export GREP_OPTIONS=""
  283. alias gr="grep -n --color=always"
  284. $iswindows && alias gr="grep -n"
  285. alias less="less -F"
  286. __safe_alias em="emacs -nw"
  287. __safe_alias vi=vim
  288. alias pstree="LANG=C pstree"
  289. alias cp="cp -v"
  290. alias mv="mv -v"
  291. alias rm="rm -v"
  292. alias mkdir="mkdir -v"
  293. alias rmdir="rmdir -v"
  294. alias psaux="ps auxww"
  295. alias modest="ionice -c 2 -n 7 nice -n 19"
  296. alias q=exit
  297. __safe_alias e3=e3em
  298. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  299. alias po=popd
  300. alias pu=pushd
  301. __safe_alias sudo="sudo " # use aliases through sudo
  302. __safe_alias sudoe="sudoedit"
  303. # __safe_alias halt="sudo halt"
  304. # __safe_alias reboot="sudo reboot"
  305. null type dbus-send && {
  306. alias suspend="dbus-send --system --print-reply \
  307. --dest=org.freedesktop.UPower \
  308. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  309. alias hibernate="dbus-send --system --print-reply \
  310. --dest=org.freedesktop.UPower \
  311. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  312. }
  313. alias rand="echo \$RANDOM"
  314. __safe_alias xunp="file-roller -h"
  315. __safe_alias pc="sudo \paco -D"
  316. alias pycalc="python -i -c 'from math import *' "
  317. __safe_alias py3=python3
  318. __safe_alias py2=python2
  319. __safe_alias ipy=ipython
  320. __safe_alias ipy3=ipython3
  321. __safe_alias ipy2=ipython2
  322. # Sometimes SHELL cannot be used. For example, when running bash inside zsh
  323. # SHELL is set to be /bin/zsh
  324. if $inbash
  325. then
  326. alias _reloadrc="rm '$__shrc_lock'; exec bash"
  327. elif $inzsh
  328. then
  329. alias _reloadrc="rm '$__shrc_lock'; exec zsh"
  330. else
  331. alias _reloadrc="rm '$__shrc_lock'; exec $SHELL"
  332. fi
  333. # alias mytime="date +%Y%m%d-%H%M%S"
  334. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  335. # some extra utilities
  336. # type trash >/dev/null 2>&1 && alias rm=trash
  337. __safe_alias mpg123="mpg123 -C -v --title"
  338. __safe_alias xm="xmms2"
  339. #export PLAYER="mpg123 -C -v --title"
  340. __safe_alias screen="screen -e^z^z"
  341. #alias zcd="cd \`zenity --file-selection --directory\`"
  342. __safe_alias gtags="gtags --verbose"
  343. __safe_alias htags="htags --xhtml --symbol --line-number \
  344. --frame --alphabet --verbose"
  345. __safe_alias au=aunpack
  346. # __safe_alias lv="lv|less"
  347. __safe_alias rs="rsync --progress --itemize-changes --compress"
  348. __safe_alias vagr=vagrant
  349. __safe_alias ztw='twitter set "`zenity --entry --title ztw --text Status:`"'
  350. if $iscygwin
  351. then
  352. __my_wget_options=" --no-check-certificate"
  353. __safe_alias wget="wget $__my_wget_options"
  354. fi
  355. if $isdarwin
  356. then
  357. alias updatedb="LC_ALL=C updatedb"
  358. # do not use locate installed by macports
  359. test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  360. fi
  361. # somehow not work
  362. # typeset -ga chpwd_functions
  363. # chpwd_functions+=pwd
  364. # chpwd(){
  365. # pwd
  366. # }
  367. cd(){
  368. builtin cd "$@" && pwd
  369. }
  370. root(){
  371. if test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = true
  372. then
  373. cd "`git rev-parse --show-toplevel`"
  374. else
  375. cd /
  376. fi
  377. }
  378. # pad
  379. alias pad=notepad
  380. __safe_alias pad=gedit
  381. __safe_alias pad=leafpad
  382. $isdarwin && alias pad="open -e"
  383. __find_latest_vimdir(){
  384. vimdir=/usr/share/vim
  385. if test -d "$vimdir"
  386. then
  387. find "$vimdir" -name 'vim??' -type d | sort | tail -n 1
  388. else
  389. echo ""
  390. fi
  391. }
  392. for f in /usr/share/vim/vimcurrent "`__find_latest_vimdir`"
  393. do
  394. test -n "$f" || continue
  395. f="$f/macros/less.sh"
  396. test -f $f && alias vl=$f && break
  397. done
  398. __safe_alias pa="pacman --color=always"
  399. __safe_alias pa="pacmatic --color=always"
  400. __safe_alias pa="pacapt"
  401. __safe_alias yt=yaourt
  402. # variable for yaourt
  403. null type pacmatic && export PACMAN="pacmatic"
  404. __safe_alias cower="cower --color=auto"
  405. __my_pacman_update_mirrorlist_with_reflector(){
  406. f=/etc/pacman.d/mirrorlist
  407. cmd="$(expr "$(grep -m 1 reflector $f)" : '# With: *\(.*\)')"
  408. if test -z "$cmd"
  409. then
  410. cmd="reflector --verbose --number 5 --sort rate --protocol http --protocol https --save $f"
  411. fi
  412. echo ">>> $cmd ..." 1>&2
  413. sudo sh -c "$cmd" && cat $f
  414. }
  415. null type reflector && test -d /etc/pacman.d && \
  416. alias reflect_mirrorlist=__my_pacman_update_mirrorlist_with_reflector
  417. null type apt-get && {
  418. alias aupgrade="sudo sh -xc 'apt-get autoremove --yes && \
  419. apt-get update --yes && apt-get upgrade --yes'"
  420. alias aptin="apt-get install"
  421. alias aptsearch="apt-cache search"
  422. alias aptshow="apt-cache show"
  423. }
  424. null type port && {
  425. alias port="port -v"
  426. alias pupgrade="sudo sh -xc 'port -v selfupdate && \
  427. port -v upgrade outdated'"
  428. }
  429. if $iscygwin
  430. then
  431. null type windate || \
  432. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  433. # alias cygsu="cygstart /cygwinsetup.exe"
  434. fi
  435. mkpatch(){
  436. if test $# -eq 0
  437. then
  438. echo "usage: mkpatch <olddir> <newdir>"
  439. elif ! test -d "$1"
  440. then
  441. echo "mkpatch: $1 is not a directory"
  442. elif ! test -d "$2"
  443. then
  444. echo "mkpatch: $2 is not a directory"
  445. else
  446. diff -Naur "$1" "$2"
  447. fi
  448. }
  449. __sdcv_less(){
  450. command sdcv -n "$@" | less --quit-if-one-screen
  451. }
  452. command -v sdcv >/dev/null && alias dict=__sdcv_less
  453. g(){
  454. if test $# -eq 0 && null type git-info
  455. then
  456. git info
  457. else
  458. git -c color.ui=always "$@"
  459. fi
  460. }
  461. if null type _git && $inbash
  462. then
  463. # enable programmable completion for g
  464. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  465. || complete -o default -o nospace -F _git g
  466. fi
  467. #git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  468. __safe_alias m=gitmemo
  469. alias setup.py3="sudo python3 setup.py install --record files.txt"
  470. randomstr(){
  471. len=$1
  472. test -z "$len" && len=8
  473. uuidgen | tr -d - | cut -c 1-len
  474. }
  475. datestr(){
  476. # datestr YYYYMMDDhhmmss
  477. if test -z "$1" || test "$1" = "-h"
  478. then
  479. echo "datestr: usage: datestr <YYYYMMDDhhmmss>"
  480. return 1
  481. fi
  482. dfmt="" # actual format for date command
  483. while test -n "$1"
  484. do
  485. fmt="$1"
  486. shift
  487. while test -n "$fmt"
  488. do
  489. case "$fmt" in
  490. YYYY*|yyyy*) # year
  491. dfmt="${dfmt}%Y"
  492. fmt="`echo "$fmt" | cut -c 5-`"
  493. ;;
  494. YY*|yy*) # last two digits of year
  495. dfmt="${dfmt}%y"
  496. fmt="`echo "$fmt" | cut -c 3-`"
  497. ;;
  498. MM*) # month (01..12)
  499. dfmt="${dfmt}%m"
  500. fmt="`echo "$fmt" | cut -c 3-`"
  501. ;;
  502. DD*|dd*) # day of month (01..12)
  503. dfmt="${dfmt}%d"
  504. fmt="`echo "$fmt" | cut -c 3-`"
  505. ;;
  506. HH* | hh*) # hour (00..23)
  507. dfmt="${dfmt}%H"
  508. fmt="`echo "$fmt" | cut -c 3-`"
  509. ;;
  510. mm*) # minute (00..59)
  511. dfmt="${dfmt}%M"
  512. fmt="`echo "$fmt" | cut -c 3-`"
  513. ;;
  514. SS*|ss*) # second (00..60)
  515. dfmt="${dfmt}%S"
  516. fmt="`echo "$fmt" | cut -c 3-`"
  517. ;;
  518. *)
  519. char=`echo "$fmt" | cut -c 1`
  520. dfmt="${dfmt}${char}"
  521. fmt="`echo "$fmt" | cut -c 2-`"
  522. ;;
  523. esac
  524. done
  525. done
  526. date +"$dfmt"
  527. }
  528. # ssh(){
  529. # # __my_terminal_title ssh
  530. # command ssh "$@"
  531. # }
  532. __ssh_with_cd(){
  533. # __ssh_with_cd <host> <directory> [<arg> ...]
  534. if test -z "$2"
  535. then
  536. echo "usage: __ssh_with_cd <host> <directory> [<arg> ...]"
  537. return 1
  538. fi
  539. host="$1"
  540. shift
  541. dir="$1"
  542. shift
  543. ssh "$host" "$@" -t "cd \"$dir\"; \$SHELL -l"
  544. }
  545. memo(){
  546. if test -z "$1"
  547. then
  548. _memo="memo.txt"
  549. else
  550. _memo="$1/memo.txt"
  551. fi
  552. $EDITOR "$_memo"
  553. if test -z "`cat "$_memo"`"
  554. then
  555. echo "$_memo is empty. Removing."
  556. rm "$_memo"
  557. fi
  558. }
  559. now(){
  560. ___tformat="%Y/%m/%d %H:%M:%S %z"
  561. cal
  562. REPLY=
  563. printf "\\r`date "+${___tformat}"`"
  564. read -t 1
  565. while test $? -ne 0
  566. do
  567. printf "\\r`date "+${___tformat}"`"
  568. read -t 1
  569. done
  570. }
  571. s(){
  572. if git rev-parse --git-dir >/dev/null 2>&1
  573. then
  574. echo ">> git grep -n $@" 1>&2
  575. git grep -n "$@"
  576. elif which ag >/dev/null 2>&1
  577. then
  578. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  579. ag --pager="$PAGER" "$@"
  580. elif which ack >/dev/null 2>&1
  581. then
  582. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  583. ack --pager="$PAGER" "$@"
  584. else
  585. echo \
  586. ">> find . " \
  587. "-path '*/.git' -prune -o" \
  588. "-path '*/.svn' -prune -o" \
  589. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  590. if test $# -eq 0
  591. then
  592. echo "No search word given." 1>&2
  593. return 1
  594. fi
  595. find . \
  596. -path '*/.git' -prune -o \
  597. -path '*/.svn' -prune -o \
  598. -type -f -exec grep -nH -e --color=always "$@" {} + \
  599. | $PAGER
  600. fi
  601. }
  602. MYMANPATH='/usr/lib/erlang/man'
  603. if $inbash || $inzsh
  604. then
  605. man(){
  606. env \
  607. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  608. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  609. LESS_TERMCAP_me=$(printf "\e[0m") \
  610. LESS_TERMCAP_se=$(printf "\e[0m") \
  611. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  612. LESS_TERMCAP_ue=$(printf "\e[0m") \
  613. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  614. MANPATH="`manpath`:$MYMANPATH" \
  615. man "$@"
  616. }
  617. fi
  618. netwait(){
  619. while ! ping -c 1 -t 1 example.com
  620. do
  621. true
  622. done
  623. echo network works.
  624. }
  625. __realpath(){
  626. if type realpath >/dev/null 2>&1
  627. then
  628. command realpath "$@"
  629. else
  630. while ! test -d $1
  631. do
  632. shift
  633. done
  634. (command cd "$d" && echo "$PWD")
  635. # local d="$OLDPWD"
  636. # command cd "$1"
  637. # echo "$PWD"
  638. # command cd "$d"
  639. fi
  640. }
  641. __safe_alias tmux='env -u SHLVL tmux'
  642. tx(){
  643. tmux start-server
  644. if test -z "$1"
  645. then
  646. echo "usage: tx <session_name>"
  647. tmux list-sessions
  648. elif test -z "$TMUX"
  649. then
  650. # -A: attach if already exists
  651. # tmux new-session -A -c "$HOME" -s "$1"
  652. tmux new-session -A -s "$1"
  653. else
  654. # create new session if not exists yet
  655. # tmux has-session -t "$1" || TMUX= tmux new-session -d -c "$HOME" -s "$1"
  656. tmux has-session -t "$1" || TMUX= tmux new-session -d -s "$1"
  657. tmux switch-client -t "$1"
  658. fi
  659. }
  660. dt(){
  661. # dt [-h] [<name>] [<command ...>]
  662. __dtach_dir="${TMP}/dtach"
  663. mkdir -p "${__dtach_dir}"
  664. if test -n "${__MY_DTACH}"
  665. then
  666. # if already in dtach session print current session
  667. soc_name="`basename "$__MY_DTACH"`"
  668. echo "Current session: ${soc_name}"
  669. fi
  670. if test -z "$1"
  671. then
  672. # if no arg given show list of sessions
  673. echo "Sessions:"
  674. ls "${__dtach_dir}"
  675. return 0
  676. elif test "$1" = "-h"
  677. then
  678. echo "dt: usage: dt [-h] [<name>] [<command ...>]" 1>&2
  679. return 1
  680. fi
  681. # set socket name
  682. soc_name="${__dtach_dir}/$1"
  683. shift
  684. if test -n "$__MY_DTACH"
  685. then
  686. echo "dtach session cannot be nested." 1>&2
  687. return 1
  688. elif test -S "$soc_name"
  689. then
  690. dtach -a "$soc_name" -e ^^
  691. elif test -e "$soc_name"
  692. then
  693. echo "dt: File named $soc_name already exists."
  694. return 1
  695. elif test -z "$1"
  696. then
  697. # if no command given invoke current shell
  698. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  699. # echo "dt: Socket named $soc_name not exists and no command specified."
  700. # return 1
  701. else
  702. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  703. fi
  704. }
  705. scr(){
  706. test -n "$1" && pf="${1}-"
  707. ___tformat="%Y%m%d-%H%M%S%z"
  708. ___file="${HOME}/${pf}`date +${___tformat}`.script"
  709. __MY_SCRIPT=${___file} script ${___file} "$@"
  710. }
  711. dtscr(){
  712. # dtscr <command ...>
  713. if test -z "$1"
  714. then
  715. echo "dtscr: usage: dtscr <command ...>"
  716. return 1
  717. fi
  718. ___cmdstr="`echo $@ | tr ' ' +`"
  719. ___tformat="%Y%m%d-%H%M%S%z"
  720. ___name="${pf}`date +${___tformat}`-${___cmdstr}"
  721. ___scr_file="${HOME}/${___name}.script"
  722. ___dt_dir="${TMP}/dtscr"
  723. mkdir -p "$___dt_dir"
  724. dtach -n "${___dt_dir}/${___name}" script "${___scr_file_}" "$@"
  725. # echo $_name
  726. # echo $_file
  727. }
  728. mcrypt_stream(){
  729. test $# -eq 2 || return 1
  730. case $1 in
  731. en)
  732. mcrypt --key $2 | base64 ;;
  733. de)
  734. base64 -d | mcrypt -d --key $2 ;;
  735. esac
  736. }
  737. gpg_stream(){
  738. test $# -eq 2 || return 1
  739. case $1 in
  740. en)
  741. gpg --passphrase $2 -c --batch |base64 ;;
  742. de)
  743. base64 -d|gpg --passphrase $2 -d --batch ;;
  744. esac
  745. }
  746. dgpg(){
  747. if test "$1" = help || test -z "$2"
  748. then
  749. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  750. return
  751. fi
  752. ___srcs="$2"
  753. ___dsts="$3"
  754. test -z "$___dsts" && ___dsts="${___srcs}.out"
  755. ___pw=
  756. echo -n "dgpg pw: "
  757. read -s ___pw
  758. echo ""
  759. test -z "$___pw" && return 1
  760. for f in *${___srcs}
  761. do
  762. ___d="$(basename "$f" "${___srcs}")${___dsts}"
  763. echo -n "Processing $f to $___d..."
  764. if test -d "$f"
  765. then
  766. echo "`printf 'failed (%s is directory)' $f`"
  767. elif test -f "$___d"
  768. then
  769. echo "`printf 'failed (%s is already exists)' $___d`"
  770. elif <"$f" gpg_stream $1 $___pw >"$___d" 2>/dev/null
  771. then
  772. echo "done"
  773. else
  774. echo "failed"
  775. test -f "$___d" && rm "$___d"
  776. fi
  777. done
  778. }
  779. alias enst="gpg_stream en"
  780. alias dest="gpg_stream de"
  781. showinfo(){
  782. echo "Japanese letters are 表示可能"
  783. __safe_run diskinfo
  784. ! $isdarwin && test -n "${DISPLAY}" && {
  785. __safe_run xrandr | \grep --color=never ^Screen
  786. }
  787. $iswindows || __safe_run finger $USER
  788. LANG=C __safe_runc id
  789. __safe_run xset q
  790. }
  791. x(){
  792. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  793. #mkdir -p ~/.var/log
  794. # nohup startx >~/.var/log/xorg.log 2>&1 &
  795. # exit
  796. exec startx
  797. else
  798. echo "X cant be started! Another X is already running?" 1>&2
  799. fi
  800. }
  801. bak(){
  802. for file in "$@"
  803. do
  804. cp -v ${file} ${file}.bak
  805. done
  806. }
  807. di(){
  808. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  809. then
  810. ___diffcmd=colordiff
  811. else
  812. ___diffcmd=diff
  813. fi
  814. ${___diffcmd} -u "$@" | ${PAGER}
  815. }
  816. tb(){
  817. __datenum=`date +%Y%m%d-%H%M%S`
  818. __tb="$HOME/.var/tb/$__datenum"
  819. mkdir -p "$__tb"
  820. mv "$@" "$__tb"
  821. }
  822. mkdd(){
  823. _d=`date +%Y%m%d-%H%M%S` && \
  824. mkdir -p "$_d"
  825. }
  826. mkcd(){
  827. if test -z "$1"
  828. then
  829. echo "mkcd: usage: mkcd <dir>"
  830. return 1
  831. elif test -d "$1"
  832. then
  833. echo "Dir \"$1\" already exists."
  834. else
  835. mkdir -p "$1"
  836. echo "Dir \"$1\" created."
  837. fi
  838. cd "$1"
  839. }
  840. mkcdd(){
  841. # make and change date directory
  842. _d=`date +%Y%m%d-%H%M%S` && \
  843. mkcd "$_d"
  844. }
  845. if test -n "$TMUX" && null type reattach-to-user-namespace
  846. then
  847. alias pbpaste="reattach-to-user-namespace pbpaste"
  848. alias pbcopy="reattach-to-user-namespace pbcopy"
  849. fi
  850. catclip(){
  851. if $iswindows
  852. then
  853. cat /dev/clipboard | tr -d \\r
  854. elif $isdarwin
  855. then
  856. pbpaste
  857. else
  858. xclip -o -selection "clipboard"
  859. fi
  860. }
  861. setclip(){
  862. if test $# -eq 0
  863. then
  864. exec 3<&0
  865. else
  866. exec 3<<__EOF__
  867. `cat "$@"`
  868. __EOF__
  869. fi
  870. if $iswindows
  871. then
  872. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  873. elif $isdarwin
  874. then
  875. pbcopy 0<&3
  876. else
  877. 0<&3 xclip -i -f -selection "primary" | \
  878. xclip -i -f -selection "clipboard"
  879. fi
  880. exec 3<&-
  881. }
  882. open_file(){
  883. if $iscygwin
  884. then
  885. cygstart "$@"
  886. elif $ismsys
  887. then
  888. cmd.exe //c start "" "$@"
  889. elif $isdarwin
  890. then
  891. touch "$@"
  892. open "$@"
  893. elif $islinux
  894. then
  895. touch "$@"
  896. if null type pcmanfm; then
  897. LC_MESSAGES= pcmanfm "$@"
  898. else
  899. LC_MESSAGES= xdg-open "$@" &
  900. fi
  901. else
  902. cat "$@"
  903. fi
  904. }
  905. o(){
  906. if test $# -eq 0
  907. then
  908. open_file .
  909. else
  910. for f in "$@"
  911. do
  912. open_file "$(realpath "$f")"
  913. done
  914. fi
  915. }
  916. convmv_sjis2utf8_test(){
  917. convmv -r -f sjis -t utf8 *
  918. }
  919. convmv_sjis2utf8_notest(){
  920. convmv -r -f sjis -t utf8 * --notest
  921. }
  922. #################################################
  923. ## pastebin services
  924. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  925. sprunge(){
  926. # http://sprunge.us
  927. if test -z "$1"
  928. then
  929. curl -F 'sprunge=<-' http://sprunge.us
  930. else
  931. curl http://sprunge.us/$1
  932. fi
  933. }
  934. dpaste(){
  935. # http://dpaste.de
  936. if test -z "$1"
  937. then
  938. curl -F 'content=<-' https://dpaste.de/api/
  939. echo
  940. else
  941. curl https://dpaste.de/$1/raw/
  942. fi
  943. }
  944. ######################################
  945. ## Prompt Settings
  946. __my_moc_state(){
  947. type mocp >/dev/null 2>&1 || return
  948. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  949. printf "$1" "`mocp -Q %title 2>/dev/null`"
  950. }
  951. __my_parse_svn_branch() {
  952. ___svn_url=$(LANG=C svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  953. ___svn_repository_root=$(LANG=C svn info 2>/dev/null | \
  954. sed -ne 's#^Repository Root: ##p')
  955. echo ${___svn_url} | sed -e 's#^'"${___svn_repository_root}"'##g' | \
  956. awk '{print $1}'
  957. }
  958. __my_svn_ps1(){
  959. if svn status >/dev/null 2>&1
  960. then
  961. ___svn_branch=$(__my_parse_svn_branch)
  962. test -n "${___svn_branch}" && printf "$1" "{$___svn_branch}"
  963. fi
  964. }
  965. __my_battery_status(){
  966. ___dir=/sys/class/power_supply/BAT0
  967. if test -d $___dir && test -r $___dir/status && test -r $___dir/charge_full && \
  968. test -r $___dir/charge_now
  969. then
  970. ___st=$(cat $___dir/status)
  971. ___full=$(cat $___dir/charge_full)
  972. ___now=$(cat $___dir/charge_now)
  973. ___rate=$(expr $now \* 100 / $full)
  974. printf "$1" "${___st}:${___rate}%"
  975. fi
  976. }
  977. alias bat='__my_battery_status %s\\n'
  978. __my_ps1_scale(){
  979. if null type stty && ! $ismsys
  980. then
  981. echo "[LxC:`stty size | tr -d $'\n' | tr " " x`]"
  982. fi
  983. }
  984. __my_ps1_tmux(){
  985. null type tmux || return $last
  986. ___tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  987. test -n "$TMUX" && echo "[TMUX:$___tmuxc]"
  988. }
  989. __my_ps1_moc(){
  990. __my_moc_state "[MOC:%s]"
  991. }
  992. for f in /usr/share/git/git-prompt.sh \
  993. /usr/share/git-core/contrib/completion/git-prompt.sh \
  994. /usr/local/share/git-core/contrib/completion/git-prompt.sh \
  995. /etc/bash_completion.d/git-prompt \
  996. /opt/local/share/git-core/git-prompt.sh \
  997. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  998. do
  999. test -r "$f" && ($inbash || $inzsh) && . "$f" && break
  1000. done
  1001. GIT_PS1_SHOWDIRTYSTATE=t
  1002. GIT_PS1_SHOWUPSTREAM=t
  1003. __my_ps1_git(){
  1004. null type __git_ps1 || return $last
  1005. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  1006. __git_ps1 "[GIT:$(__safe_run git config --get user.name):%s]"
  1007. }
  1008. __printf_ipaddr(){
  1009. # ipaddress <fmt>
  1010. type ip >/dev/null 2>&1 || return 1
  1011. ___ip=$(LANG=C ip addr show scope global | \
  1012. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  1013. test -n "$___ip" && printf "$1" $___ip
  1014. }
  1015. alias addr="__printf_ipaddr '%s'"
  1016. __my_ps1_ipaddr(){
  1017. ! $iswindows && __printf_ipaddr '[Addr:%s]'
  1018. }
  1019. __my_ps1_bttry(){
  1020. ___bst="${TMP}/batterystatus"
  1021. if test -z "$DISPLAY" && ! $iswindows
  1022. then
  1023. test -f $___bst && ___bstr="$(cat $___bst)"
  1024. test -n "$___bstr" && ! echo $___bstr | grep 100 >/dev/null 2>&1 && \
  1025. echo "[Battery:$___bstr]"
  1026. __my_battery_status %s >$___bst &
  1027. fi
  1028. }
  1029. __my_ps1_memo(){
  1030. test -f memo.txt && echo "m:`du -b memo.txt|cut -f 1`"
  1031. }
  1032. __my_ps1_dirs(){
  1033. dirs | wc -l
  1034. }
  1035. __my_ps1_jobs(){
  1036. # __my_ps1_jobs [<num>]
  1037. if test -n "$1"
  1038. then
  1039. jobs="$1"
  1040. else
  1041. jobs="`jobs | wc -l`"
  1042. fi
  1043. if test "$jobs" -gt 0
  1044. then
  1045. echo "JOBS:$jobs"
  1046. fi
  1047. }
  1048. __my_ps1_dollar(){
  1049. if test -z "$SHLVL"
  1050. then
  1051. printf "$1"
  1052. else
  1053. perl -e 'while($ARGV[0]-- > 0){print "$ARGV[1]";}' $SHLVL "$1"
  1054. fi
  1055. }
  1056. __my_alert_fail(){
  1057. test $laststatus -eq 0 || \
  1058. echo "STATUS:${laststatus}"
  1059. }
  1060. # About ansi escape sequences
  1061. # http://archive.linux.or.jp/JF/JFdocs/Bash-Prompt-HOWTO-5.html
  1062. # http://www.grapecity.com/japan/powernews/column/clang/047/page02.htm
  1063. if $inbash || $inzsh
  1064. then
  1065. if $inzsh
  1066. then
  1067. __attr_beg=$'%{\033['
  1068. __attr_end='m%}'
  1069. else
  1070. __attr_beg='\[\033['
  1071. __attr_end='m\]'
  1072. fi
  1073. __color_default="${__attr_beg}0${__attr_end}"
  1074. __color_black="${__attr_beg}0;30${__attr_end}"
  1075. __color_red="${__attr_beg}0;31${__attr_end}"
  1076. __color_green="${__attr_beg}0;32${__attr_end}"
  1077. __color_brown="${__attr_beg}0;33${__attr_end}"
  1078. __color_blue="${__attr_beg}0;34${__attr_end}"
  1079. __color_purple="${__attr_beg}0;35${__attr_end}"
  1080. __color_cyan="${__attr_beg}0;36${__attr_end}"
  1081. __color_light_gray="${__attr_beg}0;37${__attr_end}"
  1082. __color_dark_gray="${__attr_beg}1;30${__attr_end}"
  1083. __color_light_red="${__attr_beg}1;31${__attr_end}"
  1084. __color_light_green="${__attr_beg}1;32${__attr_end}"
  1085. __color_yellow="${__attr_beg}1;33${__attr_end}"
  1086. __color_light_blue="${__attr_beg}1;34${__attr_end}"
  1087. __color_light_purple="${__attr_beg}1;35${__attr_end}"
  1088. __color_light_cyan="${__attr_beg}1;36${__attr_end}"
  1089. __color_white="${__attr_beg}1;37${__attr_end}"
  1090. __color_bg_black="${__attr_beg}40${__attr_end}"
  1091. __color_bg_red="${__attr_beg}41${__attr_end}"
  1092. __color_bg_green="${__attr_beg}42${__attr_end}"
  1093. __color_bg_brown="${__attr_beg}43${__attr_end}"
  1094. __color_bg_blue="${__attr_beg}44${__attr_end}"
  1095. __color_bg_purple="${__attr_beg}45${__attr_end}"
  1096. __color_bg_cyan="${__attr_beg}46${__attr_end}"
  1097. __color_bg_light_gray="${__attr_beg}47${__attr_end}"
  1098. __attr_underline="${__attr_beg}4${__attr_end}"
  1099. __attr_reverse="${__attr_beg}7${__attr_end}"
  1100. __attr_bold="${__attr_beg}1${__attr_end}"
  1101. fi
  1102. # NOTE: tput is another easy way to set colors and background
  1103. # For example, "$(tput setab 4)text$(tput sgr0)" print text with background
  1104. # color blue.
  1105. # http://www.ibm.com/developerworks/jp/linux/aix/library/au-learningtput/index.html
  1106. if test "$TERM" != dumb
  1107. then
  1108. __my_c1="$__attr_bold$__attr_underline" # color for PWD
  1109. __my_c2="$__attr_bold$__attr_underline" # color for user and hostname
  1110. # color for ::
  1111. case "`hostname`" in
  1112. arch-aspireone)
  1113. __my_c4="$__color_yellow"
  1114. ;;
  1115. arch-mba)
  1116. __my_c4="$__color_light_cyan"
  1117. ;;
  1118. newkiwi)
  1119. __my_c4="$__color_light_purple"
  1120. ;;
  1121. debian-vb-win7-opti)
  1122. __my_c4="$__color_light_red"
  1123. ;;
  1124. *)
  1125. __my_c4="$__color_light_green"
  1126. ;;
  1127. esac
  1128. __my_c5="$__color_black$__color_bg_light_gray" # color for SCR
  1129. __my_cdef="$__color_default"
  1130. fi
  1131. if $inbash
  1132. then
  1133. __my_ps1_sh="[BASH:$BASH_VERSION]"
  1134. elif $inzsh
  1135. then
  1136. __my_ps1_sh="[ZSH:$ZSH_VERSION]"
  1137. fi
  1138. __my_ps1_info1(){
  1139. # first line of PS1
  1140. echo "${__my_ps1_sh}$(__my_ps1_scale)$(__my_ps1_git)$(__my_ps1_bttry)$(__my_ps1_moc)"
  1141. }
  1142. test -n "$__MY_SCRIPT" && \
  1143. __my_ps1_str_scr="SCR"
  1144. test -n "$SSH_CONNECTION" && \
  1145. __my_ps1_str_ssh="SSH"
  1146. test -n "$__MY_DTACH" && \
  1147. __my_ps1_str_dt="DT:`basename "$__MY_DTACH$"`"
  1148. __my_ps1_info2(){
  1149. # second line of PS1
  1150. echo $(__my_ps1_memo) $(__my_ps1_jobs) ${__my_ps1_str_scr} \
  1151. ${__my_ps1_str_ssh} ${__my_ps1_str_dt} $(__my_alert_fail) \
  1152. | sed -e 's/ /|/g'
  1153. }
  1154. __my_ps1_beg="${__my_c4}:: ${__my_cdef}"
  1155. __my_ps1_save_pos="\[\033[s\]"
  1156. __my_ps1_restore_pos="\[\033[u\]"
  1157. __my_ps1_move_rightmost="\[\033[\$(tput cols)C\]"
  1158. __my_ps1_move_15left="\[\033[15D\]"
  1159. # collapse when command line is too long and try to write over this string
  1160. # __my_ps1_right="${__my_ps1_save_pos}${__my_ps1_move_rightmost}"
  1161. # ${__my_ps1_move_15left}\D{%Y/%m/%d %H:%M}${__my_ps1_restore_pos}
  1162. if $inzsh
  1163. then
  1164. PROMPT="\
  1165. ${__my_ps1_beg}[${__my_c2}%n@%M${__my_cdef}:${__my_c1}%~/${__my_cdef}]\$(__my_ps1_info1)
  1166. ${__my_ps1_beg}\$(__my_ps1_info2) $(__my_ps1_dollar %#) "
  1167. RPROMPT="%D{%Y/%m/%d %H:%M}"
  1168. elif $inbash
  1169. then
  1170. PS1="\
  1171. ${__my_ps1_beg}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_info1)\n\
  1172. ${__my_ps1_beg}\D{%Y/%m/%d %H:%M} \$(__my_ps1_info2)${__my_ps1_right} $(__my_ps1_dollar \\$) "
  1173. else
  1174. true
  1175. # PS1="$(printf $(whoami)@$(hostname)$ )"
  1176. fi
  1177. ###################################
  1178. # set header and titles
  1179. __my_set_header_line(){
  1180. # save current position
  1181. printf "\033[s"
  1182. # move to 0,0
  1183. printf "\033[0;0H"
  1184. # clear curent to eol
  1185. printf "\033[K"
  1186. # inverse color
  1187. printf "\033[7m"
  1188. printf "$1"
  1189. # restore color
  1190. printf "\033[0m"
  1191. # restore saved position
  1192. printf "\033[u"
  1193. }
  1194. __my_set_screen_name(){
  1195. # set window name
  1196. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  1197. then
  1198. echo -ne "\033k$1\033\\"
  1199. fi
  1200. }
  1201. __my_set_title(){
  1202. case $TERM in
  1203. (rxvt*|xterm*|aterm|screen*)
  1204. test -t 1 &&
  1205. test -z "$EMACS" &&
  1206. echo -n -e "\033]0;$1\007"
  1207. ;;
  1208. esac
  1209. }
  1210. if test -n "$TMUX"
  1211. then
  1212. # running tmux locally
  1213. __terminal_title="\$(basename \${PWD})"
  1214. elif test -n "$SSH_CONNECTION" && expr "$TERM" : '^screen' >/dev/null
  1215. then
  1216. # ssh connect from tmux terminal
  1217. __terminal_title="`whoami`@`hostname`:\$(basename \${PWD})"
  1218. else
  1219. __terminal_title="`whoami`@`hostname`:\${PWD}"
  1220. fi
  1221. if $inzsh
  1222. then
  1223. precmd(){
  1224. laststatus=$?
  1225. eval __my_set_title ${__terminal_title}
  1226. }
  1227. else
  1228. PROMPT_COMMAND="laststatus=\$?;__my_set_title \"${__terminal_title}\";$PROMPT_COMMAND"
  1229. fi
  1230. laststatus=0