omi...@my-deja.com wrote:
> I'm trying to get a prompt like :
> HostnaMe: /usr/export/home/omitka/docs >
Use my script, which functions as a front-end to create an appropriate
prompt, It's a little rough around the edges, but will suffice
robustly for the purposes that you have in mind:
. mkprompt
mkprompt --hostname ': ' --cwd '> '
The advantage to you is that a prompt will be constructed (you may
then unset or delete "mkprompt" -- it is then not further accessed)
that will attempt to ellide the pathname of your cwd by using the
"~/..." notation:
HostnaMe: ~/omitka/docs >
#! /bin/echo error: only source
#*TAG:50241 13:Oct 18 1998:0644:mkprompt:
# Author: Brian Hiles <b...@iname.com>
# Copyright: (c) 1998
# Date: 1998/10/18
# Description: construct a custom PS1 prompt
# Name: mkprompt
# Sccs: @(#)mkprompt.sh 1.3 1998/02 b...@iname.com (Brian Hiles)
# Usage: mkprompt [option|string]...
# Version: 1.03
#01
function mkprompt # [option|string]...
# construct a custom PS1 prompt
{ set -o noglob
typeset IFS TERM=${TERM:-dumb} bell=$(print \\007) esc=$(print \\033)
typeset ext ipost ipre nl='
' ps1 wpost wpre xport xterm
[[ $#$1 = @(0|1--) ]] &&
{ # no arguments: output code suitable for re-input
[[ $PS1 = *\${HOSTNAME}* ]] &&
print -r ': ${HOSTNAME:=$(hostname)}'
[[ $PS1 = *SECONDS* ]] &&
print "SECONDS=\$(date '+%H*3600+%M*60+%S')" &&
print 'typeset -Z2 _h _m _s\ntypeset -i _t'
[[ $PS1 = *PWD* ]] &&
print 'TILDE=\~'
[[ $PS1 = *\${SHLVL}* ]] && print 'typeset -i10 -x SHLVL' \
"\nalias -x ksh='SECONDS=\$SECONDS SHLVL=${SHLVL:-0}+1 ksh'"
[[ " $(typeset +x) " = *' 'PS1' '* ]] &&
print -n 'typeset -x TILDE '
print -r -- "PS1='$PS1'"
return 0
}
#XXX unused options: fgqvy
[[ $#$1 = 1@(-h|--help) ]] &&
{ print -r -- \
'-a|--alert - sound a bell
-b|--bold - turn on bold video
-c|--cwd - show current working directory
-d|--date - show date
-e|--exmark - show an exclamation mark
-h|--history - show history number
-i|--icon - set title of icon with subsequent PS1 text
XXX+i|--unicon - terminate title of icon
-j|--newline - show a newline
-k|--blink - turn on blinking video
-l|--level - show shell level
-m|--dim - turn on dim video
-n|--hostname - show hostname
-o|--status - show exit status of previous command
-p|--prompt - show prompt character (dollar sign or hash mark)
-r|--reverse - show reverse video
-s|--standout - turn on reverse video
-t|--time - show 24-hour time
-u|--user - show username
-w|--window - set title of window (XXXand icon?) with subsequent PS1 text
XXX+w|--unwindow - terminate title of window
-x|--export - export PS1 to subshells
-z|--zero - zero out prompt (use with --window/--icon options.)
XXX-z|--zero - terminate title of window
Without arguments "mkprompt" outputs shell code to recreate the current prompt.'
return 0
}
while (($#))
do case $1 in
(--) break ;;
(-a|--alert)
ATTR_BELL=${ATTR_BELL#$esc} \
: ${ATTR_BELL:=$(tput -T$TERM bel)}
ps1="$ps1${ATTR_BELL:-$bell}" ;;
(-b|--bold)
ATTR_SMSO=${ATTR_SMSO#$esc} \
: ${ATTR_SMSO:=$(tput -T$TERM smso)}
ps1="$ps1$esc$ATTR_SMSO" ;;
(+b|--unbold)
ATTR_RMSO=${ATTR_RMSO#$esc} \
: ${ATTR_RMSO:=$(tput -T$TERM rmso)}
ps1="$ps1$esc$ATTR_RMSO" ;;
(-c|--cwd)
ext="$ext${nl}TILDE=\\~" \
ps1="$ps1\${TILDE[(1-0\${PWD%%@([!/]*|\$HOME*)}1)]" \
ps1="$ps1:-}\${PWD#\$HOME}" ;;
(-d|--date)
# Warning: not dynamically calculated each display
ps1="$ps1$(date +%D)" ;;
(-e|--exmark)
ps1="$ps1!!" ;;
(-h|--history)
ps1="$ps1!" ;;
(-i|--icon)
#if [[ $1 = --icon=* ]]
#then xterm=${1#--icon=}
#else xterm=${1#-i}
#[[ -z $xterm ]] && xterm=$2 shift
#fi
case $xterm in
(630?(-*))
ipre= ipost= ;; #XXX
(dtterm?(-*)|sun-cmd?(-*))
ipre=$esc]L ipost=$esc ;;
(hpterm?(-*))
ipre=$esc\&f-1k\${#PS1}D ipost=' ' ;;
(xterm?(-*))
ipre=$esc]1\; ipost=$bell ;;
esac
${DIAG:+print -ru2 [ $0: xterm: $xterm ]
_inst="$(print -nr -- "$ipre"|od -c|head -1)"
print -ru2 [ preamble: ${_inst#0000000 } ]
_inst="$(print -nr -- "$ipost"|od -c|head -1)"
print -ru2 [ postamble: ${_inst#0000000 } ]}
;;
(-j|--newline)
ps1="$ps1$nl" ;;
(-k|--blink)
ATTR_BLINK=${ATTR_BLINK#$esc} \
ATTR_SGR0=${ATTR_SGR0#$esc} \
: ${ATTR_BLINK:=$(tput -T$TERM blink)} \
${ATTR_SGR0:=$(tput -T$TERM sgr0)}
ps1="$ps1$esc$ATTR_BLINK" ;;
(-l|--level)
ext="$ext${nl}typeset -i10 -x SHLVL" \
ext="$ext${nl}alias -x ksh='SHLVL=\${SHLVL:-0}+1 ksh'" \
ps1="$ps1\${SHLVL}" ;;
(-m|--dim)
ATTR_DIM=${ATTR_DIM#$esc} \
ATTR_SGR0=${ATTR_SGR0#$esc} \
: ${ATTR_DIM:=$(tput -T$TERM dim)} \
${ATTR_SGR0:=$(tput -T$TERM sgr0)}
ps1="$ps1$esc$ATTR_DIM" ;;
(+m|--undim)
ATTR_SGR0=${ATTR_SGR0#$esc} \
: ${ATTR_SGR0:=$(tput -T$TERM sgr0)}
ps1="$ps1$esc$ATTR_SGR0" ;;
(-n|--hostname)
# Ksh93 does command substitution within PS1;
# ksh88 only implements variable substitution.
ps1="$ps1\${HOSTNAME}" \
: ${HOSTNAME:=$(hostname)} ;;
(-o|--status) # cannot as yet suppress display if $? equals 0
ps1="$ps1[\$?]" ;; #XXX no [, ] !!
(-p|--prompt)
# Warning: not dynamically calculated each display
if [[ $(LANG=C /bin/id) = uid=0\(* ]]
then ps1="$ps1#"
else ps1="$ps1\\$"
fi ;;
(-r|--reverse)
ATTR_REV=${ATTR_REV#$esc} \
ATTR_SGR0=${ATTR_SGR0#$esc} \
: ${ATTR_REV:=$(tput -T$TERM rev)} \
${ATTR_SGR0:=$(tput -T$TERM sgr0)}
ps1="$ps1$esc$ATTR_REV" ;;
(+r|--unreverse)
ATTR_SGR0=${ATTR_SGR0#$esc} \
: ${ATTR_SGR0:=$(tput -T$TERM sgr0)}
ps1="$ps1$esc$ATTR_SGR0" ;;
(-s|--standout)
ATTR_SMSO=${ATTR_SMSO#$esc} \
ATTR_RMSO=${ATTR_RMSO#$esc} \
: ${ATTR_SMSO:=$(tput -T$TERM smso)} \
${ATTR_RMSO:=$(tput -T$TERM rmso)}
ps1="$ps1$esc$ATTR_SMSO" ;;
(+s|--unstandout)
ATTR_RMSO=${ATTR_RMSO#$esc} \
: ${ATTR_RMSO:=$(tput -T$TERM rmso)}
ps1="$ps1$esc$ATTR_RMSO" ;;
(-t|--time)
ext="$ext${nl}typeset -\${xport}Z2 _h _m _s" \
ext="$ext${nl}typeset -i\$xport _t" \
ext="$ext${nl}SECONDS=\$(date '+%H*3600+%M*60+%S')" \
ext="$ext${nl}TILDE=\\~" \
ps1="$ps1\${TILDE[1+((_t=SECONDS)==(_s=_t%60)==" \
ps1="$ps1(_m=_t/60%60)==(_h=_t/3600%24))]}" \
ps1="$ps1\$_h:\$_m:\$_s" ;;
#ps1="$ps1(_m=(_t/60)%60)==(_h=(_t/3600)%24))]}" \
(-u|--user)
ps1="$ps1\${LOGNAME:-\$USER}" ;;
(-w|--window)
#if [[ $1 = --window=* ]]
#then xterm=${1#--window=}
#else xterm=${1#-w}
#[[ -z $xterm ]] && xterm=$2 shift
#fi
case $xterm in
(630?(-*))
wpre="$esc[?\${#PS1};0v" wpost= ;;
(dtterm?(-*)|sun-cmd?(-*))
wpre=$esc]l wpost=$esc ;;
(hpterm?(-*))
wpre=$esc\&f0k\${#PS1}D wpost=' ' ;;
(xterm?(-*))
wpre=$esc]2\; wpost=$bell ;;
esac
${DIAG:+print -ru2 [ $0: xterm: $xterm ]
_inst="$(print -nr -- "$wpre"|od -c|head -1)"
print -ru2 [ preamble: ${_inst#0000000 } ]
_inst="$(print -nr -- "$wpost"|od -c|head -1)"
print -ru2 [ postamble: ${_inst#0000000 } ]}
;;
(-z|--zero) #XXX will not work!
ps1= ;; #XXX ps1="$ps1\$PS1=" ??
(--*) print -ru2 "$0: error: unknown option \"$1\""
return 2 ;;
(-??*) #XXX convert into builtins:
eval "shift
set -- $(print -r -- "${1#-}" |
sed 's/./-& /g; s/ -$//') \"\$@\""
continue ;;
(-?) print -ru2 "$0: error: unknown option \"$1\""
return 2 ;;
(*) ps1="$ps1$1" ;;
esac
shift
done
# just in case: turn off all video attributes
ATTR_SGR0=${ATTR_SGR0#$esc} : ${ATTR_SGR0:-$(tput -T$TERM sgr0)}
# ksh88 bug: even a quoted "[" cannot be matched in patterns!
[[ $ps1 = *$esc* && $ps1 != *$esc$ATTR_SGR0 ]] &&
ps1="$ps1$esc$ATTR_SGR0"
[[ -n $ps1 && $ps1 != *' ' ]] && ps1="$ps1 "
# uncomment line below to explicate constructed PS1 and return:
#print -n "$ps1" | od -c >&2 && return
# uncomment line below to work around broken shells (Solaris 2.5.x):
#eval '$ext'; ${xport:+export TILDE} PS1='$ps1'
trap "eval '$ext'; ${xport:+typeset -x TILDE} PS1='$ps1'" EXIT
return 0
}
#02 EMBEDDED MAN-PAGE FOR "scr2man"
: '
#++
NAME
mkprompt - construct a custom PS1 prompt in ksh
SYNOPSIS
mkprompt option|string ...
mkprompt
DESCRIPTION
Mkprompt constructs a PS1 prompt for use under ksh(1) and other shells
that perform variable substitution on PS1 and implement arrays. There
is no requirement for any other invocation of mkprompt thereafter.
None of the options insert any whitespace, etc, into PS1. To do so,
apply an explicit space character (which has to be protected by quotes)
as a command line parameter. However, one space is appended if PS1 not
already terminated by whitespace.
Note that PS1 is not automatically exported to subshells by mkprompt,
even if the "--level" option is active.
From the SunOS 4.x terminfo(5) man page:
"If your terminal has one or more kinds of display attri-
butes, these can be represented in a number of different
ways. You should choose one display form as standout mode
(see curses(3V)), representing a good, high contrast, easy-
on-the-eyes, format for highlighting error messages and
other attention getters. (If you have a choice, reverse-
video plus half-bright is good, or reverse-video alone; how-
ever, different users have different preferences on dif-
ferent terminals.)"
URL of xterm FAQ. #XXX
OPTIONS
-a|--alert - Sound a bell (if term supports capability.)
-b|--bold - Turn on bold video (if term supports capability.)
-c|--cwd - Show current working directory.
-d|--date - Show date.
-e|--exmark - Show an exclamation mark.
-h|--history - Show history number.
-i|--icon - Set title of icon with prompt string.
-j|--newline - Show a newline.
-k|--blink - Turn on blinking video (if term supports capability.)
-l|--level - Show shell level.
-m|--dim - Turn on dim video (if term supports capability.)
-n|--hostname - Show hostname.
-o|--status - Show exit status of previous command.
-p|--prompt - Show prompt character (dollar sign or hash mark.)
-r|--reverse - Show reverse video (if term supports capability.)
-s|--standout - Turn on reverse video
...
read more »