From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Prompt theme: Show the original job text after foregrounding
Date: Mon, 15 Aug 2011 23:05:07 -0700 [thread overview]
Message-ID: <110815230507.ZM3115@torch.brasslantern.com> (raw)
For years I've been using a two-line prompt that displays the previous
command and its exit status (represented by highlighting) in the first
of the two lines. You may have seen this as "prompt bart" in the theme
system distributed with zsh.
In that theme, the previous command is extracted from the history like
so:
zmodload zsh/parameter
psvar[7]="$history[$[HISTCMD-1]]"
A few days ago I had backgrounded a command. I brought it into the
foreground again, after which it exited with a nonzero status. This
left [fg] in reverse video in the upper line of my prompt; not very
informative. It occurred to me that it might be possible to recover
the text of the command that was foregrounded, so that I could easily
see what actual command had failed -- and in fact it was even easier
than I expected.
The doc for the zsh/parameter module describes the jobdirs, jobtexts,
and jobstates associative arrays thusly:
The keys of the associative arrays are usually valid job numbers,
and these are the values output with, for example, ${(k)jobdirs}.
Non-numeric job references may be used when looking up a value;
for example, ${jobdirs[%+]} refers to the current job.
What this means is that anything you can pass to the "jobs" command
to identify a job, can be used as a key in any of these associations.
So the text of the job that will be resumed by
fg
is given by
$jobtexts[%+]
With that, it's possible to write a preexec function that recognizes
the "fg" command and looks up the job text for the command that is
about to be resumed. This can then be stashed in a variable for use
by precmd when it builds the next prompt; or it can be poked into the
window title bar, etc.
There are still a few gotchas -- compound commands are annoying to
interpret even with ${(z)...} to handle the syntax, and multiple jobs
can be resumed by a single fg -- but a function that handles all the
common cases is not hard to write. See prompt_bart_preexec below.
Also handling "wait" is left as an exercise. I can't remember the
last time I used "wait" in an interactive shell except when testing
a zsh bugfix, so this doesn't seem like a significant omission.
--- current/Functions/Prompts/prompt_bart_setup 2009-05-12 08:14:11.000000000 -0700
+++ Functions/Prompts/prompt_bart_setup 2011-08-15 21:50:05.000000000 -0700
@@ -67,15 +67,38 @@
}
integer PSCOL=1
+typeset PSCMD=
+
+prompt_bart_preexec () {
+ setopt localoptions noxtrace noksharrays unset
+ local -a cmd; cmd=( ${(z)3} )
+ if [[ $cmd[1] = fg ]]
+ then
+ shift cmd
+ cmd[1]=${cmd[1]:-%+}
+ fi
+ if [[ $#cmd -eq 1 && $cmd[1] = %* ]]
+ then
+ PSCMD=$jobtexts[$cmd[1]]
+ elif [[ -o autoresume && -n $jobtexts[%?$2] ]]
+ then
+ PSCMD=$jobtexts[%?$2]
+ else
+ PSCMD=$2
+ fi
+ return 0
+}
prompt_bart_precmd () {
setopt localoptions noxtrace noksharrays unset
local zero='%([BSUbfksu]|[FB]{*})' escape colno lineno
+ : "${PSCMD:=$history[$[HISTCMD-1]]}" # Default to history text
+
# Using psvar here protects against unwanted promptsubst expansions.
- psvar[7]="$history[$[HISTCMD-1]]" # Use history text, not just number
- psvar[8]='' # No padding until we compute it
+ psvar[7]="$PSCMD"
+ psvar[8]='' # No padding until we compute it
psvar[9]=()
typeset -g PSCOL
@@ -153,6 +176,7 @@
repeat 1 case "$1:l" in
(off|disable)
add-zsh-hook -D precmd "prompt_*_precmd"
+ add-zsh-hook -D preexec "prompt_*_preexec"
functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_bart_winch}"
[[ $prompt_theme[1] = bart ]] && PS1=${${(f)PS1}[-1]}
return 1
@@ -182,6 +206,7 @@
# Paste our special commands into precmd and TRAPWINCH
add-zsh-hook precmd prompt_bart_precmd
+ add-zsh-hook preexec prompt_bart_preexec
functions[TRAPWINCH]="${functions[TRAPWINCH]//prompt_bart_winch}
prompt_bart_winch"
next reply other threads:[~2011-08-16 6:05 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-16 6:05 Bart Schaefer [this message]
2011-08-28 16:16 ` Vin Shelton
2011-08-29 15:44 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=110815230507.ZM3115@torch.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).