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

Re: fg jobs info



On Sep 3, 12:31pm, Matthew Wozniski wrote:
} Subject: Re: fg jobs info
}
} On Sun, Sep 02, 2007 at 10:59:53AM -0700, Bart Schaefer wrote:
} ...
} > The "jobs" command should accept all the same job-identifier patterns
} > as "fg", and in recent zsh "jobs" doesn't lose track of the job list
} > when run in a subshell
} 
} In how recent a zsh?  I can't seem to get that working with 4.3.4.

You're right.  $(jobs) and (jobs) produce different output.  I thought
that because the latter worked, the former would also.

Looks to me as if there's a bug here; compare

    print $(jobs)
    (print $(jobs))

Also, although "jobs" or "jobs -l" prints out the entire jobs listing
even in subshells/pipelines/etc., "jobs %2" (for example) produces a
"no such job" error, which surprised me.  That should at least be
documented.  Try:

    sleep 40 & sleep 30 &
    jobs | cat
    jobs %1 | cat

So the easy way out is not going to work here after all.

On Sep 4, 11:16pm, Atom Smasher wrote:
}
} On Sun, 2 Sep 2007, Bart Schaefer wrote:
} 
} > Have a look at the variables jobdirs, jobtexts, and jobstates in the 
} > zsh/parameter module.  Or there's a lazier way out:
} =============
} 
} that seems reasonably elegant... as long as a) there's only one
} suspended job or b) jobs are only referred to by number :(
}
} if the there's more than one job and either 1) the current job is
} implied or 2) a job is specified not by job number, things get messy.

Not all *that* messy.  "The current job" is

    jobno=${(k)jobstates[(r)*:+:*]}

Something like "fg %fred" is

    jobno=${(k)jobstates[(r)*:?:fred*]}

And "fg %?wilma" is

    jobno=${(k)jobstates[(r)*:?:*wilma*]}

Then you just do

    fgjob="${jobtexts[$jobno]}"
    fg %$jobno

} also, here's a case where (given what i'm trying to do) i'm confident
} that anything would fail (at least partially), short of some
} non-trivial updates to zsh:
} 
} % fg %1 %2

You just have to handle that inside your "fg" function.

    fg() {
	[[ -z $1 ]] && set -- %+
        # Still ignoring all sorts of error handling here ...
	for jobspec
	do
	    case $jobspec in
	    (%<->) jobno=${jobspec#%};;
	    (%+) jobno=${(k)jobstates[(r)*:+:*]};;
	    (%-) jobno=${(k)jobstates[(r)*:-:*]};;
            # Need more quoting than shown for $jobspec below
	    (%[?]*) jobno=${(k)jobstates[(r)*:?:*${jobspec#%?}*]};;
	    (*) jobno=${(k)jobstates[(r)*:?:${jobspec#%}*]};;
	    esac
	    tab-title "$jobtexts[$jobno]"
	    builtin fg %$jobno
	done
    }

Jobs don't get renumbered as others exit, so the jobspecs that come in
are usable throughout.

The case that's going to be pretty difficult to handle is:

    % %2

(That is, bringing a job to the foreground by simply typing its job spec.
Probably need a preexec function to catch that one.)

Incidentally, yet another bug:

    % sleep 30 & sleep 50 &
    % fg %- %-
    [1]  - running    sleep 30
    fg: %3: no such job

Where did I ever ask for job 3 ?



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