This commit is contained in:
ge 2022-10-05 23:49:50 +03:00
parent 19860fe8db
commit e7ce1c198f
2 changed files with 16 additions and 10 deletions

4
README
View File

@ -67,6 +67,10 @@ and etc.
Changelog Changelog
========= =========
v0.4 (05 Oct 2022)
* Fix parsing paths
v0.3 (05 Oct 2022) v0.3 (05 Oct 2022)
* Refactored, added POSIX-compatibility * Refactored, added POSIX-compatibility

22
g.sh
View File

@ -1,5 +1,5 @@
# * g (goto directory) - bookmark directories in shell. # * g (goto directory) - bookmark directories in shell.
# * version: 0.3 # * version: 0.4
# This is free and unencumbered software released into the public domain. # This is free and unencumbered software released into the public domain.
# #
@ -42,17 +42,16 @@ _g_cd() {
printf 'g: no such directory %s\n' "$1" >&2 printf 'g: no such directory %s\n' "$1" >&2
return 1 return 1
fi fi
if cd -- "$1" > /dev/null; then _dir="$(echo "$1" | sed "s%^~%$HOME%;s%//%/%g")"
echo "$1" if cd -- "$_dir" > /dev/null; then
echo "$_dir"
else else
printf '\bg: cannot cd into %s\n' "$1" >&2 printf '\bg: cannot cd into %s\n' "$_dir" >&2
fi fi
return "$?" return "$?"
} }
_g_prompt() { _g_prompt() {
# Prompt and cd
# Exit if no dirs in $_g_file # Exit if no dirs in $_g_file
if [ "$#" -eq 0 ] || [ "$*" = '' ]; then if [ "$#" -eq 0 ] || [ "$*" = '' ]; then
echo goto: nothing to do; return 1 echo goto: nothing to do; return 1
@ -99,8 +98,7 @@ _g_search() {
} }
_g() { _g() {
# Main function # Get dirs from $_g_file
# Get directory list ('dirs' array)
if [ -f "$_g_file" ]; then if [ -f "$_g_file" ]; then
if [ "$1" ]; then if [ "$1" ]; then
# Search dirs with Perl regex # Search dirs with Perl regex
@ -116,9 +114,13 @@ _g() {
_g_save() { _g_save() {
# Save dir in $_g_file # Save dir in $_g_file
if [ -n "$1" ]; then if [ -n "$1" ]; then
_dir="$1" if hash realpath >/dev/null 2>&1; then
_dir="$(realpath "$1")"
else
_dir="$(echo "${PWD}/${1%*/}" | sed 's%//%/%g')"
[ -d "$_dir" ] || _dir="$1"
fi
else else
_dir="$PWD" _dir="$PWD"
fi fi