Browse code

Merge pull request #208 from sorin-ionescu/plugin-extract

extract plugin

Robby Russell authored on 23/07/2011 at 16:33:42
Showing 4 changed files
... ...
@@ -22,4 +22,3 @@ alias sl=ls # often screw this up
22 22
 
23 23
 alias afind='ack-grep -il'
24 24
 
25
-alias x=extract
... ...
@@ -15,38 +15,3 @@ function take() {
15 15
   cd $1
16 16
 }
17 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
-
53 18
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+#compdef extract
1
+#autoload
2
+
3
+_arguments \
4
+  '(-r --remove)'{-r,--remove}'[Remove archive.]' \
5
+  "*::archive file:_files -g '(#i)*.(tar|tgz|tbz|tbz2|txz|tlz|gz|bz2|xz|lzma|Z|zip|rar|7z|deb)(-.)'" && return 0
6
+
7
+
0 8
new file mode 100644
... ...
@@ -0,0 +1,85 @@
0
+# ------------------------------------------------------------------------------
1
+#          FILE:  extract.plugin.zsh
2
+#   DESCRIPTION:  oh-my-zsh plugin file.
3
+#        AUTHOR:  Sorin Ionescu (sorin.ionescu@gmail.com)
4
+#       VERSION:  1.0.1
5
+# ------------------------------------------------------------------------------
6
+
7
+
8
+function extract() {
9
+  local remove_archive
10
+  local success
11
+  local file_name
12
+  local extract_dir
13
+
14
+  if (( $# == 0 )); then
15
+    echo "Usage: extract [-option] [file ...]"
16
+    echo
17
+    echo Options:
18
+    echo "    -r, --remove    Remove archive."
19
+    echo
20
+    echo "Report bugs to <sorin.ionescu@gmail.com>."
21
+  fi
22
+
23
+  remove_archive=1
24
+  if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
25
+    remove_archive=0 
26
+    shift
27
+  fi
28
+
29
+  while (( $# > 0 )); do
30
+    if [[ ! -f "$1" ]]; then
31
+      echo "extract: '$1' is not a valid file" 1>&2
32
+      shift
33
+      continue
34
+    fi
35
+
36
+    success=0
37
+    file_name="$( basename "$1" )"
38
+    extract_dir="$( echo "$file_name" | sed "s/\.${1##*.}//g" )"
39
+    case "$1" in
40
+      (*.tar.gz|*.tgz) tar xvzf "$1" ;;
41
+      (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;;
42
+      (*.tar.xz|*.txz) tar --xz --help &> /dev/null \
43
+        && tar --xz -xvf "$1" \
44
+        || xzcat "$1" | tar xvf - ;;
45
+      (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
46
+        && tar --lzma -xvf "$1" \
47
+        || lzcat "$1" | tar xvf - ;;
48
+      (*.tar) tar xvf "$1" ;;
49
+      (*.gz) gunzip "$1" ;;
50
+      (*.bz2) bunzip2 "$1" ;;
51
+      (*.xz) unxz "$1" ;;
52
+      (*.lzma) unlzma "$1" ;;
53
+      (*.Z) uncompress "$1" ;;
54
+      (*.zip) unzip "$1" -d $extract_dir ;;
55
+      (*.rar) unrar e -ad "$1" ;;
56
+      (*.7z) 7za x "$1" ;;
57
+      (*.deb)
58
+        mkdir -p "$extract_dir/control"
59
+        mkdir -p "$extract_dir/data"
60
+        cd "$extract_dir"; ar vx "../${1}" > /dev/null
61
+        cd control; tar xzvf ../control.tar.gz
62
+        cd ../data; tar xzvf ../data.tar.gz
63
+        cd ..; rm *.tar.gz debian-binary
64
+        cd ..
65
+      ;;
66
+      (*) 
67
+        echo "extract: '$1' cannot be extracted" 1>&2
68
+        success=1 
69
+      ;; 
70
+    esac
71
+
72
+    (( success = $success > 0 ? $success : $? ))
73
+    (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1"
74
+    shift
75
+  done
76
+}
77
+
78
+alias x=extract
79
+
80
+# add extract completion function to path
81
+fpath=($ZSH/plugins/extract $fpath)
82
+autoload -U compinit
83
+compinit -i
84
+