prompt wars
zben at umd5.UUCP
zben at umd5.UUCP
Sun Mar 15 08:16:15 AEST 1987
I started by picking up and enhancing some aliases originally posted by
Chris Torek in article <5150 at mimsy.uucp>. I wanted to have the name of
the current directory in the prompt, but some amount of its parentage.
This precluded use of the :t modifier. Via a combination of aliases I
got what I wanted (see below) but it was taking over four SECONDS to do
a change directory. This was considered annoying, so I replaced the mess
with a C program (at the end of this article). The change directory time
is now one second or less.
WHAT I WANTED:
Current machine (no need for domains) and the last two directory names of
the full name of the current directory. Here is an example:
umd5[3] ~ % cd /usr/spool/news/comp/unix/questions
umd5[4] ...unix/questions %
ALIAS, SHELL SCRIPT, AND AWK SOLUTION:
in ".cshrc":
alias set_prompt 'set prompt="`hnm1`[\\!] `dirs|cdt2` % "'
alias cd 'cd \!*; set_prompt'
alias pushd 'pushd \!*; set_prompt'
alias popd 'popd \!*; set_prompt'
set_prompt
in "~/bin/hnm1":
#! /bin/sh
# output user-friendly head of host name
hostname | awk -F. '{ print $1 }'
in "~/bin/cdt2":
#! /bin/sh
# output user-friendly tail of CD name
awk '{print $1}' |
awk -F/ '
(NF>3)||(NF>2)&&(length($1)!=0) {printf "..."}
(NF>1) {printf "%s/",$(NF-1)}
{printf "%s\n",$NF}'
ALIAS AND C PROGRAM SOLUTION:
in ".cshrc":
alias cd 'cd \!*; set prompt="`dirs|genpro`"'
alias pushd 'pushd \!*; set prompt="`dirs|genpro`"'
alias popd 'popd \!*; set prompt="`dirs|genpro`"'
set prompt="`dirs|genpro`"
and the following C program in "~/bin/genpro":
/* Generate prompt string
* Ben Cranston 3/14/87
*
* designed to be called as:
*
* alias cd 'cd \!*; set prompt="`dirs|genpro`"'
*
* builds and outputs string: <hostname> [!] <workdir>
*
* where <hostname> is the name of the current host sans trailing domains
* <workdir> is the current directory, with all but the last two
* directory names replaced by ... for brevity.
*
* I had all this working with "awk" scripts, but it was taking over
* four SECONDS to switch directories. This was considered wasteful.
*
*/
#define BUFFSIZE 512
#include <sys/param.h> /* MAXHOSTNAMELEN */
#include <strings.h> /* string funcs */
main(argc,argv)
int argc;
char **argv;
{
char hostname[MAXHOSTNAMELEN];
char dirs[BUFFSIZE], buff[BUFFSIZE];
char *cp;
char *p1, *p2;
gethostname(hostname,sizeof(hostname));
if ( 0 != (cp=index(hostname,'.')) )
*cp = 0; /* truncate hostname at first period (if any) */
gets(dirs);
if ( 0 != (cp=index(dirs,' ')) )
*cp = 0; /* truncate dir string at first space (if any) */
/* search backwards for slash. if found, temporarily make null and
* search backwards for slash again. if found again, replace string
* to the left of the second slash with "..."
*
* the additional test of (p1!=dirs) is for a special case, a two-string
* explicitly rooted. that is, "cd /etc/ns" will display as /etc/ns
* rather than ...etc/ns
*/
buff[0] = 0;
cp = dirs;
if ( 0 != (p2 = rindex(dirs,'/')) ) {
*p2 = 0;
if ( (0 != (p1 = rindex(dirs,'/'))) && (p1 != dirs) ) {
strcpy(buff,"...");
cp = p1 + 1;
}
*p2 = '/';
}
strcat(buff,cp);
printf("%s[!] %s %% ",hostname,buff);
}
Needless to say this technique provides a skeleton upon which a variety
of personal preferences can be hung. Enjoy!
--
umd5.UUCP <= {seismo!mimsy,ihnp4!rlgvax}!cvl!umd5!zben
Ben Cranston zben @ umd2.UMD.EDU Kingdom of Merryland UniSys 1100/92
umd2.BITNET "via HASP with RSCS"
More information about the Comp.unix.questions
mailing list