492f712df281e96d6d1aa9ebb53b1304284b19a0

Author
Konfekt <Konfekt@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(plugins): add `universalarchive` plugin to conveniently compress files (#6846)

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

  1diff --git a/plugins/universalarchive/README.md b/plugins/universalarchive/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..93a1bd9fc47ddc975d638b55b061cc6d8f1e2646
  4--- /dev/null
  5+++ b/plugins/universalarchive/README.md
  6@@ -0,0 +1,46 @@
  7+# universalarchive plugin
  8+
  9+Lets you compress files by a command `ua <format> <files>`, supporting various
 10+compression formats (e.g. 7z, tar.gz, lzma, ...).
 11+
 12+To enable it, add `universalarchive` to the plugins array in your zshrc file:
 13+
 14+```zsh
 15+plugins=(... universalarchive)
 16+```
 17+
 18+## Usage
 19+
 20+Run `ua <format> <files>` to compress `<files>` into an archive file using `<format>`.
 21+For example:
 22+
 23+```sh
 24+ua xz *.html
 25+```
 26+
 27+this command will compress all `.html` files in directory `folder` into `folder.xz`.
 28+
 29+This plugin saves you from having to remember which command line arguments compress a file.
 30+
 31+## Supported compression formats
 32+
 33+| Extension        | Description                    |
 34+|:-----------------|:-------------------------------|
 35+| `7z`             | 7zip file                      |
 36+| `bz2`            | Bzip2 file                     |
 37+| `gz`             | Gzip file                      |
 38+| `lzma`           | LZMA archive                   |
 39+| `lzo`            | LZO archive                    |
 40+| `rar`            | WinRAR archive                 |
 41+| `tar`            | Tarball                        |
 42+| `tbz`/`tar.bz2`  | Tarball with bzip2 compression |
 43+| `tgz`/`tar.gz`   | Tarball with gzip compression  |
 44+| `tlz`/`tar.lzma` | Tarball with lzma compression  |
 45+| `txz`/`tar.xz`   | Tarball with lzma2 compression |
 46+| `tZ`/`tar.Z`     | Tarball with LZW compression   |
 47+| `xz`             | LZMA2 archive                  |
 48+| `Z`              | Z archive (LZW)                |
 49+| `zip`            | Zip archive                    |
 50+| `zst`            | Zstd archive                   |
 51+
 52+See [list of archive formats](https://en.wikipedia.org/wiki/List_of_archive_formats) for more information regarding the archive formats.
 53diff --git a/plugins/universalarchive/_universalarchive b/plugins/universalarchive/_universalarchive
 54new file mode 100644
 55index 0000000000000000000000000000000000000000..17cfd782e0072a5d03a090a4cdb5458736876be8
 56--- /dev/null
 57+++ b/plugins/universalarchive/_universalarchive
 58@@ -0,0 +1,6 @@
 59+#compdef ua
 60+
 61+_arguments \
 62+  "1:archive format:(7z bz2 gz lzma lzo rar tar tar.bz2 tar.gz tar.lzma tar.xz tar.Z tbz tgz tlz txz tZ xz Z zip zst)" \
 63+  "*:input files:_files" \
 64+&& return 0
 65diff --git a/plugins/universalarchive/universalarchive.plugin.zsh b/plugins/universalarchive/universalarchive.plugin.zsh
 66new file mode 100644
 67index 0000000000000000000000000000000000000000..5a661a5b0d8cc9eeb629930ef04208f5caf2bd90
 68--- /dev/null
 69+++ b/plugins/universalarchive/universalarchive.plugin.zsh
 70@@ -0,0 +1,70 @@
 71+function ua() {
 72+  local usage=\
 73+"Archive files and directories using a given compression algorithm.
 74+
 75+Usage:   $0 <format> <files>
 76+Example: $0 tbz PKGBUILD
 77+
 78+Supported archive formats are:
 79+7z, bz2, gz, lzma, lzo, rar, tar, tbz (tar.bz2), tgz (tar.gz),
 80+tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst."
 81+
 82+  if [[ $# -lt 2 ]]; then
 83+    echo >&2 "$usage"
 84+    return 1
 85+  fi
 86+
 87+  local ext="$1"
 88+  local input="$(realpath "$2")"
 89+
 90+  shift
 91+
 92+  if [[ ! -e "$input" ]]; then
 93+    echo >&2 "$input not found"
 94+    return 1
 95+  fi
 96+
 97+  # generate output file name
 98+  local output
 99+  if [[ $# -gt 1 ]]; then
100+    output="$(basename "${input%/*}")"
101+  elif [[ -f "$input" ]]; then
102+    output="$(basename "${input%.[^.]*}")"
103+  elif [[ -d "$input" ]]; then
104+    output="$(basename "${input}")"
105+  fi
106+
107+  # if output file exists, generate a random name
108+  if [[ -f "${output}.${ext}" ]]; then
109+    output=$(mktemp "${output}_XXX") && rm "$output" || return 1
110+  fi
111+
112+  # add extension
113+  output="${output}.${ext}"
114+
115+  # safety check
116+  if [[ -f "$output" ]]; then
117+    echo >&2 "output file '$output' already exists. Aborting"
118+    return 1
119+  fi
120+
121+  case "$ext" in
122+    7z)           7z u                        "${output}"   "${@}" ;;
123+    bz2)          bzip2 -vcf                  "${@}" > "${output}" ;;
124+    gz)           gzip -vcf                   "${@}" > "${output}" ;;
125+    lzma)         lzma -vc -T0                "${@}" > "${output}" ;;
126+    lzo)          lzop -vc                    "${@}" > "${output}" ;;
127+    rar)          rar a                       "${output}"   "${@}" ;;
128+    tar)          tar -cvf                    "${output}"   "${@}" ;;
129+    tbz|tar.bz2)  tar -cvjf                   "${output}"   "${@}" ;;
130+    tgz|tar.gz)   tar -cvzf                   "${output}"   "${@}" ;;
131+    tlz|tar.lzma) XZ_OPT=-T0 tar --lzma -cvf  "${output}"   "${@}" ;;
132+    txz|tar.xz)   XZ_OPT=-T0 tar -cvJf        "${output}"   "${@}" ;;
133+    tZ|tar.Z)     tar -cvZf                   "${output}"   "${@}" ;;
134+    xz)           xz -vc -T0                  "${@}" > "${output}" ;;
135+    Z)            compress -vcf               "${@}" > "${output}" ;;
136+    zip)          zip -rull                   "${output}"   "${@}" ;;
137+    zst)          zstd -c -T0                 "${@}" > "${output}" ;;
138+    *) echo >&2 "$usage"; return 1 ;;
139+  esac
140+}