Unix Programming - I want to clear a portion of a screen - How??

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > September 2006 > I want to clear a portion of a screen - How??





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author I want to clear a portion of a screen - How??
Rajendran

2006-09-02, 7:39 am

I just want to clear the above three lines from the current position,
so that I can start printing my results from that point. In short I
want to clear a portion of my screen.

Note: I work in the UNIX environment using gcc compiler. And I'm not a
super user.

I want a "simple solution".

I tried the combination of "\r\b" escape sequences. But \b is not
taking me back to the above line?
I urgently need the key for this.
Please help me achieving this.

I asked this question in comp.lang.c group. I got a reply to ask this
question here!
I daily visit my topic to see whether I got the answer or not!
Thank you.

comp.lang.c

2006-09-02, 7:39 am

Rajendran wrote:

> I just want to clear the above three lines from the current position,
> so that I can start printing my results from that point. In short I
> want to clear a portion of my screen.
>
> Note: I work in the UNIX environment using gcc compiler. And I'm not a
> super user.
>
> I want a "simple solution".
>
> I tried the combination of "\r\b" escape sequences. But \b is not
> taking me back to the above line?
> I urgently need the key for this.
> Please help me achieving this.
>
> I asked this question in comp.lang.c group. I got a reply to ask this
> question here!


Indeed here is the right place to ask this question.

The cleanest and fairly simple solution is to
use the curses (ncurses on Linux) library. You
can read about it at
http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/

You might also try "console codes" but they
may be less portable. Have a look at
http://homepages.cwi.nl/~aeb/linux/...le_codes.4.html

Renato Golin

2006-09-05, 7:26 am

Rajendran wrote:
> I just want to clear the above three lines from the current position,
> so that I can start printing my results from that point. In short I
> want to clear a portion of my screen.
>
> Note: I work in the UNIX environment using gcc compiler. And I'm not a
> super user.
>
> I want a "simple solution".
>
> I tried the combination of "\r\b" escape sequences. But \b is not
> taking me back to the above line?
> I urgently need the key for this.
> Please help me achieving this.


Are you just trying to give some ENTERs to continue writing to screen or
you really wanted to erase the content of the three lines above ?

If a few ENTERs would do you might try "\n" instead of "\r\b". \r means
"return" (to the initial position on the same line), \b means
"backspace" (erase char before cursor, even if it's a line-break) and \n
means line-break.

On DOS you need the \r\n because \n only go to the next line and does
not return the cursor to position zero. On Unix it's implicit, so just a
\n would do the job.

cheers,
--renato
Stephan Schulz

2006-09-20, 1:29 pm

In article <44FD523F.5080307@gmail.com>, Renato Golin wrote:
>Rajendran wrote:

As others have mentioned, ncurses is the way to go for any complex
screen handling.
[vbcol=seagreen]
>
>Are you just trying to give some ENTERs to continue writing to screen or
>you really wanted to erase the content of the three lines above ?
>
>If a few ENTERs would do you might try "\n" instead of "\r\b". \r means
>"return" (to the initial position on the same line), \b means
>"backspace" (erase char before cursor, even if it's a line-break) and \n
>means line-break.
>
>On DOS you need the \r\n because \n only go to the next line and does
>not return the cursor to position zero. On Unix it's implicit, so just a
>\n would do the job.


Assuming you use C, any conformant implementation will translate "\n"
to whatever linebreak convention is used at the system level. There
is almost never a reason to use \r in a C program.

Bye,

Stephan

--
-------------------------- It can be done! ---------------------------------
Please email me as schulz@eprover.org (Stephan Schulz)
----------------------------------------------------------------------------
Keith Thompson

2006-09-20, 7:52 pm

schulz@sunbroy2.informatik.tu-muenchen.de (Stephan Schulz) writes:
> In article <44FD523F.5080307@gmail.com>, Renato Golin wrote:
>
> As others have mentioned, ncurses is the way to go for any complex
> screen handling.

[...]

Yes, but clearing three lines isn't necessarily what I'd call complex.

The problem with ncurses is that it takes control of the entire screen
-- and, where possible, it preserves the prevous contents of the
screen and restores them when it's finished. That's great for a
full-screen text editor, but it's not necessarily a good thing if you
want to erase some information.

For example, if I happen to accidentally display sensitive information
on my screen (say, a password), I'd like to erase just a few lines
without affecting the entire screen. I have a PERL script called
"erase" that I use for this. It assumes a VT100-compatible terminal
or emulator, which happens to be good enough for my purposes, but it's
not a universal solution.

Here's the script (it would be easy to implement in another language
such as C or /bin/sh). If you don't know Perl, "\r" is CR (ASCII 13)
and \e is escape (ASCII 27).

========================================
================================
#!/usr/bin/perl -w

sub Usage(@);

my $lines;

if (scalar @ARGV == 0) {
$lines = 10;
}
elsif (scalar @ARGV == 1) {
$lines = $ARGV[0];
Usage if $lines !~ /^\d+$/;
}
else {
Usage;
}

$| = 1;
print "\r\e[${lines}A\e[J";

sub Usage(@) {
print @_ if @_;
print "Usage: $0 n\n";
print "Clears n lines (default 10)\n";
exit 1;
} # Usage
========================================
================================

For a more general solution that would work on non-VT100-compatible
terminals, you'd want to use termcap or terminfo.

tcsh has a builtin command "echotc" that prints specified termcap
sequences.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Michael Wojcik

2006-09-22, 7:28 pm


In article <lnirji5hrr.fsf@nuthaus.mib.org>, Keith Thompson <kst-u@mib.org> writes:
>
> For a more general solution that would work on non-VT100-compatible
> terminals, you'd want to use termcap or terminfo.
>
> tcsh has a builtin command "echotc" that prints specified termcap
> sequences.


The tput command writes terminal control sequences to stdout and
is standardized (by SUSv3), though only three commands (clear,
init, and reset) are standard. But typical implementations support
writing many or all of the terminfo (or in some cases termcap)
sequences.

For example, to erase the first two lines of the screen, something
like:

tput sc; tput home; tput el; tput cud1; tput el; tput rc

would probably work for a fair number of implementation / terminal
type combinations. ("man terminfo" should have the capability
names.)

--
Michael Wojcik michael.wojcik@microfocus.com

Poe said that poetry was exact.
But pleasures are mechanical
and know beforehand what they want
and know exactly what they want. -- Elizabeth Bishop
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com