Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

darxun

macrumors member
Original poster
Mar 18, 2008
40
0
I am programming C with xCode. I tried these:
Code:
#include <curses.h>
system("clear");
But they are not working, system("clear") just puts a lot of space between the lines in terminal. And in the DebuggerConsole it says "TERM environment variable not set."
So what can I do?
 

Cromulent

macrumors 604
Oct 2, 2006
6,812
1,100
The Land of Hope and Glory
I am programming C with xCode. I tried these:
Code:
#include <curses.h>
system("clear");
But they are not working, system("clear") just puts a lot of space between the lines in terminal. And in the DebuggerConsole it says "TERM environment variable not set."
So what can I do?

Learn the curses API would be a better idea. The system() function calls programs external to yours. You want to use the curses clear function.

Plus don't forget to include the curses library in your Xcode project.

This is an example curses program:

Code:
#include <ncurses.h>

int main()
{	int ch;

	initscr();			/* Start curses mode 		*/
	raw();				/* Line buffering disabled	*/
	keypad(stdscr, TRUE);		/* We get F1, F2 etc..		*/
	noecho();			/* Don't echo() while we do getch */

    	printw("Type any character to see it in bold\n");
	ch = getch();			/* If raw() hadn't been called
					 * we have to press enter before it
					 * gets to the program 		*/
	if(ch == KEY_F(1))		/* Without keypad enabled this will */
		printw("F1 Key pressed");/*  not get to us either	*/
					/* Without noecho() some ugly escape
					 * charachters might have been printed
					 * on screen			*/
	else
	{	printw("The pressed key is ");
		attron(A_BOLD);
		printw("%c", ch);
		attroff(A_BOLD);
	}
	refresh();			/* Print it on to the real screen */
    	getch();			/* Wait for user input */
	endwin();			/* End curses mode		  */

	return 0;
}

Here is a decent guide:

http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html
 

PyroTurtle

macrumors regular
Jul 27, 2001
240
0
10 Minutes from Disneyland
As a side note, in BSD (esp Darwin) that is all that clear does. It just makes it so that you don't see any of the output text by (basically) returning an entire screens worth of new lines basically. Annoying, ain't it?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
it sounds like you want to make a commandline app but need more than a very rudimentary UI. If you want your app to be portable, using an established library instead of cobbling together some control characters that only work on OS X and maybe a few BSDs is a better idea.

My guess is it will get messy fast if you're getting terminal geometry and trying to directly control everything.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Double post, sorry...

The example posted does more than just clear the screen. The initscr() method is all that it takes. Perhaps this might serve as a word of warning, however... this is what ncurses printed to initalize the screen:
Code:
"\^[)0\^[7\^[[?47h\^[[1;56r\^[[m\^[[4l\^[[H\^[[2J"

and this to clear it subsequently:
Code:
"\^[[H\^[[2J"

and wrote this to restore it:
Code:
"\^[[56;1H\^[[2J\^[[?47l\^[8\r\^[[?1l\^[>"

I have no idea what any of these control characters are... but ncurses handles all of this, per platform, for you.

To try to simplify it, I wrote this:
Code:
#include <ncurses.h>

int main(int argc, char *argv[]) {
  initscr();
  getch();
  printw("Muss up the screen a bit");
  getch();
  clear();
  getch();
  endwin();
  return 0;
}

The initscr() is all it takes to clear the screen. endwin() is all it takes to restore it to its previous state. The getch() is in there so it will stay with a blank screen until you press a key. I also put something on the screen and called clear again, to demonstrate that you can clear whenever you want once ncurses is up, not just at startup.

I should state that there is definitely a way to do this by hand... but this makes it better. I have a pretty good feel for how this works, and I'd never looked at ncurses at all until 20 minutes ago.

-Lee

P.S. To compile this, which i called testncurses.c, i ran:
Code:
gcc -o testncurses -lncurses testncurses.c

the -lncurses would be the only thing I wold expect to be unusual. That just adds in the ncurses library for the linker. I'm pretty sure this can be added easily to XCode to handle this for you, but I won't speak to things that I don't know for sure.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.