In comp.os.linux.development.apps, James T. Dennis
wrote
on 4 Oct 2000 08:40:46 GMT
>> hi. i have been able to write a simple program that displays the
>> current time, formatted to how i want, using 'strftime' from the
>> console. i've played around with it, and no problems.
>> however, i am now wanting to get a similar result in the console, but
>> have the displayed time constantly updating. "real-time", so to speak,
>> instead of just displaying the time of the program's execution.
>> is there anyway to get this done, so i can have the time displayed
>> constantly in a console? if not console, could someone point me in the
>> right direction to doing this in a GUI, X Windows System interface?
>> thanks all.
> Curses. To do this you want to get the appropriate escape
> sequences to control your terminal's cursor. (Your Linux console is
> controlled primarily via a terminal compatible abstraction; it
> is treated by the system as a set of independent "virtual terminals").
> At a minimum you need to know how to perform a destructive backspace
> so that you can do something like:
> Wed 4 Oct 2000 01:11:49 << print time
> Wed 4 Oct 2000 01:11: << two backspaces
> Wed 4 Oct 2000 01:11:50 << print new digits
> Wed 4 Oct 2000 01:11:5 << one backspace
> Wed 4 Oct 2000 01:11:51 << replace last digit
> ... repeat that last for next 9 seconds,
> then backspace 4 characters and print 01:12:00
> etc.
As I understand (n)curses, it will do a "minimal update", which means
that one can simply do a call to ctime(), use printw() or mvprintw(),
refresh(), sleep for a second, then repeat. Curses will only update
those characters that actually changed -- an issue that may seem a bit
anachronistic now, but was presumably a Godsend in the days of
300-baud modems.
One problem with this is that curses will clear the screen upon
initialization, so all one will see is an updating clock
(unless one implements additional stuff, such as a marquee, ad
banner, scrollable text region for reading a book, input form
system, etc.) There are aso some issues regarding keyboard
responsiveness; I'd frankly have to experiment to see what I can
do regarding getch(), if one can get stdscr to return a file
descriptor (or just be crufty and use standard input -- 0 --
in a select() call, which should work fine unless one uses newterm()
instead of initscr()).
'man ncurses' for a starting point, at least on RedHat systems.
It's an interesting, if primitive, environment. :-)
'man select' for details on the select call, which can wait for
a number of sockets and pipes to retrieve data, along with a
timeout specifiable in milliseconds -- perfect for a clock which
needs keyboard input. :-)
Quote:> That's the basics. In a shell you could look some for these
> using the tput command. For example:
> #!/bin/sh
> bs=$(tput cub1) # Read the tput(1) and terminfo(4) man pages
> x=0
> while [ "$x" -lt 10 ] ; do
> echo -n $x; sleep 1; echo -n "$bs"
> done
> Should work (as a simple example).
Yes, that should work reasonably well.
--