[pulseaudio-discuss] [PATCH 1/6] shell-completion: zsh: Move duplicated code into a function

Tanu Kaskinen tanu.kaskinen at linux.intel.com
Sat Aug 16 04:48:14 PDT 2014


This is just refactoring, no functional changes.
---
 shell-completion/pulseaudio-zsh-completion.zsh | 88 ++++++++++----------------
 1 file changed, 34 insertions(+), 54 deletions(-)

diff --git a/shell-completion/pulseaudio-zsh-completion.zsh b/shell-completion/pulseaudio-zsh-completion.zsh
index ef279f1..243c1b4 100644
--- a/shell-completion/pulseaudio-zsh-completion.zsh
+++ b/shell-completion/pulseaudio-zsh-completion.zsh
@@ -1,8 +1,17 @@
 #compdef pulseaudio pactl pacmd pacat paplay parec parecord padsp pasuspender
 
+_set_remote() {
+    for (( i = 0; i < ${#words[@]}; i++ )) do
+        if [[ ${words[$i]} == -s ]]; then
+            remote="-s ${words[$i+1]}"
+            break;
+        fi
+    done
+}
+
 _devices() {
     local -a _device_list
-    local cmd _device _device_description _remote_cmd
+    local cmd _device _device_description
 
     if [[ $service == pactl  || $service == pacmd ]]; then
         case $words[$((CURRENT - 1))] in
@@ -55,15 +64,8 @@ _devices() {
 
     fi
 
-    for (( i = 0; i < ${#words[@]}; i++ )) do
-        if [[ ${words[$i]} == -s ]]; then
-            _remote_cmd="-s ${words[$i+1]}"
-            break;
-        fi
-    done
-
     for target in $cmd; do
-        for device_info in ${(ps:\n\n:)"$(_call_program device_tag "pactl $_remote_cmd list $target 2> /dev/null")"}; do
+        for device_info in ${(ps:\n\n:)"$(_call_program device_tag "pactl $remote list $target 2> /dev/null")"}; do
             for line in ${(f)device_info}; do
                 if [[ $target == (sink-inputs|source-outputs) ]]; then
                     if [[ $line == (Sink*Input|Source*Output)* ]]; then
@@ -98,18 +100,11 @@ _devices() {
 
 _profiles() {
     local -a _profile_list
-    local _current_card _raw_profiles _profile_name _profile_description _remote_cmd
+    local _current_card _raw_profiles _profile_name _profile_description
 
     _current_card=$words[$((CURRENT - 1))]
 
-    for (( i = 0; i < ${#words[@]}; i++ )) do
-        if [[ ${words[$i]} == -s ]]; then
-            _remote_cmd="-s ${words[$i+1]}"
-            break;
-        fi
-    done
-
-    for card in ${(ps:\n\n:)"$(_call_program profiles_tag "pactl $_remote_cmd list cards 2> /dev/null")"}; do
+    for card in ${(ps:\n\n:)"$(_call_program profiles_tag "pactl $remote list cards 2> /dev/null")"}; do
         if [[ $card == *$_current_card* ]]; then
             _raw_profiles=${card##*Profiles:}
             _raw_profiles=${_raw_profiles%%Active Profile:*}
@@ -130,7 +125,7 @@ _profiles() {
 
 _ports() {
     local -a _port_list
-    local _raw_ports _port_name _port_description _current_device _remote_cmd
+    local _raw_ports _port_name _port_description _current_device
 
     case $words[$((CURRENT - 2))] in
         set-sink-port) cmd="sinks";;
@@ -140,14 +135,7 @@ _ports() {
 
     _current_device=$words[$((CURRENT - 1))]
 
-    for (( i = 0; i < ${#words[@]}; i++ )) do
-        if [[ ${words[$i]} == -s ]]; then
-            _remote_cmd="-s ${words[$i+1]}"
-            break;
-        fi
-    done
-
-    for device in ${(ps:\n\n:)"$(_call_program port_tag "pactl $_remote_cmd list $cmd 2> /dev/null")"}; do
+    for device in ${(ps:\n\n:)"$(_call_program port_tag "pactl $remote list $cmd 2> /dev/null")"}; do
         if [[ $device == *Ports:* && $device == *$_current_device* ]]; then
             _raw_ports=${device##*Ports:}
             _raw_ports=${_raw_ports%%Active Port:*}
@@ -168,16 +156,9 @@ _ports() {
 
 _cards(){
     local -a _card_list
-    local _card _cad_name _remote_cmd
-
-    for (( i = 0; i < ${#words[@]}; i++ )) do
-        if [[ ${words[$i]} == -s ]]; then
-            _remote_cmd="-s ${words[$i+1]}"
-            break;
-        fi
-    done
+    local _card _cad_name
 
-    for card_info in ${(ps:\n\n:)"$(_call_program card_tag "pactl $_remote_cmd list cards 2> /dev/null")"}; do
+    for card_info in ${(ps:\n\n:)"$(_call_program card_tag "pactl $remote list cards 2> /dev/null")"}; do
         for line in ${(f)card_info}; do
             if [[ $line == *Name:* ]]; then
                 _card=${line#*: }
@@ -201,16 +182,9 @@ _all_modules(){
 }
 
 _loaded_modules(){
-    local -a _loaded_modules_list _remote_cmd
+    local -a _loaded_modules_list
 
-    for (( i = 0; i < ${#words[@]}; i++ )) do
-        if [[ ${words[$i]} == -s ]]; then
-            _remote_cmd="-s ${words[$i+1]}"
-            break;
-        fi
-    done
-
-    for module in ${(f)"$(_call_program modules_tag "pactl $_remote_cmd list modules short 2> /dev/null")"}; do
+    for module in ${(f)"$(_call_program modules_tag "pactl $remote list modules short 2> /dev/null")"}; do
         _loaded_modules_list+=(${${(ps:\t:)module}[1]}:${${(ps:\t:)module}[2]})
     done
     _describe 'module list' _loaded_modules_list
@@ -226,16 +200,9 @@ _resample_methods() {
 
 _clients() {
     local -a _client_list
-    local _client _client_description _remote_cmd
+    local _client _client_description
 
-    for (( i = 0; i < ${#words[@]}; i++ )) do
-        if [[ ${words[$i]} == -s ]]; then
-            _remote_cmd="-s ${words[$i+1]}"
-            break;
-        fi
-    done
-
-    for client_info in ${(ps:\n\n:)"$(_call_program clients_tag "pactl $_remote_cmd list clients 2> /dev/null")"}; do
+    for client_info in ${(ps:\n\n:)"$(_call_program clients_tag "pactl $remote list clients 2> /dev/null")"}; do
         for line in ${(f)client_info}; do
             if [[ $line == Client[[:space:]]#* ]]; then
                 _client=${line#*\#}
@@ -257,6 +224,8 @@ _pacat_file_formats() {
 }
 
 _pactl_completion() {
+    _set_remote
+
     _pactl_command(){
         _pactl_commands=(
             'help: show help and exit'
@@ -459,6 +428,8 @@ _padsp_completion() {
 
 # TODO channel map completion
 _pacat_completion() {
+    _set_remote
+
     _pacat_sample_formats=('s16le' 's16be' 'u8' 'float32le' 'float32be'
         'ulaw' 'alaw' 's32le' 's32be' 's24le' 's24-32le' 's24-32be')
 
@@ -535,6 +506,15 @@ _pulseaudio_completion() {
 _pulseaudio() {
     local state line curcontext="$curcontext"
 
+    # Some commands, like pactl and pacat, have an option for specifying the
+    # server address, like "--server=somehost". If that option is set, then the
+    # helper commands that are run as part of the autocompletion need to use
+    # that same option. The option is saved in this variable in _set_remote(),
+    # which is called in the beginning of _pactl_completion() and others. The
+    # autocompletion commands can then find the option in that variable if the
+    # option is set.
+    local remote
+
     case $service in
         pulseaudio) _pulseaudio_completion;;
         pactl) _pactl_completion;;
-- 
1.9.3



More information about the pulseaudio-discuss mailing list