* completion function for system_profiler (OS X)
@ 2014-02-05 12:00 luc
2014-02-05 17:23 ` Jun T.
0 siblings, 1 reply; 5+ messages in thread
From: luc @ 2014-02-05 12:00 UTC (permalink / raw)
To: zsh-users
[-- Attachment #1: Type: text/plain, Size: 1101 bytes --]
Hello list menbers,
I'm writing a completion function for the OS X command system_profiler. (I
did't find one preinstalled with `find $fpath -name _system_profiler` so I
assume there isn't one already)
The manpage for system_profiler says:
> SYNOPSIS
> system_profiler [-usage]
> system_profiler [-listDataTypes]
> system_profiler [-xml] dataType1 ... dataTypeN
> system_profiler [-xml] [-detailLevel level]
My problem is the third posibility: I do not know how to tell the completion
system to allow several dataTypes after the command. I store the dataTypes in
an array which I populate with `system_profiler -listDataTypes`. How can I
tell zsh that any of these strings may follow in any order several times,
unless -usage or -listDataTypes is given? My code so far is attached.
Note that the man page is not correct at one point: It is possible to specify
both -detailLevel and some dataTypes. For example `system_profiler
-detailLevel mini SPAirPortDataType` differs from `system_profiler -detailLevel
full SPAirPortDataType`
Thank you for any help or hints
Lucas
[-- Attachment #2: _system_profiler --]
[-- Type: text/plain, Size: 762 bytes --]
#compdef system_profiler
typeset -A opt_args
local context state line
local -a _data_types
# TODO: Should this be static? Calling `system_profiler -listDataTypes` takes
# about 0.07-0.08 sec. Does this list ever change (between different versions
# of OS X)?
_data_types=$(_call_program path system_profiler -listDataTypes 2>/dev/null)
_arguments \
'(-listDataTypes -detailLevel -timeout -usage)-usage' \
'(-listDataTypes -usage)-xml[generate xml output]' \
'(-listDataTypes -xml -detailLevel -timeout -usage)-listDataTypes[lists the available datatypes]' \
'(-listDataTypes -usage)-detailLevel[level of detail for the report]:detail level:(mini basic full)' \
'(-listDataTypes -usage)-timeout+[maximum time to wait in seconds]' \
&& return 0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion function for system_profiler (OS X)
2014-02-05 12:00 completion function for system_profiler (OS X) luc
@ 2014-02-05 17:23 ` Jun T.
2014-02-06 1:50 ` luc
0 siblings, 1 reply; 5+ messages in thread
From: Jun T. @ 2014-02-05 17:23 UTC (permalink / raw)
To: zsh-users
2014/02/05 21:00, luc <luc.lists@gmail.com> wrote:
> My problem is the third posibility: I do not know how to tell the completion
> system to allow several dataTypes after the command. I store the dataTypes in
> an array which I populate with `system_profiler -listDataTypes`. How can I
> tell zsh that any of these strings may follow in any order several times,
> unless -usage or -listDataTypes is given? My code so far is attached.
You can simply use '*:message:action' as a spec for _arguments. This can
complete any number of normal arguments. As the 'action', you can use
'(val1 val2 ...)' as you have already used for the option -detailLevel.
So the simplest possibility may be the following:
-----------
#compdef system_profiler
local context state state_descr line
typeset -A opt_args
local -a _data_types
_data_types=( ${${(f)"$(_call_program datatypes system_profiler -listDataTypes 2>/dev/null)"}[2,-1]} )
_arguments \
'(- *)-usage' \
'(- *)-listDataTypes[lists the available datatypes]' \
'(-listDataTypes -usage)-xml[generate xml output]' \
'(-listDataTypes -usage)-detailLevel[level of detail for the report]:detail level:(mini basic full)' \
'(-listDataTypes -usage)-timeout+[maximum time to wait in seconds]' \
'*:data type:'"($_data_types)"
-----------
Output from $(system_profiler -listDataTypes) contains a header line,
which is removed by ( ${$(f)"$(command)"}[2,-1]} ).
You are using _call_program so a user can replace the command by using
zstyle. But the 1st line of the output from the user-specified command
will also be removed.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion function for system_profiler (OS X)
2014-02-05 17:23 ` Jun T.
@ 2014-02-06 1:50 ` luc
2014-02-06 9:41 ` Peter Stephenson
2014-02-06 15:44 ` Bart Schaefer
0 siblings, 2 replies; 5+ messages in thread
From: luc @ 2014-02-06 1:50 UTC (permalink / raw)
To: Zsh Mailinglists
[-- Attachment #1: Type: text/plain, Size: 725 bytes --]
Hello Jun,
> You can simply use '*:message:action' as a spec for _arguments.
This works perfectly.
> Output from $(system_profiler -listDataTypes) contains a header line,
> which is removed by ( ${$(f)"$(command)"}[2,-1]} ).
> You are using _call_program so a user can replace the command by using
> zstyle. But the 1st line of the output from the user-specified command
> will also be removed.
I am now hardcoding the data types into the array as the call for
`system_profiler -listDataTypes` is taking a lot of time sometimes. I
think this list hardly ever changes (in new versions of OS X?).
I will attach my "final" version again. Is it reasonalble (or desired)
to commit this upsream?
Thank you for your help
Lucas
[-- Attachment #2: _system_profiler --]
[-- Type: text/plain, Size: 1311 bytes --]
#compdef system_profiler
typeset -A opt_args
local context state state_descr line
local -a _data_types
# TODO: Should this be static? Calling `system_profiler -listDataTypes` takes
# about 0.07-0.08 secs on my machine. Does this list ever change (between
# different versions of OS X)?
_data_types=( SP{AirPort,Applications,Audio,Bluetooth,Camera,CardReader,Component,ConfigurationProfile,DeveloperTools,Diagnostics,DisabledSoftware,DiscBurning,Displays,Ethernet,Extensions,FibreChannel,FireWire,Firewall,Fonts,Frameworks,Hardware,HardwareRAID,InstallHistory,Logs,ManagedClient,Memory,Network,NetworkLocation,NetworkVolume,PCI,ParallelATA,ParallelSCSI,Power,PrefPane,Printers,PrintersSoftware,SAS,SPI,SerialATA,Software,StartupItem,Storage,SyncServices,Thunderbolt,USB,UniversalAccess,WWAN}DataType )
# the dynamic alternative is:
#_data_types=( ${${(f)"$(_call_program path system_profiler -listDataTypes 2>/dev/null)"}[2,-1]} )
_arguments \
'(- *)-usage' \
'(- *)-listDataTypes[lists the available datatypes]' \
'(-listDataTypes -usage)-xml[generate xml output]' \
'(-listDataTypes -usage)-detailLevel[level of detail for the report]:detail level:(mini basic full)' \
'(-listDataTypes -usage)-timeout+[maximum time to wait in seconds]' \
'(-listDataTypes -usage)*:data type:'"($_data_types)"
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion function for system_profiler (OS X)
2014-02-06 1:50 ` luc
@ 2014-02-06 9:41 ` Peter Stephenson
2014-02-06 15:44 ` Bart Schaefer
1 sibling, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-02-06 9:41 UTC (permalink / raw)
To: Zsh Mailinglists
On Thu, 06 Feb 2014 02:50:34 +0100
luc <luc.lists@gmail.com> wrote:
> I will attach my "final" version again. Is it reasonalble (or desired)
> to commit this upsream?
Thanks, I've done that.
pws
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion function for system_profiler (OS X)
2014-02-06 1:50 ` luc
2014-02-06 9:41 ` Peter Stephenson
@ 2014-02-06 15:44 ` Bart Schaefer
1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-02-06 15:44 UTC (permalink / raw)
To: Zsh Mailinglists
On Feb 6, 2:50am, luc wrote:
}
} I am now hardcoding the data types into the array as the call for
} `system_profiler -listDataTypes` is taking a lot of time sometimes. I
} think this list hardly ever changes (in new versions of OS X?).
You could also establish a cache policy which updates only if the
operating system has changed, and populate the cache the first time
system_profiler is completed.
--
Barton E. Schaefer
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-02-07 1:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-05 12:00 completion function for system_profiler (OS X) luc
2014-02-05 17:23 ` Jun T.
2014-02-06 1:50 ` luc
2014-02-06 9:41 ` Peter Stephenson
2014-02-06 15:44 ` Bart Schaefer
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).