* Prompt theme: Show the original job text after foregrounding
@ 2011-08-16 6:05 Bart Schaefer
2011-08-28 16:16 ` Vin Shelton
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2011-08-16 6:05 UTC (permalink / raw)
To: zsh-users
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"
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Prompt theme: Show the original job text after foregrounding
2011-08-16 6:05 Prompt theme: Show the original job text after foregrounding Bart Schaefer
@ 2011-08-28 16:16 ` Vin Shelton
2011-08-29 15:44 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Vin Shelton @ 2011-08-28 16:16 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-users
Hi, Bart -
On Tue, Aug 16, 2011 at 2:05 AM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
> 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.
I've been an enthusiastic user of "prompt bart" for several years. Thank you!
I think your latest change has caused alias expansion to occur for the
upper left prompt. In my case, this has converted the fairly tidy
[l abcde]
to the messier (IMO)
[ls --dereference-command-line-symlink-to-dir -F abcde]
I didn't see a way to disable the preexec hook - is there any way to
customize this?
Thanks,
Vin
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Prompt theme: Show the original job text after foregrounding
2011-08-28 16:16 ` Vin Shelton
@ 2011-08-29 15:44 ` Bart Schaefer
0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2011-08-29 15:44 UTC (permalink / raw)
To: zsh-users
On Aug 28, 12:16pm, Vin Shelton wrote:
}
} I think your latest change has caused alias expansion to occur for the
} upper left prompt.
Indeed, that's a side-effect I hadn't considered. I've almost entirely
replaced aliases with functions now because of the inability to refer
to positional parameters in zsh aliases, so I hadn't noticed. I like
it better without the alias expansion too.
Index: Functions/Prompts/prompt_bart_setup
--- Functions/Prompts/prompt_bart_setup.~1.18.~ 2011-08-16 23:28:44.000000000 -0700
+++ Functions/Prompts/prompt_bart_setup 2011-08-29 08:38:17.000000000 -0700
@@ -70,7 +70,7 @@
typeset PSCMD=
prompt_bart_preexec () {
- setopt localoptions noxtrace noksharrays unset
+ setopt localoptions noxtrace noshwordsplit noksharrays unset
local -a cmd; cmd=( ${(z)3} )
if [[ $cmd[1] = fg ]]
then
@@ -84,7 +84,8 @@
then
PSCMD=$jobtexts[%?$2]
else
- PSCMD=$2
+ # Use history text to avoid alias expansion
+ PSCMD=$history[$HISTCMD]
fi
return 0
}
--
Barton E. Schaefer
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-08-29 15:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-16 6:05 Prompt theme: Show the original job text after foregrounding Bart Schaefer
2011-08-28 16:16 ` Vin Shelton
2011-08-29 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).