Announcement

Collapse
No announcement yet.

xterm question display time

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    xterm question display time

    Hi,
    I is is possible to display current system time in the title bar of the xterm terminal or anywhere within a xterm terminal window. I would like to see the current system time displayed in the xterm window.
    Is this possble?

    Thanks

    #2
    How about setting you prompt to show current time?

    Enter this while in xterm:

    export PS1="\u@\h \t:\$ "

    It doesn't update until you hit enter, but could be useful...

    Please Read Me

    Comment


      #3
      Originally posted by oshunluvr View Post
      How about setting you prompt to show current time?

      Enter this while in xterm:

      export PS1="\u@\h \t:\$ "

      It doesn't update until you hit enter, but could be useful...
      Hi,
      Thanks for this. Is it possible change the time to display the duration of the xterm has been active, i.e. in way to show uptime of the xterm terminal?
      How do i make this setting permanent so that it is all ways their when i launch xterm?

      thanks for you help....

      Comment


        #4
        Originally posted by test006
        is it possible to display current system time in the title bar of the xterm terminal?
        ...
        Is it possible change the time to display the duration of the xterm has been active, i.e. in way to show uptime of the xterm terminal?
        How do i make this setting permanent so that it is all ways their when i launch xterm?
        Shells and xterms can do lots of this stuff these days. I'll assume bash and a sufficiently xterm-ish terminal emulator like konsole.

        Some elements you could use:
        • .bashrc in your home directory is where you put stuff you want set up every time you start a bash instance.
        • "strftime" format strings, as shown by man strftime.
        • Bash's printf "builtin" has a %(...)T construct, that hands off to strftime. For example, printf '%(%x %X)T\n' -1 will print the current time in your local format. -2 gives the bash instance start time, which looks useful for your uptime request.
        • The bash variable PROMPT_COMMAND, the contents of which are run just before printing the prompt.
        • Bash shell functions can be used to run several commands that can be formatted nicely, and quoting issues are avoided.
        • As mentioned by oshnluvr, the bash variable PS1 specifies the prompt. It is evaluated, so if the backslash sequences it supports don't fit the bill, you can use $(...) to execute arbitrary commands. I set it to run a bash shell function.
        • The xterm escape sequence ESC ] 0; whatever BEL can be used to set the title bar. With konsole the setting "show window title on the titlebar" has to be checked. For example,
          echo -en '\e]0;whatever\007'
        • A bit hackish, but you can start a sub-process that runs continually in the background, maybe with a sleep, f.ex.
          while :;do sleep 0.9;echo Hi;done &
          On a heavily updating terminal, this could mess up with interleaved output, but you mostly can't read such output anyway.


        You could put these together with these lines in your .bashrc:
        Code:
        tf()
        {
            local start=$(printf '%(%s)T' -2);
            local   now=$(printf '%(%s)T' -1);
            local   dur=$((now - start));
            TZ=Z printf '%(%T)T' $dur
        }
        PROMPT_COMMAND='echo -en "\e]0;$(tf)\007"'
        Hmm, I'll just explain that TZ hack. $dur gets the number of seconds uptime, but us humans want hours, minutes seconds. TZ=Z sets the time zone to "zulu" time, UTC, then we pretend we're printing a time on the day of the unix epoch (1-Jan-1970). Cleaner, and correct if there's more than 24 hours, would be
        Code:
        printf '%02d:%02d:%02d' $((dur/3600)) $((dur%3600/60)) $((dur%60))
        Instead of setting PROMPT_COMMAND, try
        Code:
            while :;do sleep 0.9;echo -en "\e]0;$(tf)\007";done &
        Regards, John Little

        Comment


          #5
          Hey Thanks jlittle, I like your style.

          Comment


            #6
            Thanks this did the trick. This did the trick.
            Originally posted by jlittle View Post
            Shells and xterms can do lots of this stuff these days. I'll assume bash and a sufficiently xterm-ish terminal emulator like konsole.

            Some elements you could use:
            • .bashrc in your home directory is where you put stuff you want set up every time you start a bash instance.
            • "strftime" format strings, as shown by man strftime.
            • Bash's printf "builtin" has a %(...)T construct, that hands off to strftime. For example, printf '%(%x %X)T\n' -1 will print the current time in your local format. -2 gives the bash instance start time, which looks useful for your uptime request.
            • The bash variable PROMPT_COMMAND, the contents of which are run just before printing the prompt.
            • Bash shell functions can be used to run several commands that can be formatted nicely, and quoting issues are avoided.
            • As mentioned by oshnluvr, the bash variable PS1 specifies the prompt. It is evaluated, so if the backslash sequences it supports don't fit the bill, you can use $(...) to execute arbitrary commands. I set it to run a bash shell function.
            • The xterm escape sequence ESC ] 0; whatever BEL can be used to set the title bar. With konsole the setting "show window title on the titlebar" has to be checked. For example,
              echo -en '\e]0;whatever\007'
            • A bit hackish, but you can start a sub-process that runs continually in the background, maybe with a sleep, f.ex.
              while :;do sleep 0.9;echo Hi;done &
              On a heavily updating terminal, this could mess up with interleaved output, but you mostly can't read such output anyway.


            You could put these together with these lines in your .bashrc:
            Code:
            tf()
            {
                local start=$(printf '%(%s)T' -2);
                local   now=$(printf '%(%s)T' -1);
                local   dur=$((now - start));
                TZ=Z printf '%(%T)T' $dur
            }
            PROMPT_COMMAND='echo -en "\e]0;$(tf)\007"'
            Hmm, I'll just explain that TZ hack. $dur gets the number of seconds uptime, but us humans want hours, minutes seconds. TZ=Z sets the time zone to "zulu" time, UTC, then we pretend we're printing a time on the day of the unix epoch (1-Jan-1970). Cleaner, and correct if there's more than 24 hours, would be
            Code:
            printf '%02d:%02d:%02d' $((dur/3600)) $((dur%3600/60)) $((dur%60))
            Instead of setting PROMPT_COMMAND, try
            Code:
                while :;do sleep 0.9;echo -en "\e]0;$(tf)\007";done &

            Comment

            Working...
            X