aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <msabate@suse.com>2020-11-14 20:33:34 +0100
committerMiquel Sabaté Solà <msabate@suse.com>2020-11-14 20:33:34 +0100
commit4ba33ceb73b2c8f4cc747c32786eaa9680384d53 (patch)
treeca958b974a7c6090e44f5d1dd3c8f5c095ef3ce7
parentc074c0d51a6e3c7924a2ddea3ea418729564a252 (diff)
downloadg-4ba33ceb73b2c8f4cc747c32786eaa9680384d53.tar.gz
g-4ba33ceb73b2c8f4cc747c32786eaa9680384d53.zip
Added some compatibilities with zsh
The work is half-done, but I also needed some of the other fixes entangled in this whole mess of a commit. Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
-rw-r--r--Dockerfile2
-rw-r--r--Makefile9
-rw-r--r--g.sh64
-rw-r--r--t/test.sh28
4 files changed, 66 insertions, 37 deletions
diff --git a/Dockerfile b/Dockerfile
index 14f004e..2fa1e49 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,5 +1,5 @@
FROM alpine
MAINTAINER Miquel Sabaté Solà
-RUN apk add --update bash util-linux && rm -rf /var/cache/apk/*
+RUN apk add --update bash zsh util-linux && rm -rf /var/cache/apk/*
COPY g.sh t/test.sh t/expected.txt /
diff --git a/Makefile b/Makefile
index e086ae1..130d599 100644
--- a/Makefile
+++ b/Makefile
@@ -2,8 +2,15 @@
test: git-validation shellcheck unit-test
.PHONY: unit-test
-unit-test:
+unit-test: build-image bash-test
+
+.PHONY: build-image
+build-image:
@docker build -t mssola/g:latest .
+
+.PHONY: bash-test
+bash-test:
+ @echo -e "BASH\ttest.sh"
@docker run --rm mssola/g:latest bash test.sh
.PHONY: git-validation
diff --git a/g.sh b/g.sh
index 05434ff..a9610b8 100644
--- a/g.sh
+++ b/g.sh
@@ -37,7 +37,8 @@ __g_get_shortcuts() {
local word=""
__g_shortcuts=()
- while read -r line; do
+ # shellcheck disable=SC2162
+ while read line; do
if [ -n "$line" ]; then
if [ -z "$word" ]; then
word=$line
@@ -49,21 +50,25 @@ __g_get_shortcuts() {
done < "$__g_file"
}
-# Save the computed shortcuts into the __g_file.
-__g_save_shortcuts() {
- # Erase the contents of the __g_file.
- :>"$__g_file"
-
- # Bash vs zsh
+# Echoes the keys of the current shortcuts. This is needed to cope with some
+# differences between GNU Bash and ZSH.
+__g_fetch_shortcut_keys() {
if [ -n "$ZSH_VERSION" ]; then
# shellcheck disable=SC2154
- keys="${(@i)__g_shortcuts}"
+ echo "${(@k)__g_shortcuts}"
else
# shellcheck disable=SC2124
- keys="${!__g_shortcuts[@]}"
+ echo "${!__g_shortcuts[@]}"
fi
+}
+
+# Save the computed shortcuts into the __g_file.
+__g_save_shortcuts() {
+ # Erase the contents of the __g_file.
+ :>"$__g_file"
- # Finally write the hash into the __g_file.
+ # And write the hash into the __g_file.
+ keys=$(__g_fetch_shortcut_keys)
for i in $keys; do
echo "$i" >> "$__g_file"
echo "${__g_shortcuts[$i]}" >> "$__g_file"
@@ -111,7 +116,7 @@ g() {
if [ -n "$GFILE" ]; then
__g_file="$GFILE"
fi
- touch "$__g_file"
+ :>> "$__g_file"
# Parse the command.
case "$cmd" in
@@ -129,10 +134,10 @@ HERE
;;
add)
if [ "$#" = "2" ]; then
- path=$(pwd)
+ p=$(pwd)
else
if [ "$#" = "3" ]; then
- path="$3"
+ p="$3"
else
echo "usage: g add <name> [path]"
return 1
@@ -143,7 +148,7 @@ HERE
return 1
fi
__g_get_shortcuts
- __g_shortcuts[$2]=$(__g_realpath "$path")
+ __g_shortcuts[$2]=$(__g_realpath "$p")
__g_save_shortcuts
;;
rm)
@@ -157,15 +162,20 @@ HERE
;;
list)
__g_get_shortcuts
+ keys=$(__g_fetch_shortcut_keys)
if [[ "$#" = "2" && "$2" = "--keys" ]]; then
str=""
- for i in "${!__g_shortcuts[@]}"; do
+ for i in $keys; do
str="$str $i"
done
echo "$str"
else
- for i in "${!__g_shortcuts[@]}"; do
- echo -e "$i\t=> ${__g_shortcuts[$i]}"
+ for i in $keys; do
+ if [ -d "${__g_shortcuts[$i]}" ]; then
+ echo -e "$i\t=> ${__g_shortcuts[$i]}"
+ else
+ echo -e "$i\t=> ${__g_shortcuts[$i]} (broken)"
+ fi
done | sort | column -t -s $'\t'
fi
;;
@@ -174,8 +184,15 @@ HERE
# Split the path and check whether the first element is a shortcut or
# not.
- IFS='/' read -r -a path <<< "$cmd"
- init="${path[0]}"
+ if [ -n "$ZSH_VERSION" ]; then
+ # shellcheck disable=SC2154
+ p=( "${(@s|/|)cmd}" )
+ init="${p[1]}"
+ else
+ # shellcheck disable=SC2124
+ IFS='/' read -r -a p <<< "$cmd"
+ init="${p[0]}"
+ fi
if [ -z "${__g_shortcuts[$init]}" ]; then
echo -e "Unknown shortcut \`$init'.\n"
@@ -183,8 +200,13 @@ HERE
return 1
else
# Expand the shortcut and append the remaining parts of the path.
- path[0]="${__g_shortcuts[$init]}"
- cd "$(__g_join_path "${path[@]}")" || return 1
+ if [ -n "$ZSH_VERSION" ]; then
+ p[1]="${__g_shortcuts[$init]}"
+ else
+ p[0]="${__g_shortcuts[$init]}"
+ fi
+
+ cd "$(__g_join_path "${p[@]}")" || return 1
fi
;;
esac
diff --git a/t/test.sh b/t/test.sh
index f5405b7..bc2d06d 100644
--- a/t/test.sh
+++ b/t/test.sh
@@ -9,10 +9,10 @@ cd /
mkdir -p a/b/c
mkdir -p a/d
-g -v &>> /output.txt
-g --version &>> /output.txt
-g -h &>> /output.txt
-g --help &>> /output.txt
+g -v >> /output.txt 2>&1
+g --version >> /output.txt 2>&1
+g -h >> /output.txt 2>&1
+g --help >> /output.txt 2>&1
# Let's add some shortcuts.
g add root
@@ -21,8 +21,8 @@ g add c a/b/c
# Errors on the `add` command.
set +e
-g add add &>> /output.txt
-g add lala lala lala &>> /output.txt
+g add add >> /output.txt 2>&1
+g add lala lala lala >> /output.txt 2>&1
set -e
# Removing shortcuts.
@@ -31,26 +31,26 @@ g rm d
# Errors on the `rm` command.
set +e
-g rm &>> /output.txt
-g rm lala lala &>> /output.txt
+g rm >> /output.txt 2>&1
+g rm lala lala >> /output.txt 2>&1
set -e
# The list command.
-g list --keys | xargs -n1 | sort | xargs &>> /output.txt
-g list | awk '{ print $1 }' | sort | xargs &>> /output.txt
+g list --keys | xargs -n1 | sort | xargs >> /output.txt 2>&1
+g list | awk '{ print $1 }' | sort | xargs >> /output.txt 2>&1
# Bare g command.
cd /
g b
-basename $(pwd) &>> /output.txt
+basename $(pwd) >> /output.txt 2>&1
g root
-basename $(pwd) &>> /output.txt
+basename $(pwd) >> /output.txt 2>&1
g root/a/b/c
-basename $(pwd) &>> /output.txt
+basename $(pwd) >> /output.txt 2>&1
# Unknown shortcut.
set +e
-g unknown &>> /output.txt
+g unknown >> /output.txt 2>&1
set -e
# Final diff