Last active
December 26, 2022 02:42
-
-
Save ktnyt/d96ac47aca771dc2db8b8d87a83af86b to your computer and use it in GitHub Desktop.
Directory teleportation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
function tp() { | |
help() { | |
echo "usage: tp [-h | --help] [<alias>] [<directory>]" | |
} | |
TP_CONFIG_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/tp" | |
TP_CONFIG_FILE="${TP_CONFIG_PATH}/config.tsv" | |
if [ ! -d $TP_CONFIG_PATH ] | |
then | |
mkdir -p $TP_CONFIG_PATH | |
fi | |
if [ ! -f $TP_CONFIG_FILE ] | |
then | |
touch $TP_CONFIG_FILE | |
fi | |
for arg in $* | |
do | |
case $arg in | |
-h|--help) | |
help | |
return | |
;; | |
esac | |
done | |
case $# in | |
0) | |
cat $TP_CONFIG_FILE | |
return | |
;; | |
1) | |
cd $(cat $TP_CONFIG_FILE | awk -v key="$1" '$1 == key' | cut -f 2) | |
return | |
;; | |
2) | |
{ cat $TP_CONFIG_FILE | awk -v key="$1" '$1 != key'; echo "$1\t$2" } | sort > $TP_CONFIG_FILE | |
;; | |
*) | |
help | |
return | |
;; | |
esac | |
} | |
function _tp() { | |
_arguments '1: :($(cat $TP_CONFIG_FILE | cut -f 1))' '2: :_path_files' | |
} | |
compdef _tp tp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment