diff --git a/custom-completions/pacman/pacman-completions.nu b/custom-completions/pacman/pacman-completions.nu new file mode 100644 index 00000000..c692664e --- /dev/null +++ b/custom-completions/pacman/pacman-completions.nu @@ -0,0 +1,53 @@ +# nu-version: 0.117.0 +# Completions nativas: opções extraídas da ajuda da versão instalada. +def "nu-complete pacman" [token: record, place: record] { + let operations = [ + {short: h, long: help, description: "Mostrar ajuda"} + {short: V, long: version, description: "Mostrar versão"} + {short: D, long: database, description: "Modificar a base de dados"} + {short: F, long: files, description: "Consultar arquivos nos repositórios"} + {short: Q, long: query, description: "Consultar pacotes instalados"} + {short: R, long: remove, description: "Remover pacotes"} + {short: S, long: sync, description: "Sincronizar e instalar pacotes"} + {short: T, long: deptest, description: "Verificar dependências"} + {short: U, long: upgrade, description: "Instalar arquivo de pacote"} + ] + let words = ($place.command | skip 1) + let operation = ($operations | where {|op| + $op.short not-in [h V] and ($words | any {|word| + $word == $"--($op.long)" or ( + ($word starts-with '-') and not ($word starts-with '--') + and ($word | str substring 1.. | str contains $op.short) + ) + }) + } | first 1) + let text = $token.text + if ($operation | is-empty) { + return ($operations | each {|op| + [ + {value: $"-($op.short)", description: $op.description} + {value: $"--($op.long)", description: $op.description} + ] + } | flatten) + } + if ($text | is-not-empty) and not ($text starts-with '-') { return null } + let help = (^pacman $"-($operation.0.short)" --help | complete) + if $help.exit_code != 0 { return null } + let options = ($help.stdout | lines | parse --regex '^\s+(?:-(?P[A-Za-z]),\s+)?--(?P[a-z][a-z-]*)(?:\s+<[^>]+>)?\s+(?P.+)$') + $options | each {|option| + let long = {value: $"--($option.long)", description: $option.description} + if ($text starts-with '--') or ($option.short | is-empty) { + [$long] + } else { + let prefix = if ($text | str length) > 1 { $text } else { "-" } + [ + {value: $"($prefix)($option.short)", description: $option.description} + $long + ] + } + } | flatten +} + +export extern pacman [ + ...args: external_arg@"nu-complete pacman" +] diff --git a/custom-completions/pacman/readme.md b/custom-completions/pacman/readme.md new file mode 100644 index 00000000..962f6734 --- /dev/null +++ b/custom-completions/pacman/readme.md @@ -0,0 +1,52 @@ +# Pacman completions + +Native Nushell completions for [pacman](https://archlinux.org/pacman/), +the package manager used by Arch Linux and related distributions. + +## Requirements + +- Nushell 0.117.0 or later (tested with 0.117.0). +- `pacman` available on `PATH`. + +Pacman is included with Arch Linux. For documentation and source downloads, +see the [official pacman website](https://archlinux.org/pacman/). +On other distributions, consult your distribution's package repositories +for availability. + +## Usage + +Add this import to your Nushell `config.nu`, adjusting the repository path: + +```nu +use /nu_scripts/custom-completions/pacman/pacman-completions.nu * +``` + +Open a new Nushell session, or run the import in your current session. +Then press Tab after any of these examples: + +```text +pacman - +pacman -Q +pacman -Q - +pacman -S -- +pacman --query -- +``` + +The module suggests operation names and operation-specific flags with +descriptions. For example, `pacman -Q` offers combined short flags +such as `-Qi`, `-Ql`, and `-Qs`. + +## How it works + +A single `extern` passes arguments through without declaring flags. +The native completer identifies the operation and reads its options from +`pacman - --help`, using the installed version's help output. +Neither Fish nor Carapace is required. + +## Limitations + +- Package names and operation-specific argument values are not completed. +- Parsing expects pacman's current option-line format; wrapped description + continuations are not included. +- Combined short-flag suggestions do not check for duplicates or + incompatible flags.