1db6575f14d07b9ed3b0ce530837a8a0713b69d0

Author
Sorin Ionescu <sorin.ionescu@gmail.com>
Committer
Sorin Ionescu <sorin.ionescu@gmail.com>
Date

Message

Added extract plugin.

Diff

  1diff --git a/lib/aliases.zsh b/lib/aliases.zsh
  2index b47de5bde5a573e4c9465048111c133b3213edc3..0555be2642fcd73e6d4ced8bc44e7eee75e13607 100644
  3--- a/lib/aliases.zsh
  4+++ b/lib/aliases.zsh
  5@@ -22,4 +22,3 @@ alias sl=ls # often screw this up
  6 
  7 alias afind='ack-grep -il'
  8 
  9-alias x=extract
 10diff --git a/lib/functions.zsh b/lib/functions.zsh
 11index b29d3e482d6dc1b0c1aad5980ed271332b232d68..ef7cc6383038b7e6f9a02c857f13ea498ba5a8fe 100644
 12--- a/lib/functions.zsh
 13+++ b/lib/functions.zsh
 14@@ -15,38 +15,3 @@ function take() {
 15   cd $1
 16 }
 17 
 18-function extract() {
 19-    unset REMOVE_ARCHIVE
 20-    
 21-    if test "$1" = "-r"; then
 22-        REMOVE=1
 23-        shift
 24-    fi
 25-  if [[ -f $1 ]]; then
 26-    case $1 in
 27-      *.tar.bz2) tar xvjf $1;;
 28-      *.tar.gz) tar xvzf $1;;
 29-      *.tar.xz) tar xvJf $1;;
 30-      *.tar.lzma) tar --lzma -xvf $1;;
 31-      *.bz2) bunzip $1;;
 32-      *.rar) unrar x $1;;
 33-      *.gz) gunzip $1;;
 34-      *.tar) tar xvf $1;;
 35-      *.tbz2) tar xvjf $1;;
 36-      *.tgz) tar xvzf $1;;
 37-      *.zip) unzip $1;;
 38-      *.Z) uncompress $1;;
 39-      *.7z) 7z x $1;;
 40-      *) echo "'$1' cannot be extracted via >extract<";;
 41-    esac
 42-
 43-    if [[ $REMOVE_ARCHIVE -eq 1 ]]; then
 44-        echo removing "$1";
 45-        /bin/rm "$1";
 46-    fi
 47-
 48-  else
 49-    echo "'$1' is not a valid file"
 50-  fi
 51-}
 52-
 53diff --git a/plugins/extract/_extract b/plugins/extract/_extract
 54new file mode 100644
 55index 0000000000000000000000000000000000000000..dca890954aaad3e36543d18b6fc8b04c090cfc7d
 56--- /dev/null
 57+++ b/plugins/extract/_extract
 58@@ -0,0 +1,8 @@
 59+#compdef extract
 60+#autoload
 61+
 62+_arguments \
 63+  '(-r --remove)'{-r,--remove}'[Remove archive.]' \
 64+  "*::archive file:_files -g '(#i)*.(tar|tgz|tbz|tbz2|txz|tlz|gz|bz2|xz|lzma|Z|zip|rar|7z|deb)(-.)'" && return 0
 65+
 66+
 67diff --git a/plugins/extract/extract.plugin.zsh b/plugins/extract/extract.plugin.zsh
 68new file mode 100644
 69index 0000000000000000000000000000000000000000..c58692fbfd66b794b90069aae18d14b6e19f93bb
 70--- /dev/null
 71+++ b/plugins/extract/extract.plugin.zsh
 72@@ -0,0 +1,83 @@
 73+# ------------------------------------------------------------------------------
 74+#          FILE:  extract.plugin.zsh
 75+#   DESCRIPTION:  oh-my-zsh plugin file.
 76+#        AUTHOR:  Sorin Ionescu (sorin.ionescu@gmail.com)
 77+#       VERSION:  1.0.0
 78+# ------------------------------------------------------------------------------
 79+
 80+
 81+function extract() {
 82+  local remove_archive
 83+  local success
 84+  local file_name
 85+  local extract_dir
 86+
 87+  if (( $# == 0 )); then
 88+    echo "Usage: extract [-option] [file ...]"
 89+    echo
 90+    echo Options:
 91+    echo "    -r, --remove    Remove archive."
 92+    echo
 93+    echo "Report bugs to <sorin.ionescu@gmail.com>."
 94+  fi
 95+
 96+  remove_archive=1
 97+  if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
 98+    remove_archive=0 
 99+    shift
100+  fi
101+
102+  while (( $# > 0 )); do
103+    if [[ ! -f "$1" ]]; then
104+      echo "extract: '$1' is not a valid file" 1>&2
105+      shift
106+      continue
107+    fi
108+
109+    success=0
110+    file_name="$( basename "$1" )"
111+    extract_dir="$( echo "$file_name" | sed "s/\.${1##*.}//g" )"
112+    case "$1" in
113+      (*.tar.gz|*.tgz) tar xvzf "$1" ;;
114+      (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;;
115+      (*.tar.xz|*.txz) tar xvJf "$1" ;;
116+      # (*.tar.xz|*.txz) xzcat "$1" | tar xvf - ;;
117+      (*.tar.lzma|*.tlz) tar --lzma -xvf "$1" ;;
118+      # (*.tar.lzma|*.tlz) lzcat "$1" | tar xvf - ;;
119+      (*.tar) tar xvf "$1" ;;
120+      (*.gz) gunzip "$1" ;;
121+      (*.bz2) bunzip2 "$1" ;;
122+      (*.xz) unxz "$1" ;;
123+      (*.lzma) unlzma "$1" ;;
124+      (*.Z) uncompress "$1" ;;
125+      (*.zip) unzip "$1" -d $extract_dir ;;
126+      (*.rar) unrar e -ad "$1" ;;
127+      (*.7z) 7za x "$1" ;;
128+      (*.deb)
129+        mkdir -p "$extract_dir/control"
130+        mkdir -p "$extract_dir/data"
131+        cd "$extract_dir"; ar vx "../${1}" > /dev/null
132+        cd control; tar xzvf ../control.tar.gz
133+        cd ../data; tar xzvf ../data.tar.gz
134+        cd ..; rm *.tar.gz debian-binary
135+        cd ..
136+      ;;
137+      (*) 
138+        echo "extract: '$1' cannot be extracted" 1>&2
139+        success=1 
140+      ;; 
141+    esac
142+
143+    (( success = $success > 0 ? $success : $? ))
144+    (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1"
145+    shift
146+  done
147+}
148+
149+alias x=extract
150+
151+# add extract completion function to path
152+fpath=($ZSH/plugins/extract $fpath)
153+autoload -U compinit
154+compinit -i
155+