Skip to content

Instantly share code, notes, and snippets.

@multun
Last active April 20, 2020 21:35
Show Gist options
  • Save multun/ade5f833eb76cee781acb3c13cff15c2 to your computer and use it in GitHub Desktop.
Save multun/ade5f833eb76cee781acb3c13cff15c2 to your computer and use it in GitHub Desktop.
A shell workspace management library
## A workspace manager
: ${WS_CONFIG_DIR:=~/.config/ws}
ws_l () {
for ws_file in "${WS_CONFIG_DIR}"/*.ws; do
local ws_name="$(basename "$ws_file")"
ws_name="${ws_name%.ws}"
printf "%-10s\t%s\n" "$ws_name" "$(cat "${ws_file}")"
done
}
ws_path () {
printf '%s\n' "${WS_CONFIG_DIR}/${1}".ws
}
ws_check () {
if (( $# != 1 )); then
echo "missing workspace name" >&2
return 1
fi
if ! [ -f "$(ws_path "$1")" ]; then
echo "workspace does not exist" >&2
return 1
fi
}
ws_n () {
if (( $# < 1 )) || (( $# > 2 )); then
echo "usage: ws n NAME [PATH]" >&2
return
fi
local name="$1"
local target_path="$2"
if [ -z "$target_path" ]; then
target_path="$(pwd)"
fi
printf '%s\n' "$target_path" > "$(ws_path "$name")"
}
ws_g () {
local target_path="$(ws_path "$1")"
if ! [ -f "$target_path" ]; then
echo "no such workspace" >&2
fi
cd "$(cat "$target_path")"
}
ws () {
mkdir -p -- "${WS_CONFIG_DIR}"
local sub_name="$1"
shift
ws_"${sub_name}" "$@"
}
pgo () {
ws g "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment