From - Mon Sep 16 07:18:36 2002 Path: news.net.uni-c.dk!arclight.uoregon.edu!newsfeed.mathworks.com!pln-e!spln!dex!extra.newsguy.com!newsp.newsguy.com!enews3 From: Floyd Davidson Newsgroups: comp.os.linux.development.apps Subject: Re: moving the cursor Date: 05 Sep 2002 13:16:34 -0800 Organization: __________ Lines: 119 Sender: floyd@barrow.com Message-ID: <874rd4i1cd.fld@barrow.com> References: NNTP-Posting-Host: p-706.newsdawg.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii User-Agent: gnus 5.8.3/XEmacs 21.1.9/Linux Xref: news.net.uni-c.dk comp.os.linux.development.apps:135063 "Mark Swaanenburg" wrote: >Hi, > >is it possible without the use of ncurses to move the cursor around a >little, like one line up or one character back or something. > >Tnx in advancem > Mark Do *not* embed terminal escape sequences directly into your code. That is a very easy solution, but is very poor style. The use of ncurse, slang or any other such library of terminal-independant screen manipulation functions is certainly a valid solution and should be considered. If you have never worked with ncurses... it must be time to learn! For only a few simple screen attributes, rather than invoke the entire ncurses package, it is generally easier to whip up a few functions that directly access the TERMINFO database. Below is a demo program which demonstrates part of how this is done. It lacks a number of refinements that really should be included in a real program, but adding them would make it a bit too large to post. (If you ask, I'll be happy to email a much larger demo that provides an extensive example.) For example, this demo does not verify that any of the TERMINFO capabilities are actually defined in the database before attempting to use them. If a capability is not defined, the results are necessarily strange! It also does not demonstrate exactly the terminal attributes you are looking for... /* Demonstrate direct use of TERMINFO database */ /* compile command: cc -O2 -W -Wall foo.c -lncurses -o foo */ #include #include #include void init_terminal(void); /* fetch TERMINFO data */ /* functions which manipulate the screen */ void clrscrn(void); /* clear screen and home cursor */ void cursor_move(int, int); /* cursor positioning */ void sc(void); /* save current cursor position */ void rc(void); /* restore saved cursor position */ int main(void) { init_terminal(); clrscrn(); cursor_move(10, 5); /* column 10, row 5 */ printf("This line starts at "); sc(); /* save this location */ cursor_move(10, 10); /* column 10, row 10 */ printf("This line starts at row 10, column 10."); rc(); /* restore cursor location */ printf("row 5, column 10."); cursor_move(1, 20); /* put cursor lower on screen */ return EXIT_SUCCESS; } /* home the cursor and clear to end of screen */ void clrscrn(void) { tputs(clear_screen, 1, putchar); } /* restore the cursor location */ void rc(void) { tputs(restore_cursor, 1, putchar); } /* save the cursor location */ void sc(void) { tputs(save_cursor, 1, putchar); } /* cursor positioning */ /* home is 1,1 */ void cursor_move(int col, int row) { /* reject out of hand anything negative */ if (--row < 0 || --col < 0) { return; } /* move the cursor */ tputs(tparm(cursor_address, row, col), 1, putchar); } /* set up the terminal */ void init_terminal(void) { char *term = getenv("TERM"); int err_ret; if ( !term || ! *term) { fprintf(stderr,"ERROR: The TERM environment variable is not set.\n"); exit(EXIT_FAILURE); } /* Note: bsd would use err_ret = setterm(term); */ setupterm(term, 1, &err_ret); switch (err_ret) { case -1: fprintf(stderr,"ERROR: Can't access terminfo database.\n"); exit(EXIT_FAILURE); break; case 0: fprintf(stderr, "ERROR: Can't find entry for terminal %s\n", term); exit(EXIT_FAILURE); break; case 1: break; default: fprintf(stderr, "ERROR: Unknown setupterm() return code %d\n", err_ret); exit(EXIT_FAILURE); } } -- Floyd L. Davidson Ukpeagvik (Barrow, Alaska) floyd@barrow.com