[pacman-dev] Add pacman-conf zsh completions

Message ID 20200822032002.61651-1-rpigott@berkeley.edu
State New
Headers show
Series
  • [pacman-dev] Add pacman-conf zsh completions
Related show

Commit Message

Ronan Pigott Aug. 22, 2020, 3:20 a.m. UTC
From: Ronan Pigott <rpigott@berkeley.edu>

Signed-off-by: Ronan Pigott <rpigott@berkeley.edu>
---
 scripts/completion/zsh_completion.in | 96 +++++++++++++++++++++++++++-
 1 file changed, 95 insertions(+), 1 deletion(-)

Comments

Allan McRae Sept. 3, 2020, 2:56 a.m. UTC | #1
On 22/8/20 1:20 pm, Ronan Pigott wrote:
> From: Ronan Pigott <rpigott@berkeley.edu>
> 
> Signed-off-by: Ronan Pigott <rpigott@berkeley.edu>
> ---

This looks good to me.

Allan
Andrew Gregory Sept. 4, 2020, 1:38 a.m. UTC | #2
On 08/21/20 at 08:20pm, Ronan Pigott wrote:
> From: Ronan Pigott <rpigott@berkeley.edu>
> 
> Signed-off-by: Ronan Pigott <rpigott@berkeley.edu>

Sure, I guess.  pacman-conf is meant for use in scripts; who on Earth
is running it in a terminal?
Ronan Pigott Sept. 4, 2020, 2:03 a.m. UTC | #3
On Thu Sep 3, 2020 at 11:38 AM MST, Andrew Gregory wrote:
>
> Sure, I guess. pacman-conf is meant for use in scripts; who on Earth
> is running it in a terminal?

I dunno. Anything useful in scripts can be useful in an interactive
shell. No one is gonna bother without completions though.

I wanted a list of servers enabled for my repo so I tried
'pacman-conf -r myrepo Server' but no completions makes it kind of a
bummer to use.

It's a good tool, but even better with completions. Why not?
Eli Schwartz Sept. 4, 2020, 2:24 a.m. UTC | #4
On 9/3/20 9:38 PM, Andrew Gregory wrote:
> On 08/21/20 at 08:20pm, Ronan Pigott wrote:
>> From: Ronan Pigott <rpigott@berkeley.edu>
>>
>> Signed-off-by: Ronan Pigott <rpigott@berkeley.edu>
> 
> Sure, I guess.  pacman-conf is meant for use in scripts; who on Earth
> is running it in a terminal?

It can be pretty useful for running without options in order to
copy-paste your pacman.conf for help, but that's again not really a use
case for completions.

...

I guess we want the same completions in bash_completion.in, but given I
don't really have a good use for it myself, I'm not sure I'll bother
writing it myself...
Allan McRae Sept. 4, 2020, 2:34 a.m. UTC | #5
On 4/9/20 12:24 pm, Eli Schwartz wrote:
> On 9/3/20 9:38 PM, Andrew Gregory wrote:
>> On 08/21/20 at 08:20pm, Ronan Pigott wrote:
>>> From: Ronan Pigott <rpigott@berkeley.edu>
>>>
>>> Signed-off-by: Ronan Pigott <rpigott@berkeley.edu>
>>
>> Sure, I guess.  pacman-conf is meant for use in scripts; who on Earth
>> is running it in a terminal?
> 
> It can be pretty useful for running without options in order to
> copy-paste your pacman.conf for help, but that's again not really a use
> case for completions.

I was working on the principle, that if someone submitted them, we can
include them.  Adds no overhead, beyond keeping them up to date.

> I guess we want the same completions in bash_completion.in, but given I
> don't really have a good use for it myself, I'm not sure I'll bother
> writing it myself...

If someone submits, I will include them.  If not, I don't care.

A

Patch

diff --git a/scripts/completion/zsh_completion.in b/scripts/completion/zsh_completion.in
index 92fc2382..e4bf3312 100644
--- a/scripts/completion/zsh_completion.in
+++ b/scripts/completion/zsh_completion.in
@@ -1,4 +1,4 @@ 
-#compdef pacman pacman.static=pacman pacman-key makepkg
+#compdef pacman pacman.static=pacman pacman-conf pacman-key makepkg
 
 # copy this file to /usr/share/zsh/site-functions/_pacman
 
@@ -496,6 +496,97 @@  _pacman_zsh_comp() {
 	esac
 }
 
+_pacman_conf_general_directives=(
+	'RootDir'
+	'DBPath'
+	'CacheDir'
+	'HookDir'
+	'GPGDir'
+	'LogFile'
+	'HoldPkg'
+	'IgnorePkg'
+	'NoUpgrade'
+	'NoExtract'
+	'Architecture'
+	'XferCommand'
+	'UseSyslog'
+	'Color'
+	'TotalDownload'
+	'CheckSpace'
+	'VerbosePkgLists'
+	'DisableDownloadTimeout'
+	'NoProgressBar'
+	'ParallelDownloads'
+	'CleanMethod'
+	'SigLevel'
+	'LocalFileSigLevel'
+	'RemoteFileSigLevel'
+)
+
+_pacman_conf_repo_directives=(
+	'Server'
+	'SigLevel'
+	'Usage'
+)
+
+_pacman_conf_completions_repositories() {
+	local -a repositories
+	# If the user specified an alternate config, use those repos
+	repositories=($(pacman-conf ${(kv)opt_args[(I)-c|--config]} --repo-list ))
+	typeset -U repositories
+	compadd "$@" -a repositories
+}
+
+_pacman_conf_directive() {
+	# Directives use a case-insensitive matcher-list
+	local casematch="m:{[:lower:][:upper:]}={[:upper:][:lower:]}"
+
+	if [[ -n ${opt_args[(I)-r|--repo]} ]]; then
+		compadd -M "$casematch" "$@" -a _pacman_conf_repo_directives
+	else
+		if [[ $words[$CURRENT] == [iI][lL]* ]]; then
+			compadd -M "$casematch" "$@" ILoveCandy # Secret completion!
+		else
+			compadd -M "$casematch" "$@" -a _pacman_conf_general_directives
+		fi
+	fi
+}
+
+_pacman_conf_commands=(
+	{-l,--repo-list}'[List configured repositories]:*: :->repo_list'
+	{-h,--help}'[Output systax and command line options]:*: :->complete'
+	{-V,--version}'[Display version and exit]:*: :->complete'
+)
+
+_pacman_conf_options=(
+	'(-r --repo)'{-r,--repo=}'[Query options for a specific repository]:package repo:_pacman_conf_completions_repositories'
+	'(-v --verbose)'{-v,--verbose}'[Always show directive names]'
+)
+
+_pacman_conf_options_common=(
+	'*'{-c,--config=}'[Specify an alternate configuration file]: :_files'
+	'*'{-R,--rootdir=}'[Specify an alternate insallation root]: :_files'
+)
+
+_pacman_conf() {
+	_arguments -s : \
+		"$_pacman_conf_commands[@]" \
+		"$_pacman_conf_options_common[@]" \
+		"$_pacman_conf_options[@]" \
+		'*:pacman directive:_pacman_conf_directive'
+
+	case $state in
+		repo_list)
+			_arguments -s \
+				'(-l --repo-list)'{-l,--repo-list} \
+				"$_pacman_conf_options_common[@]"
+			;;
+		*)
+			_message "no more arguments"
+			;;
+	esac
+}
+
 _key_shortopts=(
 	'-h[show help]'
 	'-a[Add the specified keys (empty for stdin)]: :_files'
@@ -704,6 +795,9 @@  _pacman_comp() {
 		makepkg)
 			_makepkg "$@"
 			;;
+		pacman-conf)
+			_pacman_conf "$@"
+			;;
 		pacman-key)
 			_pacman_key "$@"
 			;;