screen.h

After programming with C and C++ for a few years using Borland C++ (first 2.0, then 5.02), I got used to being able to change text/background colours, clear the screen, and move the cursor to x, y all with a standard (so I thought) library: conio.h. Well, after getting into Linux and playing with C on Linux, I found that those commands weren't implemented at all in a standard library, it was just a nice idea of Borland's to make such things simple to do. I've played with Visual C++ as well (partly at college for assignment work, partly at home, for the sake of learning about different IDEs), and found that Microsoft have also kindly provided a 'simple' way of doing it - the Windows Console API.

What does this mean for those of us who have our Linux boxes for programming? Ask on any Linux programming board or newsgroup, and the answer will almost always be 'use ncurses.' So, I built ncurses on my boxes and typed in a demo program:

#include <ncurses.h>

int main() {	
  initscr();
  printw("Hello.");
  refresh();
  getch();
  endwin();

  return 0; }
			    
What's so bad about that? Well, you have to : What's more, where a 'hello, world' program might, with normal C, come out of the compiler at ~10K, using ncurses it comes out at ~150K. Neither might sound like much in this time when anything under 10M or so is considered spare change, but the difference is still extreme: the ncurses executable is around fifteen times as big as the 'standard' C executable.

I thought to myself 'there's got to be a simpler way of doing this, without ncurses,' because even ls on a modern Linux box will output in colour, without wiping the screen. So I looked around, and looked around, and eventually I discovered the answer: ANSI escape sequences!

But there is a potential problem there, too: typing 'printf("\e[2J\e[0;0m")' into a C program might get a little tedious, especially when it's not the only escape sequence I'm using. What do I do?

"screen.h"

I write a library. Now I can write a program in C on my Linux box, and I can move the cursor around at will, or change the colour of the text and background, or even clear the screen, without making the executables fifteen times as big as they should be. All I have to do is include a (currently) ~5K header into my source.

And I've decided to make it available here, for anyone who wants a quick way to do things for which most people would tell you 'you need to use ncurses.' I'm releasing it under the General Public License. If you try it and like it, please let me know. If you try it and have a suggestion or three, please let me know. If you think the idea sucks, let me know. But if you just want to rail at me, I have a different email address, specially for that purpose: /dev/null.

Obviously, the library can't compete with ncurses: I'd imagine that it's got loads of really useful stuff in it that justifies the massive executables. This is as much a learning experience for me as a practical tool, and, even though it's little more than a 'toy' header, I hope it's useful to whoever tries it out.

To install screen.h properly, download the tarball below and extract it, then run the install shell-script in the screen.h directory as root. When you've installed it, you can include it in your programs as you would any other library. There is also a manual provided, which is copied to the appropriate place when you run the install script, and a copy of the same manual provided as a pdf (I've also made it available separately).

By downloading the files on this page, you are agreeing to and accepting the terms of the GPL.

Happy Hacking.

Version Link What it is
5 screen.h.tar.gz The screen.h files
5 screen.h.pdf The manual, including a tutorial


This website is (C) Copyright Mark Tuson 2010.
To send me an email, click here.