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

228 lines
3.8 KiB
HTML

<HTML
><HEAD
><TITLE
>The Floating Clock Prompt</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="A Prompt the Width of Your Term"
HREF="x869.html"><LINK
REL="NEXT"
TITLE="The Elegant Useless Clock Prompt"
HREF="the-elegant-useless-clock-prompt.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="x869.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="the-elegant-useless-clock-prompt.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="CLOCKT"
></A
>12.8. The Floating Clock Prompt</H1
><P
>I've rewritten this prompt several times. It was originally written using
octal escape sequences, but the ones I needed most for this (save and
restore cursor position) aren't honoured by one of the commonest terminal
emulators, rxvt. I rewrote it using <B
CLASS="COMMAND"
>tput</B
>, and that's
what you see here. The required <B
CLASS="COMMAND"
>tput</B
> codes seem to be
universally honoured. The body of the prompt is essentially the same as
the "Lightweight" prompt shown earlier, but a clock is kept floating in the
upper right corner of the term. It will reposition itself correctly even
if the term is resized.&#13;</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>#!/bin/bash
# Rewrite of "clock" using tput
function prompt_command {
# prompt_x is where to position the cursor to write the clock
let prompt_x=$(tput cols)-6
# Move up one; not sure why we need to do this, but without this, I always
# got an extra blank line between prompts
tput cuu1
tput sc
tput cup 0 ${prompt_x}
tput setaf 4 ; tput bold
echo -n "["
tput setaf 1
echo -n "$(date +%H%M)"
tput setaf 4 ; tput bold
echo -n "]"
tput rc
}
PROMPT_COMMAND=prompt_command
function clockt {
local BLUE="\[$(tput setaf 4 ; tput bold)\]"
local LIGHT_RED="\[$(tput setaf 1 ; tput bold)\]"
local WHITE="\[$(tput setaf 7 ; tput bold)\]"
local NO_COLOUR="\[$(tput sgr0)\]"
case $TERM in
xterm*|rxvt*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR=""
;;
esac
PS1="${TITLEBAR}\
$BLUE[$LIGHT_RED\u@\h:\w$BLUE]\
$WHITE\$$NO_COLOUR "
PS2='&#62; '
PS4='+ '
}&#13;</PRE
></FONT
></TD
></TR
></TABLE
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="images/clockt.png"><DIV
CLASS="CAPTION"
><P
>The floating clock prompt in action. The clock will stay in correct
position even if the term is resized.&#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="x869.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="the-elegant-useless-clock-prompt.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>A Prompt the Width of Your Term</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 Elegant Useless Clock Prompt</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>