Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: multiple background jobs of the same program



In case it helps, here's a function I posted some time ago to enable
completion of jobs along the lines required.  The difference is
essentially that it relies on interactivecomments, putting the
job description in a comment, e.g.

% kill %1  # sleep 10 

so parsing isn't a problem.  Otherwise it's not so different.  Again,
some kind of menu-like completion is best.

# Function to complete jobnames, with a commented description, as
# suggested by Harmanjit Singh.
# Author:  pws@xxxxxx (Peter Stephenson)
# Usage:  with a compctl such as
#   compctl -Q -K jobfunc -x 's[-] p[1,1]' -k signals -- kill

# Make sure interactivecomments is on in the shell running the function.
unsetopt localoptions
setopt interactivecomments

# Use a temporary file to store the jobs, since we can't use a pipe.
local tmpf=/tmp/zshjobs$$

# Get the jobs list: looks like
# [1]  + running    sleep 30
jobs >&! $tmpf
reply=()

local job jobno
while read -A job
do
   # Extract the job no. from the square brackets
   jobno=$job[1]
   jobno=${${jobno#\[}%\]}

   shift job
   # If this job is marked as - or +, the command starts at the
   # fourth field, else at the third.
   [[ $job[1] = [-+] ]] && shift job
   shift job
   # Now the remaining elements of $job contain the command description.
   # Add the whole completion to the reply.
   reply[$#reply+1]="%$jobno  # $job"
done < $tmpf

# Tidy up the job file.
rm $tmpf



Messages sorted by: Reverse Date, Date, Thread, Author