zsh-users
 help / color / mirror / code / Atom feed
* Retrieving the terminal title / setting it using the name of the foreground process
@ 2008-11-25 18:40 Enny Ammis
  2008-11-25 20:24 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Enny Ammis @ 2008-11-25 18:40 UTC (permalink / raw)
  To: zsh-users

Hi,

i'm fairly new to zsh (it's been 4 days now), and i've been spending some time customizing it. one thing i haven't been able to do yet is change a terminal's title so that it contains the name of the currently active process, i.e. "zsh" if i'm simply using the shell, "perl args" if i'm running a perl program, "emacs file.txt" if i'm running emacs and "zsh" again if i hit ^Z and send emacs to the background. 

i haven't found as simple a way to do it as with the name of the current directory. using precmd or that kind of stuff probably wouldn't work, be it only because the name of the foreground app might change without my actually executing a command -- again, ^Z should also trigger a title change. i've tried using preexec, but then it gets mixed up when using e.g. "fg" to come back to a bg'd process. i also wanted to define TRAPTSTP to handle bg'ing, but i get the following error upon starting the shell: "can't trap SIGTSTP in interactive shells" (perhaps it's the wrong signal?)

anyway, another way i've thought of is to have a script (in my case it's in perl) run in the background, and every second call ps, capture the output and recognize the foreground processes in all tty's (most often i have a lot of them, that's why i need a way of differentiating them) and set the titles accordingly. some might argue that it's not very efficient, which might be right, and i welcome all suggestions for alternatives, then again i haven't found any other way. 

anyway, i still have some problems which i'm sure are solvable, but i haven't figured the solutions yet, perhaps someone can give me a hand? 

1) my title already contains information, so i need to *update* it, not simply overwrite it, i.e. i want to append the name of the current foreground process at the end...
e.g. my basic title as set by the precmd function is like this:
username@host:tty pwd ()
and i want the space between the brackets to be filled with the name of the fg process. 

i have thought of two possible ways to do that, both of which i dunno how to implement: 
a) upon creating the title in the precmd function, store it in a cache or something, which my perl script has access to. however, i dunno how to expand the cryptic expression "%n@%m:%l %~ ()" into a nice title from within the function... as i don't believe it's possible to access the environment of any session from a given terminal to retrieve the environment variables like $PWD, i have to use the value generated within the precmd function and extend it. anyone knows how to do that? 
b) retrieve the title of a given tty. the good part is then i wouldn't have to use a cache to store the titles of several tty's. the bad part is i dunno if it's possible. 

2) i can't remember how it's possible to "remotely" (i.e. from another terminal) change a terminal's title. i'm pretty sure i've read somewhere that this can be done by doing something like sending the "usual string" "\e]0;nice title\a" into /dev/ttyN, but that doesn't seem to work -- if i try to do that on the console, i get an error:
> echo "\e]0;abc\a" > /dev/tty5
zsh: keine Berechtigung: /dev/tty5
>
(yeah, sorry, it's german, it means "not authorized" or something like that)
anyone knows how to do that? 
if it's not possible to do that remotely, then there's always the possibility of having one perl script run in each terminal -- not nice, but that would work...

it'd be real cool if someone could give me some hints as to how i can get this on track. thx a lot in advance for your help! 

cheers

Enny



      


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Retrieving the terminal title / setting it using the name of the foreground process
  2008-11-25 18:40 Retrieving the terminal title / setting it using the name of the foreground process Enny Ammis
@ 2008-11-25 20:24 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2008-11-25 20:24 UTC (permalink / raw)
  To: zsh-users

On Tue, 25 Nov 2008 10:40:32 -0800 (PST)
Enny Ammis <enny_ammis@yahoo.com> wrote:
> i'm fairly new to zsh (it's been 4 days now), and i've been spending
> some time customizing it. one thing i haven't been able to do yet is
> change a terminal's title so that it contains the name of the currently
> active process, i.e. "zsh" if i'm simply using the shell, "perl args" if
> i'm running a perl program, "emacs file.txt" if i'm running emacs

The simplest thing you can do is

  preexec() { print -nr $'\033'"]2;$1"$'\a' }

This is designed to avoid problems with the command line to be executed,
passed in $1, being interpreted specially, which is why it's more
complicated than you might first expect.

> and "zsh" again if i hit ^Z and send emacs to the background. 

Well, you can't trap the ^Z itself in an interactive shell.  However,
if you're just trying to get something to happen when you get back to
the prompt, it's easy.  If all you want is to cancel the stuff in the
title bar and replace it with zsh when a programme isn't running in the
foreground:

  precmd() { print -nr $'\033'"]2;zsh"$'\a' }

I'm not sure if that was all there was too it.  You can get the information
about jobs from parameters; in a recent shell, the variable $jobstates,
contained in the zsh/parameter module, is the easiest to manipulate.
See the zshmodules manual.

If you have other information to go into the title you will need to
store that in a shell variable whereever it's generated and add it to
the information that's printed out.

  # wherever
  foo="... some stuff ..."

  # then...
  preexec() { print -nr $'\033'"]2;$foo$1"$'\a' }
  precmd() { print -nr $'\033'"]2;${foo}zsh"$'\a' }

If you want to do something different, short, specific questions are
most likely to get a useful answer.

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-11-25 20:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-25 18:40 Retrieving the terminal title / setting it using the name of the foreground process Enny Ammis
2008-11-25 20:24 ` Peter Stephenson

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).