Commit 6fd0b73e authored by zghember's avatar zghember
Browse files

Merge commit '141c2e59'

parents 6f70d288 141c2e59
...@@ -11,20 +11,22 @@ fi ...@@ -11,20 +11,22 @@ fi
local DOC='scd -- smart change to a recently used directory local DOC='scd -- smart change to a recently used directory
usage: scd [options] [pattern1 pattern2 ...] usage: scd [options] [pattern1 pattern2 ...]
Go to a directory path that contains all fixed string patterns. Prefer Go to a directory path that contains all fixed string patterns. Prefer
recently visited directories and directories with patterns in their tail recent or frequently visited directories as found in the directory index.
component. Display a selection menu in case of multiple matches. Display a selection menu in case of multiple matches.
Options: Options:
-a, --add add specified directories to the directory index -a, --add add specified directories to the directory index.
--unindex remove specified directories from the index --unindex remove current or specified directories from the index.
-r, --recursive apply options --add or --unindex recursively -r, --recursive apply options --add or --unindex recursively.
--alias=ALIAS create alias for the current or specified directory and --alias=ALIAS create alias for the current or specified directory and
store it in ~/.scdalias.zsh store it in ~/.scdalias.zsh.
--unalias remove ALIAS definition for the current or specified --unalias remove ALIAS definition for the current or specified
directory from ~/.scdalias.zsh directory from ~/.scdalias.zsh.
--list show matching directories and exit -A, --all include all matching directories. Disregard matching by
-v, --verbose display directory rank in the selection menu directory alias and filtering of less likely paths.
-h, --help display this message and exit --list show matching directories and exit.
-v, --verbose display directory rank in the selection menu.
-h, --help display this message and exit.
' '
local SCD_HISTFILE=${SCD_HISTFILE:-${HOME}/.scdhistory} local SCD_HISTFILE=${SCD_HISTFILE:-${HOME}/.scdhistory}
...@@ -35,9 +37,9 @@ local SCD_THRESHOLD=${SCD_THRESHOLD:-0.005} ...@@ -35,9 +37,9 @@ local SCD_THRESHOLD=${SCD_THRESHOLD:-0.005}
local SCD_SCRIPT=${RUNNING_AS_COMMAND:+$SCD_SCRIPT} local SCD_SCRIPT=${RUNNING_AS_COMMAND:+$SCD_SCRIPT}
local SCD_ALIAS=~/.scdalias.zsh local SCD_ALIAS=~/.scdalias.zsh
local ICASE a d m p i tdir maxrank threshold local ICASE a d m p i maxrank threshold
local opt_help opt_add opt_unindex opt_recursive opt_verbose local opt_help opt_add opt_unindex opt_recursive opt_verbose
local opt_alias opt_unalias opt_list local opt_alias opt_unalias opt_all opt_list
local -A drank dalias local -A drank dalias
local dmatching local dmatching
local last_directory local last_directory
...@@ -56,7 +58,8 @@ zmodload -i zsh/zutil ...@@ -56,7 +58,8 @@ zmodload -i zsh/zutil
zmodload -i zsh/datetime zmodload -i zsh/datetime
zparseopts -D -- a=opt_add -add=opt_add -unindex=opt_unindex \ zparseopts -D -- a=opt_add -add=opt_add -unindex=opt_unindex \
r=opt_recursive -recursive=opt_recursive \ r=opt_recursive -recursive=opt_recursive \
-alias:=opt_alias -unalias=opt_unalias -list=opt_list \ -alias:=opt_alias -unalias=opt_unalias \
A=opt_all -all=opt_all -list=opt_list \
v=opt_verbose -verbose=opt_verbose h=opt_help -help=opt_help \ v=opt_verbose -verbose=opt_verbose h=opt_help -help=opt_help \
|| $EXIT $? || $EXIT $?
...@@ -68,6 +71,11 @@ fi ...@@ -68,6 +71,11 @@ fi
# load directory aliases if they exist # load directory aliases if they exist
[[ -r $SCD_ALIAS ]] && source $SCD_ALIAS [[ -r $SCD_ALIAS ]] && source $SCD_ALIAS
# Private internal functions are prefixed with _scd_Y19oug_.
# Clean them up when the scd function returns.
setopt localtraps
trap 'unfunction -m "_scd_Y19oug_*"' EXIT
# works faster than the (:a) modifier and is compatible with zsh 4.2.6 # works faster than the (:a) modifier and is compatible with zsh 4.2.6
_scd_Y19oug_abspath() { _scd_Y19oug_abspath() {
set -A $1 ${(ps:\0:)"$( set -A $1 ${(ps:\0:)"$(
...@@ -123,11 +131,52 @@ if [[ -n $opt_unalias ]]; then ...@@ -123,11 +131,52 @@ if [[ -n $opt_unalias ]]; then
$EXIT $? $EXIT $?
fi fi
# The "compress" function collapses repeated directories to
# one entry with a time stamp that gives equivalent-probability.
_scd_Y19oug_compress() {
awk -v epochseconds=$EPOCHSECONDS -v meanlife=$SCD_MEANLIFE '
BEGIN { FS = "[:;]"; }
length($0) < 4096 && $2 > 0 {
tau = 1.0 * ($2 - epochseconds) / meanlife;
if (tau < -6.9078) tau = -6.9078;
prob = exp(tau);
sub(/^[^;]*;/, "");
if (NF) {
dlist[last[$0]] = "";
dlist[NR] = $0;
last[$0] = NR;
ptot[$0] += prob;
}
}
END {
for (i = 1; i <= NR; ++i) {
d = dlist[i];
if (d) {
ts = log(ptot[d]) * meanlife + epochseconds;
printf(": %.0f:0;%s\n", ts, d);
}
}
}
' $*
}
# Rewrite directory index if it is at least 20% oversized # Rewrite directory index if it is at least 20% oversized
if [[ -s $SCD_HISTFILE ]] && \ if [[ -s $SCD_HISTFILE ]] && \
(( $(wc -l <$SCD_HISTFILE) > 1.2 * $SCD_HISTSIZE )); then (( $(wc -l <$SCD_HISTFILE) > 1.2 * $SCD_HISTSIZE )); then
m=( ${(f)"$(<$SCD_HISTFILE)"} ) # compress repeated entries
print -lr -- ${m[-$SCD_HISTSIZE,-1]} >| ${SCD_HISTFILE} m=( ${(f)"$(_scd_Y19oug_compress $SCD_HISTFILE)"} )
# purge non-existent directories
m=( ${(f)"$(
for a in $m; do
if [[ -d ${a#*;} ]]; then print -r -- $a; fi
done
)"}
)
# cut old entries if still oversized
if [[ $#m -gt $SCD_HISTSIZE ]]; then
m=( ${m[-$SCD_HISTSIZE,-1]} )
fi
print -lr -- $m >| ${SCD_HISTFILE}
fi fi
# Determine the last recorded directory # Determine the last recorded directory
...@@ -135,7 +184,6 @@ if [[ -s ${SCD_HISTFILE} ]]; then ...@@ -135,7 +184,6 @@ if [[ -s ${SCD_HISTFILE} ]]; then
last_directory=${"$(tail -1 ${SCD_HISTFILE})"#*;} last_directory=${"$(tail -1 ${SCD_HISTFILE})"#*;}
fi fi
# Internal functions are prefixed with "_scd_Y19oug_".
# The "record" function adds its arguments to the directory index. # The "record" function adds its arguments to the directory index.
_scd_Y19oug_record() { _scd_Y19oug_record() {
while [[ -n $last_directory && $1 == $last_directory ]]; do while [[ -n $last_directory && $1 == $last_directory ]]; do
...@@ -217,7 +265,7 @@ _scd_Y19oug_action() { ...@@ -217,7 +265,7 @@ _scd_Y19oug_action() {
# set global arrays dmatching and drank # set global arrays dmatching and drank
_scd_Y19oug_match() { _scd_Y19oug_match() {
## single argument that is an existing directory or directory alias ## single argument that is an existing directory or directory alias
if [[ $# == 1 ]] && \ if [[ -z $opt_all && $# == 1 ]] && \
[[ -d ${d::=$1} || -d ${d::=${nameddirs[$1]}} ]] && [[ -x $d ]]; [[ -d ${d::=$1} || -d ${d::=${nameddirs[$1]}} ]] && [[ -x $d ]];
then then
_scd_Y19oug_abspath dmatching $d _scd_Y19oug_abspath dmatching $d
...@@ -227,6 +275,8 @@ _scd_Y19oug_match() { ...@@ -227,6 +275,8 @@ _scd_Y19oug_match() {
# ignore case unless there is an argument with an uppercase letter # ignore case unless there is an argument with an uppercase letter
[[ "$*" == *[[:upper:]]* ]] || ICASE='(#i)' [[ "$*" == *[[:upper:]]* ]] || ICASE='(#i)'
# support "$" as an anchor for the directory name ending
argv=( ${argv/(#m)?[$](#e)/${MATCH[1]}(#e)} )
# calculate rank of all directories in the SCD_HISTFILE and keep it as drank # calculate rank of all directories in the SCD_HISTFILE and keep it as drank
# include a dummy entry for splitting of an empty string is buggy # include a dummy entry for splitting of an empty string is buggy
...@@ -237,10 +287,10 @@ _scd_Y19oug_match() { ...@@ -237,10 +287,10 @@ _scd_Y19oug_match() {
BEGIN { FS = "[:;]"; } BEGIN { FS = "[:;]"; }
length($0) < 4096 && $2 > 0 { length($0) < 4096 && $2 > 0 {
tau = 1.0 * ($2 - epochseconds) / meanlife; tau = 1.0 * ($2 - epochseconds) / meanlife;
if (tau < -4.61) tau = -4.61; if (tau < -6.9078) tau = -6.9078;
prec = exp(tau); prob = exp(tau);
sub(/^[^;]*;/, ""); sub(/^[^;]*;/, "");
if (NF) ptot[$0] += prec; if (NF) ptot[$0] += prob;
} }
END { for (di in ptot) { print di; print ptot[di]; } }' END { for (di in ptot) { print di; print ptot[di]; } }'
)"} )"}
...@@ -249,9 +299,12 @@ _scd_Y19oug_match() { ...@@ -249,9 +299,12 @@ _scd_Y19oug_match() {
# filter drank to the entries that match all arguments # filter drank to the entries that match all arguments
for a; do for a; do
p=${ICASE}"*${a}*" p=${ICASE}"*(${a})*"
drank=( ${(kv)drank[(I)${~p}]} ) drank=( ${(kv)drank[(I)${~p}]} )
done done
# require at least one argument matches the directory name
p=${ICASE}"*(${(j:|:)argv})[^/]#"
drank=( ${(kv)drank[(I)${~p}]} )
# build a list of matching directories reverse-sorted by their probabilities # build a list of matching directories reverse-sorted by their probabilities
dmatching=( ${(f)"$( dmatching=( ${(f)"$(
...@@ -261,26 +314,6 @@ _scd_Y19oug_match() { ...@@ -261,26 +314,6 @@ _scd_Y19oug_match() {
)"} )"}
) )
# if some directory paths match all patterns in order, discard all others
p=${ICASE}"*${(j:*:)argv}*"
m=( ${(M)dmatching:#${~p}} )
[[ -d ${m[1]} ]] && dmatching=( $m )
# if some directory names match last pattern, discard all others
p=${ICASE}"*${(j:*:)argv}[^/]#"
m=( ${(M)dmatching:#${~p}} )
[[ -d ${m[1]} ]] && dmatching=( $m )
# if some directory names match all patterns, discard all others
m=( $dmatching )
for a; do
p=${ICASE}"*/[^/]#${a}[^/]#"
m=( ${(M)m:#${~p}} )
done
[[ -d ${m[1]} ]] && dmatching=( $m )
# if some directory names match all patterns in order, discard all others
p=${ICASE}"/*${(j:[^/]#:)argv}[^/]#"
m=( ${(M)dmatching:#${~p}} )
[[ -d ${m[1]} ]] && dmatching=( $m )
# do not match $HOME or $PWD when run without arguments # do not match $HOME or $PWD when run without arguments
if [[ $# == 0 ]]; then if [[ $# == 0 ]]; then
dmatching=( ${dmatching:#(${HOME}|${PWD})} ) dmatching=( ${dmatching:#(${HOME}|${PWD})} )
...@@ -302,6 +335,9 @@ _scd_Y19oug_match() { ...@@ -302,6 +335,9 @@ _scd_Y19oug_match() {
# discard all directories below the rank threshold # discard all directories below the rank threshold
threshold=$(( maxrank * SCD_THRESHOLD )) threshold=$(( maxrank * SCD_THRESHOLD ))
if [[ -n ${opt_all} ]]; then
threshold=0
fi
dmatching=( ${^dmatching}(Ne:'(( ${drank[$REPLY]} >= threshold ))':) ) dmatching=( ${^dmatching}(Ne:'(( ${drank[$REPLY]} >= threshold ))':) )
} }
...@@ -339,6 +375,7 @@ fi ...@@ -339,6 +375,7 @@ fi
## here we have multiple matches - display selection menu ## here we have multiple matches - display selection menu
a=( {a-z} {A-Z} ) a=( {a-z} {A-Z} )
a=( ${a[1,${#dmatching}]} )
p=( ) p=( )
for i in {1..${#dmatching}}; do for i in {1..${#dmatching}}; do
[[ -n ${a[i]} ]] || break [[ -n ${a[i]} ]] || break
......
...@@ -77,7 +77,7 @@ function svn_dirty() { ...@@ -77,7 +77,7 @@ function svn_dirty() {
function svn_dirty_choose_pwd () { function svn_dirty_choose_pwd () {
if in_svn; then if in_svn; then
root=`pwd` root=$PWD
if $(svn status $root 2> /dev/null | grep -Eq '^\s*[ACDIM!?L]'); then if $(svn status $root 2> /dev/null | grep -Eq '^\s*[ACDIM!?L]'); then
# Grep exits with 0 when "One or more lines were selected", return "dirty". # Grep exits with 0 when "One or more lines were selected", return "dirty".
echo $1 echo $1
......
...@@ -30,7 +30,7 @@ function retlog() { ...@@ -30,7 +30,7 @@ function retlog() {
} }
alias ping='ping -c 5' alias ping='ping -c 5'
alias clr='clear;echo "Currently logged in on $(tty), as $(whoami) in directory $(pwd)."' alias clr='clear;echo "Currently logged in on $(tty), as $USER in directory $PWD."'
alias path='echo -e ${PATH//:/\\n}' alias path='echo -e ${PATH//:/\\n}'
alias mkdir='mkdir -pv' alias mkdir='mkdir -pv'
# get top process eating memory # get top process eating memory
......
...@@ -9,7 +9,7 @@ function resolveFile ...@@ -9,7 +9,7 @@ function resolveFile
if [ -f "$1" ]; then if [ -f "$1" ]; then
echo $(readlink -f "$1") echo $(readlink -f "$1")
elif [[ "${1#/}" == "$1" ]]; then elif [[ "${1#/}" == "$1" ]]; then
echo "$(pwd)/$1" echo "$PWD/$1"
else else
echo $1 echo $1
fi fi
......
function virtualenv_prompt_info(){ function virtualenv_prompt_info(){
if [[ -n $VIRTUAL_ENV ]]; then [[ -n ${VIRTUAL_ENV} ]] || return
printf "%s[%s] " "%{${fg[yellow]}%}" ${${VIRTUAL_ENV}:t} echo "${ZSH_THEME_VIRTUALENV_PREFIX:=[}${VIRTUAL_ENV:t}${ZSH_THEME_VIRTUALENV_SUFFIX:=]}"
fi
} }
# disables prompt mangling in virtual_env/bin/activate # disables prompt mangling in virtual_env/bin/activate
......
## wd wd
==
[![Build Status](https://travis-ci.org/mfaerevaag/wd.png?branch=master)](https://travis-ci.org/mfaerevaag/wd)
`wd` (*warp directory*) lets you jump to custom directories in zsh, without using `cd`. Why? Because `cd` seems ineffecient when the folder is frequently visited or has a long path.
### Setup
### oh-my-zsh
`wd` comes bundles with [oh-my-zshell](https://github.com/robbyrussell/oh-my-zsh)!
Just add the plugin in your `~/.zshrc` file:
plugins=(... wd)
#### Automatic
Run either in terminal:
* `curl -L https://github.com/mfaerevaag/wd/raw/master/install.sh | sh`
* `wget --no-check-certificate https://github.com/mfaerevaag/wd/raw/master/install.sh -O - | sh`
#### Manual
* Clone this repo to your liking
* Add `wd` function to `.zshrc` (or `.profile` etc.):
wd() {
. ~/paht/to/wd/wd.sh
}
* Install manpage. From `wd`'s base directory (requires root permissions):
# cp wd.1 /usr/share/man/man1/wd.1
# chmod 644 /usr/share/man/man1/wd.1
Note, when pulling and updating `wd`, you'll need to do this again in case of changes to the manpage.
#### Completion
If you're NOT using [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh) and you want to utelize the zsh-completion feature, you will also need to add the path to your `wd` installation (`~/bin/wd` if you used the automatic installer) to your `fpath`. E.g. in your `~/.zshrc`:
fpath=(~/path/to/wd $fpath)
Also, you may have to force a rebuild of `zcompdump` by running:
$ rm -f ~/.zcompdump; compinit
**Maintainer:** [mfaerevaag](https://github.com/mfaerevaag)
`wd` (*warp directory*) lets you jump to custom directories in zsh, without using `cd`. Why? Because `cd` seems ineffecient when the folder is frequently visited or has a long path. [Source](https://github.com/mfaerevaag/wd)
### Usage ### Usage
...@@ -24,6 +76,7 @@ ...@@ -24,6 +76,7 @@
$ wd ... $ wd ...
This is a wrapper for the zsh `dirs` function. This is a wrapper for the zsh `dirs` function.
(You might need `setopt AUTO_PUSHD` in your `.zshrc` if you hare not using [oh-my-zshell](https://github.com/robbyrussell/oh-my-zsh)).
* Remove warp point test point: * Remove warp point test point:
...@@ -33,8 +86,55 @@ ...@@ -33,8 +86,55 @@
$ wd ls $ wd ls
* List warp points to current directory * List warp points to current directory, or optionally, path to given warp point:
$ wd show $ wd show
* Print usage with no opts or the `help` argument. * Remove warp points to non-existent directories.
$ wd clean
Use `clean!` to not be prompted with confirmation (force).
* Print usage with no opts or the `help` argument:
$ wd help
* Print the running version of `wd`:
$ wd --version
* Specifically set the config file (default `~/.warprc`), which is useful when testing:
$ wd --config ./file <action>
* Force `exit` with return code after running. This is not default, as it will *exit your terminal*, though required when testing/debugging.
$ wd --debug <action>
* Silence all output:
$ wd --quiet <action>
### Testing
`wd` comes with a small test suite, run with [shunit2](https://code.google.com/p/shunit2/). This can be used to confirm that things are working as it should on your setup, or to demonstrate an issue.
To run, simply `cd` into the `test` directory and run the `tests.sh`.
$ ./tests.sh
### License
The project is licensed under the [MIT-license](https://github.com/mfaerevaag/wd/blob/master/LICENSE).
### Finally
If you have issues, feedback or improvements, don't hesitate to report it or submit a pull-request. In the case of an issue, we would much appreciate if you would include a failing test in `test/tests.sh`. Explanation on how to run the tests, read the section "Testing" in this README.
Credit to [altschuler](https://github.com/altschuler) for awesome idea.
Hope you enjoy!
...@@ -21,7 +21,8 @@ function _wd() { ...@@ -21,7 +21,8 @@ function _wd() {
'add!:Overwrites existing warp point' 'add!:Overwrites existing warp point'
'rm:Removes the given warp point' 'rm:Removes the given warp point'
'ls:Outputs all stored warp points' 'ls:Outputs all stored warp points'
'show:Outputs all warp points that point to the current directory' 'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point'
'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point'
'help:Show this extremely helpful text' 'help:Show this extremely helpful text'
'..:Go back to last directory' '..:Go back to last directory'
) )
...@@ -43,6 +44,9 @@ function _wd() { ...@@ -43,6 +44,9 @@ function _wd() {
add) add)
_message 'Write the name of your warp point' && ret=0 _message 'Write the name of your warp point' && ret=0
;; ;;
show)
_describe -t points "Warp points" warp_points && ret=0
;;
esac esac
;; ;;
esac esac
......
...@@ -7,40 +7,108 @@ ...@@ -7,40 +7,108 @@
# #
# @github.com/mfaerevaag/wd # @github.com/mfaerevaag/wd
# version
## variables readonly WD_VERSION=0.4
readonly CONFIG=$HOME/.warprc
# colors # colors
readonly BLUE="\033[96m" readonly WD_BLUE="\033[96m"
readonly GREEN="\033[92m" readonly WD_GREEN="\033[92m"
readonly YELLOW="\033[93m" readonly WD_YELLOW="\033[93m"
readonly RED="\033[91m" readonly WD_RED="\033[91m"
readonly NOC="\033[m" readonly WD_NOC="\033[m"
## functions
## init # helpers
wd_yesorno()
{
# variables
local question="${1}"
local prompt="${question} "
local yes_RETVAL="0"
local no_RETVAL="3"
local RETVAL=""
local answer=""
# read-eval loop
while true ; do
printf $prompt
read -r answer
case ${answer:=${default}} in
Y|y|YES|yes|Yes )
RETVAL=${yes_RETVAL} && \
break
;;
N|n|NO|no|No )
RETVAL=${no_RETVAL} && \
break
;;
* )
echo "Please provide a valid answer (y or n)"
;;
esac
done
# check if config file exists return ${RETVAL}
if [ ! -e $CONFIG ] }
then
# if not, create config file
touch $CONFIG
fi
# load warp points wd_print_msg()
typeset -A points {
while read -r line if [[ -z $wd_quiet_mode ]]
do then
arr=(${(s,:,)line}) local color=$1
key=${arr[1]} local msg=$2
val=${arr[2]}
points[$key]=$val if [[ $color == "" || $msg == "" ]]
done < $CONFIG then
print " ${WD_RED}*${WD_NOC} Could not print message. Sorry!"
else
print " ${color}*${WD_NOC} ${msg}"
fi
fi
}
wd_print_usage()
{
cat <<- EOF
Usage: wd [command] <point>
## functions Commands:
add <point> Adds the current working directory to your warp points
add! <point> Overwrites existing warp point
rm <point> Removes the given warp point
show Print warp points to current directory
show <point> Print path to given warp point
ls Print all stored warp points
clean! Remove points warping to nonexistent directories
-v | --version Print version
-d | --debug Exit after execution with exit codes (for testing)
-c | --config Specify config file (default ~/.warprc)
-q | --quiet Suppress all output
help Show this extremely helpful text
EOF
}
wd_exit_fail()
{
local msg=$1
wd_print_msg $WD_RED $1
WD_EXIT_CODE=1
}
wd_exit_warn()
{
local msg=$1
wd_print_msg $WD_YELLOW $msg
WD_EXIT_CODE=1
}
# core
wd_warp() wd_warp()
{ {
...@@ -50,7 +118,7 @@ wd_warp() ...@@ -50,7 +118,7 @@ wd_warp()
then then
if [ $#1 < 2 ] if [ $#1 < 2 ]
then then
wd_print_msg $YELLOW "Warping to current directory?" wd_exit_warn "Warping to current directory?"
else else
(( n = $#1 - 1 )) (( n = $#1 - 1 ))
cd -$n > /dev/null cd -$n > /dev/null
...@@ -59,7 +127,7 @@ wd_warp() ...@@ -59,7 +127,7 @@ wd_warp()
then then
cd ${points[$point]} cd ${points[$point]}
else else
wd_print_msg $RED "Unknown warp point '${point}'" wd_exit_fail "Unknown warp point '${point}'"
fi fi
} }
...@@ -70,24 +138,28 @@ wd_add() ...@@ -70,24 +138,28 @@ wd_add()
if [[ $point =~ "^[\.]+$" ]] if [[ $point =~ "^[\.]+$" ]]
then then
wd_print_msg $RED "Warp point cannot be just dots" wd_exit_fail "Warp point cannot be just dots"
elif [[ $point =~ "(\s|\ )+" ]] elif [[ $point =~ "[[:space:]]+" ]]
then then
wd_print_msg $RED "Warp point should not contain whitespace" wd_exit_fail "Warp point should not contain whitespace"
elif [[ $point == *:* ]] elif [[ $point == *:* ]]
then then
wd_print_msg $RED "Warp point cannot contain colons" wd_exit_fail "Warp point cannot contain colons"
elif [[ $point == "" ]] elif [[ $point == "" ]]
then then
wd_print_msg $RED "Warp point cannot be empty" wd_exit_fail "Warp point cannot be empty"
elif [[ ${points[$2]} == "" ]] || $force elif [[ ${points[$2]} == "" ]] || $force
then then
wd_remove $point > /dev/null wd_remove $point > /dev/null
printf "%q:%q\n" "${point}" "${PWD}" >> $CONFIG printf "%q:%s\n" "${point}" "${PWD}" >> $WD_CONFIG
wd_print_msg $WD_GREEN "Warp point added"
wd_print_msg $GREEN "Warp point added" # override exit code in case wd_remove did not remove any points
# TODO: we should handle this kind of logic better
WD_EXIT_CODE=0
else else
wd_print_msg $YELLOW "Warp point '${point}' already exists. Use 'add!' to overwrite." wd_exit_warn "Warp point '${point}' already exists. Use 'add!' to overwrite."
fi fi
} }
...@@ -97,20 +169,21 @@ wd_remove() ...@@ -97,20 +169,21 @@ wd_remove()
if [[ ${points[$point]} != "" ]] if [[ ${points[$point]} != "" ]]
then then
if sed -i.bak "s,^${point}:.*$,,g" $CONFIG local config_tmp=$WD_CONFIG.tmp
if sed -n "/^${point}:.*$/!p" $WD_CONFIG > $config_tmp && mv $config_tmp $WD_CONFIG
then then
wd_print_msg $GREEN "Warp point removed" wd_print_msg $WD_GREEN "Warp point removed"
else else
wd_print_msg $RED "Something bad happened! Sorry." wd_exit_fail "Something bad happened! Sorry."
fi fi
else else
wd_print_msg $RED "Warp point was not found" wd_exit_fail "Warp point was not found"
fi fi
} }
wd_list_all() wd_list_all()
{ {
wd_print_msg $BLUE "All warp points:" wd_print_msg $WD_BLUE "All warp points:"
while IFS= read -r line while IFS= read -r line
do do
...@@ -120,66 +193,146 @@ wd_list_all() ...@@ -120,66 +193,146 @@ wd_list_all()
key=${arr[1]} key=${arr[1]}
val=${arr[2]} val=${arr[2]}
printf "%20s -> %s\n" $key $val if [[ -z $wd_quiet_mode ]]
then
printf "%20s -> %s\n" $key $val
fi
fi fi
done <<< $(sed "s:${HOME}:~:g" $CONFIG) done <<< $(sed "s:${HOME}:~:g" $WD_CONFIG)
} }
wd_show() wd_show()
{ {
local cwd=$(print $PWD | sed "s:^${HOME}:~:") local name_arg=$1
# if there's an argument we look up the value
wd_print_msg $BLUE "Warp points to current directory:" if [[ ! -z $name_arg ]]
wd_list_all | grep -e "${cwd}$" then
if [[ -z $points[$name_arg] ]]
then
wd_print_msg $WD_BLUE "No warp point named $name_arg"
else
wd_print_msg $WD_GREEN "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
fi
else
# hax to create a local empty array
local wd_matches
wd_matches=()
# do a reverse lookup to check whether PWD is in $points
if [[ ${points[(r)$PWD]} == $PWD ]]
then
for name in ${(k)points}
do
if [[ $points[$name] == $PWD ]]
then
wd_matches[$(($#wd_matches+1))]=$name
fi
done
wd_print_msg $WD_BLUE "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
else
wd_print_msg $WD_YELLOW "No warp point to $(echo $PWD | sed "s:$HOME:~:")"
fi
fi
} }
wd_print_msg() wd_clean() {
{ local force=$1
local color=$1 local count=0
local msg=$2 local wd_tmp=""
while read line
do
if [[ $line != "" ]]
then
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
if [[ $color == "" || $msg == "" ]] if [ -d "$val" ]
then
wd_tmp=$wd_tmp"\n"`echo $line`
else
wd_print_msg $WD_YELLOW "Nonexistent directory: ${key} -> ${val}"
count=$((count+1))
fi
fi
done < $WD_CONFIG
if [[ $count -eq 0 ]]
then then
print " ${RED}*${NOC} Could not print message. Sorry!" wd_print_msg $WD_BLUE "No warp points to clean, carry on!"
else else
print " ${color}*${NOC} ${msg}" if $force || wd_yesorno "Removing ${count} warp points. Continue? (Y/n)"
then
echo $wd_tmp >! $WD_CONFIG
wd_print_msg $WD_GREEN "Cleanup complete. ${count} warp point(s) removed"
else
wd_print_msg $WD_BLUE "Cleanup aborted"
fi
fi fi
} }
wd_print_usage() local WD_CONFIG=$HOME/.warprc
{ local WD_QUIET=0
cat <<- EOF local WD_EXIT_CODE=0
Usage: wd [add|-a|--add] [rm|-r|--remove] <point> local WD_DEBUG=0
Commands: # Parse 'meta' options first to avoid the need to have them before
add Adds the current working directory to your warp points # other commands. The `-D` flag consumes recognized options so that
add! Overwrites existing warp point # the actual command parsing won't be affected.
rm Removes the given warp point
show Outputs warp points to current directory
ls Outputs all stored warp points
help Show this extremely helpful text
EOF
}
zparseopts -D -E \
c:=wd_alt_config -config:=wd_alt_config \
q=wd_quiet_mode -quiet=wd_quiet_mode \
v=wd_print_version -version=wd_print_version \
d=wd_debug_mode -debug=wd_debug_mode
## run if [[ ! -z $wd_print_version ]]
then
echo "wd version $WD_VERSION"
fi
if [[ ! -z $wd_alt_config ]]
then
WD_CONFIG=$wd_alt_config[2]
fi
# check if config file exists
if [ ! -e $WD_CONFIG ]
then
# if not, create config file
touch $WD_CONFIG
fi
# load warp points
typeset -A points
while read -r line
do
arr=(${(s,:,)line})
key=${arr[1]}
val=${arr[2]}
points[$key]=$val
done < $WD_CONFIG
# get opts # get opts
args=$(getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*) args=$(getopt -o a:r:c:lhs -l add:,rm:,clean\!,ls,help,show -- $*)
# check if no arguments were given # check if no arguments were given, and that version is not set
if [[ $? -ne 0 || $#* -eq 0 ]] if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
then then
wd_print_usage wd_print_usage
# check if config file is writeable # check if config file is writeable
elif [ ! -w $CONFIG ] elif [ ! -w $WD_CONFIG ]
then then
# do nothing # do nothing
# can't run `exit`, as this would exit the executing shell # can't run `exit`, as this would exit the executing shell
wd_print_msg $RED "\'$CONFIG\' is not writeable." wd_exit_fail "\'$WD_CONFIG\' is not writeable."
else else
# parse rest of options
for o for o
do do
case "$o" case "$o"
...@@ -205,7 +358,15 @@ else ...@@ -205,7 +358,15 @@ else
break break
;; ;;
-s|--show|show) -s|--show|show)
wd_show wd_show $2
break
;;
-c|--clean|clean)
wd_clean false
break
;;
-c!|--clean!|clean!)
wd_clean true
break break
;; ;;
*) *)
...@@ -229,8 +390,19 @@ unset wd_remove ...@@ -229,8 +390,19 @@ unset wd_remove
unset wd_show unset wd_show
unset wd_list_all unset wd_list_all
unset wd_print_msg unset wd_print_msg
unset wd_yesorno
unset wd_print_usage unset wd_print_usage
unset wd_alt_config
unset wd_quiet_mode
unset wd_print_version
unset args unset args
unset points unset points
unset val &> /dev/null # fixes issue #1 unset val &> /dev/null # fixes issue #1
if [[ ! -z $wd_debug_mode ]]
then
exit $WD_EXIT_CODE
else
unset wd_debug_mode
fi
...@@ -16,4 +16,9 @@ function xcsel { ...@@ -16,4 +16,9 @@ function xcsel {
alias xcb='xcodebuild' alias xcb='xcodebuild'
alias xcp='xcode-select --print-path' alias xcp='xcode-select --print-path'
alias simulator='open $(xcode-select -p)/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app'
if [[ -d $(xcode-select -p)/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app ]]; then
alias simulator='open $(xcode-select -p)/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app'
else
alias simulator='open $(xcode-select -p)/Applications/iOS\ Simulator.app'
fi
...@@ -47,13 +47,13 @@ ZSH_THEME="robbyrussell" ...@@ -47,13 +47,13 @@ ZSH_THEME="robbyrussell"
# Add wisely, as too many plugins slow down shell startup. # Add wisely, as too many plugins slow down shell startup.
plugins=(git) plugins=(git)
source $ZSH/oh-my-zsh.sh
# User configuration # User configuration
export PATH=$HOME/bin:/usr/local/bin:$PATH export PATH=$HOME/bin:/usr/local/bin:$PATH
# export MANPATH="/usr/local/man:$MANPATH" # export MANPATH="/usr/local/man:$MANPATH"
source $ZSH/oh-my-zsh.sh
# You may need to manually set your language environment # You may need to manually set your language environment
# export LANG=en_US.UTF-8 # export LANG=en_US.UTF-8
......
...@@ -89,9 +89,8 @@ function precmd { ...@@ -89,9 +89,8 @@ function precmd {
# Context: user@directory or just directory # Context: user@directory or just directory
prompt_context () { prompt_context () {
local user=`whoami` if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then echo -n "${PR_RESET}${PR_RED}$USER@%m${PR_RESET}${PR_BRIGHT_YELLOW}%~%<<${PR_RESET}"
echo -n "${PR_RESET}${PR_RED}$user@%m${PR_RESET}${PR_BRIGHT_YELLOW}%~%<<${PR_RESET}"
else else
echo -n "${PR_RESET}${PR_BRIGHT_YELLOW}%~%<<${PR_RESET}" echo -n "${PR_RESET}${PR_BRIGHT_YELLOW}%~%<<${PR_RESET}"
fi fi
......
...@@ -60,10 +60,8 @@ prompt_end() { ...@@ -60,10 +60,8 @@ prompt_end() {
# Context: user@hostname (who am I and where am I) # Context: user@hostname (who am I and where am I)
prompt_context() { prompt_context() {
local user=`whoami` if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)$USER@%m"
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)$user@%m"
fi fi
} }
......
...@@ -13,7 +13,7 @@ patches: <patches|join( → )|pre_applied(%{$fg[yellow]%})|post_applied(%{$reset ...@@ -13,7 +13,7 @@ patches: <patches|join( → )|pre_applied(%{$fg[yellow]%})|post_applied(%{$reset
} }
function box_name { function box_name {
[ -f ~/.box-name ] && cat ~/.box-name || hostname -s [ -f ~/.box-name ] && cat ~/.box-name || echo $SHORT_HOST || echo $HOST
} }
PROMPT=' PROMPT='
......
...@@ -21,7 +21,7 @@ local user="%(!.%{$fg[blue]%}.%{$fg[blue]%})%n%{$reset_color%}" ...@@ -21,7 +21,7 @@ local user="%(!.%{$fg[blue]%}.%{$fg[blue]%})%n%{$reset_color%}"
# Hostname part. compressed and colorcoded per host_repr array # Hostname part. compressed and colorcoded per host_repr array
# if not found, regular hostname in default color # if not found, regular hostname in default color
local host="@${host_repr[$(hostname)]:-$(hostname)}%{$reset_color%}" local host="@${host_repr[$HOST]:-$HOST}%{$reset_color%}"
# Compacted $PWD # Compacted $PWD
local pwd="%{$fg[blue]%}%c%{$reset_color%}" local pwd="%{$fg[blue]%}%c%{$reset_color%}"
......
...@@ -21,7 +21,7 @@ function prompt_char { ...@@ -21,7 +21,7 @@ function prompt_char {
} }
function box_name { function box_name {
[ -f ~/.box-name ] && cat ~/.box-name || hostname -s [ -f ~/.box-name ] && cat ~/.box-name || echo $SHORT_HOST || echo $HOST
} }
......
...@@ -17,7 +17,7 @@ function prompt_char { ...@@ -17,7 +17,7 @@ function prompt_char {
} }
function box_name { function box_name {
[ -f ~/.box-name ] && cat ~/.box-name || hostname -s [ -f ~/.box-name ] && cat ~/.box-name || echo $SHORT_HOST || echo $HOST
} }
local ruby_env='' local ruby_env=''
......
if [ "$(whoami)" = "root" ] if [ "$USER" = "root" ]
then CARETCOLOR="red" then CARETCOLOR="red"
else CARETCOLOR="blue" else CARETCOLOR="blue"
fi fi
......
...@@ -11,19 +11,11 @@ git_custom_status() { ...@@ -11,19 +11,11 @@ git_custom_status() {
fi fi
} }
#RVM and git settings # RVM component of prompt
if [[ -s ~/.rvm/scripts/rvm ]] ; then ZSH_THEME_RVM_PROMPT_PREFIX="%{$fg[red]%}["
RPS1='$(git_custom_status)%{$fg[red]%}[`~/.rvm/bin/rvm-prompt`]%{$reset_color%} $EPS1' ZSH_THEME_RVM_PROMPT_SUFFIX="]%{$reset_color%}"
else
if which rbenv &> /dev/null; then # Combine it all into a final right-side prompt
RPS1='$(git_custom_status)%{$fg[red]%}[`rbenv version | sed -e "s/ (set.*$//"`]%{$reset_color%} $EPS1' RPS1='$(git_custom_status)$(ruby_prompt_info) $EPS1'
else
if [[ -n `which chruby_prompt_info` && -n `chruby_prompt_info` ]]; then
RPS1='$(git_custom_status)%{$fg[red]%}[`chruby_prompt_info`]%{$reset_color%} $EPS1'
else
RPS1='$(git_custom_status) $EPS1'
fi
fi
fi
PROMPT='%{$fg[cyan]%}[%~% ]%(?.%{$fg[green]%}.%{$fg[red]%})%B$%b ' PROMPT='%{$fg[cyan]%}[%~% ]%(?.%{$fg[green]%}.%{$fg[red]%})%B$%b '
# Simple theme based on my old zsh settings. # Simple theme based on my old zsh settings.
function get_host { function get_host {
echo '@'`hostname`'' echo '@'$HOST
} }
PROMPT='> ' PROMPT='> '
......
...@@ -17,7 +17,7 @@ USERNAME_NORMAL_COLOR=$WHITE ...@@ -17,7 +17,7 @@ USERNAME_NORMAL_COLOR=$WHITE
USERNAME_ROOT_COLOR=$RED USERNAME_ROOT_COLOR=$RED
HOSTNAME_NORMAL_COLOR=$BLUE HOSTNAME_NORMAL_COLOR=$BLUE
# uncomment next line if you want auto-generated hostname color # uncomment next line if you want auto-generated hostname color
#for i in `hostname`; HOSTNAME_NORMAL_COLOR=$COLOR_ARRAY[$[((#i))%7+1]] #for i in $HOST; HOSTNAME_NORMAL_COLOR=$COLOR_ARRAY[$[((#i))%7+1]]
HOSTNAME_ROOT_COLOR=$RED HOSTNAME_ROOT_COLOR=$RED
HOSTNAME_COLOR=%(!.$HOSTNAME_ROOT_COLOR.$HOSTNAME_NORMAL_COLOR) HOSTNAME_COLOR=%(!.$HOSTNAME_ROOT_COLOR.$HOSTNAME_NORMAL_COLOR)
CURRENT_DIR_COLOR=$CYAN CURRENT_DIR_COLOR=$CYAN
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment