old-www/HOWTO/Bash-Prompt-HOWTO/x869.html

251 lines
4.1 KiB
HTML

<HTML
><HEAD
><TITLE
>A Prompt the Width of Your Term</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Bash Prompt HOWTO"
HREF="index.html"><LINK
REL="UP"
TITLE="Example Prompts"
HREF="c816.html"><LINK
REL="PREVIOUS"
TITLE="Prompt Depending on Connection Type"
HREF="x865.html"><LINK
REL="NEXT"
TITLE="The Floating Clock Prompt"
HREF="clockt.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Bash Prompt HOWTO: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x865.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 12. Example Prompts</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="clockt.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AEN869"
></A
>12.7. A Prompt the Width of Your Term</H1
><P
>A friend complained that he didn't like having a prompt that kept changing
size because it had $PWD in it, so I wrote this prompt that adjusts its
size to exactly the width of your term, with the working directory on the
top line of two. </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>&#13;#!/bin/bash
# termwide prompt with tty number
# by Giles - created 2 November 98, last tweaked 31 July 2001
#
# This is a variant on "termwide" that incorporates the tty number.
#
hostnam=$(hostname -s)
usernam=$(whoami)
temp="$(tty)"
# Chop off the first five chars of tty (ie /dev/):
cur_tty="${temp:5}"
unset temp
function prompt_command {
# Find the width of the prompt:
TERMWIDTH=${COLUMNS}
# Add all the accessories below ...
local temp="--(${usernam}@${hostnam}:${cur_tty})---(${PWD})--"
let fillsize=${TERMWIDTH}-${#temp}
if [ "$fillsize" -gt "0" ]
then
fill="-------------------------------------------------------------------------------------------------------------------------------------------"
# It's theoretically possible someone could need more
# dashes than above, but very unlikely! HOWTO users,
# the above should be ONE LINE, it may not cut and
# paste properly
fill="${fill:0:${fillsize}}"
newPWD="${PWD}"
fi
if [ "$fillsize" -lt "0" ]
then
fill=""
let cut=3-${fillsize}
newPWD="...${PWD:${cut}}"
fi
}
PROMPT_COMMAND=prompt_command
function twtty {
local WHITE="\[\033[1;37m\]"
local NO_COLOUR="\[\033[0m\]"
local LIGHT_BLUE="\[\033[1;34m\]"
local YELLOW="\[\033[1;33m\]"
case $TERM in
xterm*|rxvt*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR=""
;;
esac
PS1="$TITLEBAR\
$YELLOW-$LIGHT_BLUE-(\
$YELLOW\$usernam$LIGHT_BLUE@$YELLOW\$hostnam$LIGHT_BLUE:$WHITE\$cur_tty\
${LIGHT_BLUE})-${YELLOW}-\${fill}${LIGHT_BLUE}-(\
$YELLOW\${newPWD}\
$LIGHT_BLUE)-$YELLOW-\
\n\
$YELLOW-$LIGHT_BLUE-(\
$YELLOW\$(date +%H%M)$LIGHT_BLUE:$YELLOW\$(date \"+%a,%d %b %y\")\
$LIGHT_BLUE:$WHITE\$$LIGHT_BLUE)-\
$YELLOW-\
$NO_COLOUR "
PS2="$LIGHT_BLUE-$YELLOW-$YELLOW-$NO_COLOUR "
}
&#13;</PRE
></FONT
></TD
></TR
></TABLE
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="images/twtty.png"><DIV
CLASS="CAPTION"
><P
>The twtty prompt in action.&#13;</P
></DIV
></P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x865.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="clockt.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Prompt Depending on Connection Type</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c816.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>The Floating Clock Prompt</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>