diff options
Diffstat (limited to 'home/.oh-my-zsh/plugins/universalarchive')
3 files changed, 152 insertions, 0 deletions
diff --git a/home/.oh-my-zsh/plugins/universalarchive/README.md b/home/.oh-my-zsh/plugins/universalarchive/README.md new file mode 100644 index 0000000..bcd33ce --- /dev/null +++ b/home/.oh-my-zsh/plugins/universalarchive/README.md @@ -0,0 +1,76 @@ +# universalarchive plugin + +The `universalarchive` plugin provides a convenient command-line interface for archiving files and directories using a wide variety of compression formats - without having to remember the exact syntax for each tool. + +To enable it, add `universalarchive` to the plugins array in your `.zshrc` file: + +```zsh +plugins=(... universalarchive) +``` + +## Features + - Compress files and directories using a simple, unified command: ua <format> <files> + - Automatically detects file/directory names to generate appropriate output names + - Supports fallback naming if an output file already exists + - Works with many common and advanced compression formats + - Designed for simplicity and quick use in the terminal + +## Usage + +Basic command format: +```sh +ua <format> <files...> +``` +- `<format>`: the archive format to use (e.g., `zip`, `tar.gz`, `xz`, `7z`, etc.) +- `<files...>`: one or more files or directories to compress + +## Examples: + +Compresses `notes.txt` and `images` into `notes.zip` +```sh +ua zip notes.txt images/ +``` + +Creates `myproject.tar.gz` +```sh +ua tar.gz myproject/ +``` + +Compresses all .log files into `current_folder.xz` +```sh +ua xz *.log +``` + +The plugin will generate a default archive filename based on the input: + - For a file, the output is derived from the file name without its extension. + - For a directory, it uses the directory name. + - For multiple files, it uses the name of the common parent directory. + + If the output file already exists, a unique filename is generated using `mktemp`. + +## Supported Archive Formats + +| Format | Description | Tool Used | +|:-----------------|:-------------------------------|:-----------------| +| `7z` | 7zip archive | `7z` | +| `bz2` | Bzip2-compressed file | `bzip2` | +| `gz` | Gzip-compressed file | `gzip` | +| `lzma` | LZMA-compressed file | `lzma` | +| `lzo` | LZO-compressed file | `lzop` | +| `rar` | WinRAR archive | `rar` | +| `tar` | Uncompressed tarball | `tar` | +| `tbz`,`tar.bz2` | Tarball compressed with Bzip2 | `tar + bzip2` | +| `tgz`,`tar.gz` | Tarball compressed with Gzip | `tar + gzip` | +| `tlz`,`tar.lzma` | Tarball compressed with LZMA | `tar + lzma` | +| `txz`,`tar.xz` | Tarball compressed with LZMA2 | `tar + xz` | +| `tZ`,`tar.Z` | Tarball compressed with LZW | `tar + compress` | +| `xz` | XZ-compressed file | `xz` | +| `Z` | LZW-compressed file | `compress` | +| `zip` | Standard Zip archive | `zip` | +| `zst` | Zstandard-compressed file | `zstd` | + + > Note: Some formats may require specific tools to be installed on your system (e.g. `7z`, `rar`, `lzop`, `zstd`). Make sure these tools are available in your `$PATH`. + +## Auto-Completion + +The plugin provides tab-completion for supported formats and input files. Type `ua <TAB>` to see available formats, and `ua <format> <TAB>` to browse files. diff --git a/home/.oh-my-zsh/plugins/universalarchive/_universalarchive b/home/.oh-my-zsh/plugins/universalarchive/_universalarchive new file mode 100644 index 0000000..17cfd78 --- /dev/null +++ b/home/.oh-my-zsh/plugins/universalarchive/_universalarchive @@ -0,0 +1,6 @@ +#compdef ua + +_arguments \ + "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)" \ + "*:input files:_files" \ +&& return 0 diff --git a/home/.oh-my-zsh/plugins/universalarchive/universalarchive.plugin.zsh b/home/.oh-my-zsh/plugins/universalarchive/universalarchive.plugin.zsh new file mode 100644 index 0000000..b287c22 --- /dev/null +++ b/home/.oh-my-zsh/plugins/universalarchive/universalarchive.plugin.zsh @@ -0,0 +1,70 @@ +function ua() { + local usage=\ +"Archive files and directories using a given compression algorithm. + +Usage: $0 <format> <files> +Example: $0 tbz PKGBUILD + +Supported archive formats are: +7z, bz2, gz, lzma, lzo, rar, tar, tbz (tar.bz2), tgz (tar.gz), +tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst." + + if [[ $# -lt 2 ]]; then + print -u2 -- "$usage" + return 1 + fi + + local ext="$1" + local input="${2:a}" + + shift + + if [[ ! -e "$input" ]]; then + print -u2 -- "$input not found" + return 1 + fi + + # generate output file name + local output + if [[ $# -gt 1 ]]; then + output="${input:h:t}" + elif [[ -f "$input" ]]; then + output="${input:r:t}" + elif [[ -d "$input" ]]; then + output="${input:t}" + fi + + # if output file exists, generate a random name + if [[ -f "${output}.${ext}" ]]; then + output=$(mktemp "${output}_XXX") && rm "$output" || return 1 + fi + + # add extension + output="${output}.${ext}" + + # safety check + if [[ -f "$output" ]]; then + print -u2 -- "output file '$output' already exists. Aborting" + return 1 + fi + + case "$ext" in + 7z) 7z u "${output}" "${@}" ;; + bz2) bzip2 -vcf "${@}" > "${output}" ;; + gz) gzip -vcf "${@}" > "${output}" ;; + lzma) lzma -vc -T0 "${@}" > "${output}" ;; + lzo) lzop -vc "${@}" > "${output}" ;; + rar) rar a "${output}" "${@}" ;; + tar) tar -cvf "${output}" "${@}" ;; + tbz|tar.bz2) tar -cvjf "${output}" "${@}" ;; + tgz|tar.gz) tar -cvzf "${output}" "${@}" ;; + tlz|tar.lzma) XZ_OPT=-T0 tar --lzma -cvf "${output}" "${@}" ;; + txz|tar.xz) XZ_OPT=-T0 tar -cvJf "${output}" "${@}" ;; + tZ|tar.Z) tar -cvZf "${output}" "${@}" ;; + xz) xz -vc -T0 "${@}" > "${output}" ;; + Z) compress -vcf "${@}" > "${output}" ;; + zip) zip -rull "${output}" "${@}" ;; + zst) zstd -c -T0 "${@}" > "${output}" ;; + *) print -u2 -- "$usage"; return 1 ;; + esac +} |
