*BSD News Article 14087


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!news.Hawaii.Edu!ames!haven.umd.edu!uunet!pipex!marble.uknet.ac.uk!uknet!bgers.co.uk!carp!benn
From: benn@bgers.co.uk (Ian Benn)
Newsgroups: comp.unix.bsd
Subject: Re: Esc sequences in prompts (why doesn't it wor
Message-ID: <C5200D.H8p@bgers.co.uk>
Date: 6 Apr 93 08:38:36 GMT
References: <1993Apr5.170600.24477@mnemosyne.cs.du.edu>
Sender: news@bgers.co.uk
Reply-To: benn@bgers.co.uk
Organization: British Gas plc, ERS
Lines: 107

Try using the tcsh aliases cwdcmd (which executes when you change directory)
and precmd which executes each time before the prompt is displayed.

The following code sets these aliases to change the title of a shelltool
to the current directory each time you cd, and to display the last 5 segments
of the current path at the right hand side of the screen after each command

---------------- Cut here ---------------

# ------------------
# Aliases for prompt
# ------------------
alias cwdcmd '\\
set d=( );\\
if ("$cwd" != "/") set d=(`/bin/sh ~benn/bin/dirs`);\\
set n=$#d;\\
@ n1=0;\\
@ n2=0;\\
@ n3=0;\\
if ($n > 3) @ n3=$n - 3;\\
if ($n > 2) @ n2=$n - 2;\\
if ($n > 1) @ n1=$n - 1;\\
if ($n == 1) set dir=/$d[$n];\\
if ($n == 2) set dir=/$d[$n1]/$d[$n];\\
if ($n == 3) set dir=/$d[$n2]/$d[$n1]/$d[$n];\\
if ($n == 4) set dir=/$d[$n3]/$d[$n2]/$d[$n1]/$d[$n];\\
if ($n > 4)  set dir=.../$d[$n3]/$d[$n2]/$d[$n1]/$d[$n];\\
if ("$cwd" == "/") set dir=/;\\
set l=`echo $dir | wc -c`;\\
@   l=$WINCOLS - $l;\\
echo -n $ESC"]l${NODENAME}(${TTY}):${cwd}$ESC\"'
#
alias precmd '\\
set d=( );\\
if ("$cwd" != "/") set d=(`/bin/sh ~benn/bin/dirs`);\\
set n=$#d;\\
@ n1=0;\\
@ n2=0;\\
@ n3=0;\\
if ($n > 3) @ n3=$n - 3;\\
if ($n > 2) @ n2=$n - 2;\\
if ($n > 1) @ n1=$n - 1;\\
if ($n == 1) set dir=/$d[$n];\\
if ($n == 2) set dir=/$d[$n1]/$d[$n];\\
if ($n == 3) set dir=/$d[$n2]/$d[$n1]/$d[$n];\\
if ($n == 4) set dir=/$d[$n3]/$d[$n2]/$d[$n1]/$d[$n];\\
if ($n > 4)  set dir=.../$d[$n3]/$d[$n2]/$d[$n1]/$d[$n];\\
if ("$cwd" == "/") set dir=/;\\
set l=`echo $dir | wc -c`;\\
@   l=$WINCOLS - $l;\\
echo "${ESC}[${l}C${dir}
set prompt = "${NODENAME}.${USERNAME}.%h${PUNCT} "

--------------- Cut here ---------------

Notice the prompt variable still has to be set because when tcsh actually
displays the real prompt, it can overwrite what you have just written to
the screen with precmd.

Variables:
   WINCOLS  - Is the width of the current window (typically 80)
   ESC      - Is an escape character
   NODENAME - Is the name of the machine I am logged onto
   PUNCT    - Is the punctuation at the end of my prompt
              (> normally or # for root)

benn/bin/dirs is the following script which 'parses' my current path
into separate tokens (e.g. /home/benn/bin --> home benn bin)

--------------- Cut here ---------------
#!/bin/sh
IFS=/
set `pwd`
echo $*
--------------- Cut here ---------------

Apologies if the above code doesn't work as I have cut it out of
files, cut some bits out and replaced actual escape characters with $ESC 
but if you want Escape chars in your prompt, I am sure you can figure it out.

P.S. I am using SunOS 4.1.3 and tcsh 5.16

Hope this helps ...
---
  __           __
 (_/ _   __   /__)    __  __
\_/_(_|_/ /  /___)_E_/ /_/ /

+--------------------------------------------------------------------------+
| "The best way to stay healthy is not to get ill" - Ian Benn, March 1993  |
+---------------------------------------------+----------------------------+
|     #                                       |                            |
|     ##       Ian Benn                       |                            |
|     ###      Senior Engineer                | Phone  : (+44) 91 216 0202 |
|     ####     Advanced Systems               | Extn   : 2771              |
|    ######    Maths & Computing              | BGTN   : (7446) 2771       |
|   ########   British Gas Plc                | Fax    : (+44) 91 268 3045 |
|  ##########  Research & Technology Division | Telex  : 53470             |
| ##### #####  Engineering Research Station   | E-mail : benn@bgers.co.uk  |
| #### # ####  Harvey Combe                   |      (...uknet!bgers!benn) |
| ### ### ###  Killingworth, PO Box 1LH       |                            |
|  ## ### ##   Newcastle-upon-Tyne, NE99 1LH  |                            |
|   ## # ##    England                        |                            |
|    #####                                    |                            |
+---------------------------------------------+----------------------------+
| The views expressed here are not necessarily those of British Gas Plc    |
+--------------------------------------------------------------------------+