This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!

CLI Magic: Use ANSI escape sequences to display a clock in your terminal

Posted by: Anonymous [ip:] on January 23, 2008 04:04 PM
I did this in about 1986 using wyse control codes, I stopped because of a few reasons:

1. I got fed up with scrolling applications such as vi or more corrupting the screen if I scrolled back. Sysline provided a better method that set up the scroll regions and reset the terminal height settings so that fullscreen applications still mostly worked, and reworking it to write to the X titlebar is another way of avoiding scroll problems although if you are running X windows then you have better places to put a clock than the top of your window.
2. Writes may not be atomic, which means other background tasks can write to your terminal whilst the cursor is in an odd place. Saving and restoring cursor positions may also cause issues if another application expects to use that functionality. Terminal control codes can also clash causing wierd effects.
3. More horsepower and screen real estate lets me have another window or taskbar with the time in it.

In conclusion - it looks good if you just sit at a prompt not doing anything, but fiddling with an active console causes nothing but tears. The correct answer is to follow the "screen" suggestion above with 'caption always "%{+b wk}%-21=%D %d.%m.%Y %0c"'. This provides all sorts of advantages such as detatching and reattaching sessions that go way beyond a clock. If you're a sysadmin who has had to reboot their frontend whilst a long-running interactive task is in a window then you will love 'screen'.


Return to CLI Magic: Use ANSI escape sequences to display a clock in your terminal