mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-05-31 08:18:26 +00:00
feat(extract): add support for extracting to a specified directory (#13734)
This commit is contained in:
@@ -53,5 +53,6 @@ local -a exts=(
|
|||||||
|
|
||||||
_arguments \
|
_arguments \
|
||||||
'(-r --remove)'{-r,--remove}'[Remove archive.]' \
|
'(-r --remove)'{-r,--remove}'[Remove archive.]' \
|
||||||
|
'(-t --to-directory)'{-t,--to-directory}'[Extract to a specific directory.]' \
|
||||||
"*::archive file:_files -g '(#i)*.(${(j:|:)exts})(-.)'" \
|
"*::archive file:_files -g '(#i)*.(${(j:|:)exts})(-.)'" \
|
||||||
&& return 0
|
&& return 0
|
||||||
|
|||||||
47
plugins/extract/extract.plugin.zsh
Normal file → Executable file
47
plugins/extract/extract.plugin.zsh
Normal file → Executable file
@@ -9,14 +9,41 @@ Usage: extract [-option] [file ...]
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
-r, --remove Remove archive after unpacking.
|
-r, --remove Remove archive after unpacking.
|
||||||
|
-t, --to-directory <dir> Extract to a specific directory instead of the current one.
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local remove_archive=1
|
local remove_archive=1
|
||||||
if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
|
local target_directory=""
|
||||||
remove_archive=0
|
|
||||||
shift
|
while (( $# > 0 )); do
|
||||||
fi
|
case "$1" in
|
||||||
|
-r|--remove)
|
||||||
|
remove_archive=0
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-t|--to-directory)
|
||||||
|
shift
|
||||||
|
if (( $# == 0 )); then
|
||||||
|
echo "extract: -t/--to-directory requires a directory argument" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
target_directory="$1"
|
||||||
|
shift
|
||||||
|
|
||||||
|
if [[ ! -d "$target_directory" ]]; then
|
||||||
|
echo "extract: '$target_directory' is not a valid directory" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
target_directory="${target_directory%/}"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
local pwd="$PWD"
|
local pwd="$PWD"
|
||||||
while (( $# > 0 )); do
|
while (( $# > 0 )); do
|
||||||
@@ -35,6 +62,10 @@ EOF
|
|||||||
extract_dir="${extract_dir:r}"
|
extract_dir="${extract_dir:r}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [[ -n "$target_directory" ]]; then
|
||||||
|
extract_dir="$target_directory/${extract_dir:t}"
|
||||||
|
fi
|
||||||
|
|
||||||
# If there's a file or directory with the same name as the archive
|
# If there's a file or directory with the same name as the archive
|
||||||
# add a random string to the end of the extract directory
|
# add a random string to the end of the extract directory
|
||||||
if [[ -e "$extract_dir" ]]; then
|
if [[ -e "$extract_dir" ]]; then
|
||||||
@@ -126,7 +157,7 @@ EOF
|
|||||||
# 1. Move and rename the extracted file/folder to a temporary random name
|
# 1. Move and rename the extracted file/folder to a temporary random name
|
||||||
# 2. Delete the empty folder
|
# 2. Delete the empty folder
|
||||||
# 3. Rename the extracted file/folder to the original name
|
# 3. Rename the extracted file/folder to the original name
|
||||||
if [[ "${content[1]:t}" == "$extract_dir" ]]; then
|
if [[ "${content[1]:t}" == "${extract_dir:t}" ]]; then
|
||||||
# =(:) gives /tmp/zsh<random>, with :t it gives zsh<random>
|
# =(:) gives /tmp/zsh<random>, with :t it gives zsh<random>
|
||||||
local tmp_name==(:); tmp_name="${tmp_name:t}"
|
local tmp_name==(:); tmp_name="${tmp_name:t}"
|
||||||
command mv "${content[1]}" "$tmp_name" \
|
command mv "${content[1]}" "$tmp_name" \
|
||||||
@@ -134,9 +165,9 @@ EOF
|
|||||||
&& command mv "$tmp_name" "$extract_dir"
|
&& command mv "$tmp_name" "$extract_dir"
|
||||||
# Otherwise, if the extracted folder name already exists in the current
|
# Otherwise, if the extracted folder name already exists in the current
|
||||||
# directory (because of a previous file / folder), keep the extract_dir
|
# directory (because of a previous file / folder), keep the extract_dir
|
||||||
elif [[ ! -e "${content[1]:t}" ]]; then
|
elif [[ ! -e "${target_directory:-.}/${content[1]:t}" ]]; then
|
||||||
command mv "${content[1]}" . \
|
command mv -- "${content[1]}" "${target_directory:-.}/" \
|
||||||
&& command rmdir "$extract_dir"
|
&& command rmdir -- "$extract_dir"
|
||||||
fi
|
fi
|
||||||
elif [[ ${#content} -eq 0 ]]; then
|
elif [[ ${#content} -eq 0 ]]; then
|
||||||
command rmdir "$extract_dir"
|
command rmdir "$extract_dir"
|
||||||
|
|||||||
Reference in New Issue
Block a user