ambevar-dotfiles/.scripts/zshcd

47 lines
1.1 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2014-03-06 15:35:52 +01:00
## Start zsh in the directory specified as first argument. Useful for file
## browsers to start shell in pointed folder.
2013-07-26 15:33:44 +02:00
if [ ! -d "$1" ]; then
zsh
exit
2013-07-26 15:33:44 +02:00
fi
## Default files.
ZDIRS="$HOME/.cache/zsh/dirs"
## Fetch file location dynamically.
if [ ! -f ~/.zshrc ]; then
echo '~/.zshrc not found. Exiting.'
exit 1
fi
2013-12-27 11:17:55 +01:00
## We need realpath to prevent us from adding duplicates to the zsh
## DIRSTACKFILE.
2014-01-20 17:18:36 +01:00
if ! command -v realpath >/dev/null 2>&1; then
echo "'realpath' not found. Exiting."
exit 1
2013-07-27 12:13:09 +02:00
fi
SHELL_DIR_GREP="$(grep "SHELL_DIR=" ~/.zshrc | cut -f2 -d=)"
[ -n "$SHELL_DIR_GREP" ] && SHELL_DIR="$(eval echo "$SHELL_DIR_GREP")"
if [ ! -d "$SHELL_DIR" ]; then
echo 'SHELL_DIR not found. Could not search for DIRSTACKFILE. Exiting.'
exit 1
fi
ZDIRS_GREP="$(grep -rm1 'DIRSTACKFILE=' "$SHELL_DIR" | cut -f2 -d=)"
[ -n "$ZDIRS_GREP" ] && ZDIRS="$(eval echo "$ZDIRS_GREP")"
if [ ! -f "$ZDIRS" ]; then
echo 'DIRSTACKFILE not found. Exiting.'
exit 1
fi
2013-07-27 12:13:09 +02:00
## Prepend argument to zsh dirstack.
BUF="$(realpath "$1")
2013-08-05 23:20:35 +02:00
$(grep -v "$(realpath "$1")" "$ZDIRS")"
echo "$BUF" > "$ZDIRS"
2013-07-26 15:33:44 +02:00
zsh