Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Drawing in ncurses

I've been trying to do simple line drawing (e.g. boxes) in ncurses all day, but I can't get it to work. I'm trying to print extended ASCII characters like the ones found here: http://www.theasciicode.com.ar/ascii-table-codes/ascii-codes-219.html I've seen a few mentions to wchar_t, but it apparently requires ncursesw, which I can't figure out how to include (I know it's installed)

I'm using XCode under OS X 10.6.2 and GCC 4.2.

Any ideas?

like image 513
denizen Avatar asked Dec 10 '10 00:12

denizen


2 Answers

You don't need wchar_t. The "extended" codes (c. 1985) are less than 255. For example, to draw the left lower corner of a double-lined box, use code 200 decimal, 310 octal ("\310") or 0xc8 ("\xc8").

Those characters need support from the terminal emulator you are using, but it should work fine.


edit
I have a vague memory of a 7-bit vs. 8-bit mode for old curses, but I cannot find any mention of it in the FSF ncurses 1.190 (2008/12/20), also identified as v5.7.3.20090207 which I have on Linux. The man page for curs_addch mentions symbolic constants for line drawing characters, so perhaps you are expected to use those instead of literal line drawing characters:

addch (ACS_ULCORNER);   // upper left corner
for (int j = 0;  j < boxwidth-2;  ++j)
    addch (ACS_HLINE);
addch (ACS_URCORNER);   // upper right
...
like image 152
wallyk Avatar answered Nov 14 '22 03:11

wallyk


void boxAround( int y, int x, int h, int w ) {
    move( y, x );
    addch (ACS_ULCORNER);   // upper left corner
    int j;
    for (j = 0;  j < w;  ++j)
        addch (ACS_HLINE);
    addch (ACS_URCORNER);   // upper right

    for( j = 0; j < h; ++j ) {
            move(  y+1+j, x );
            addch (ACS_VLINE);
            move( y+1+j, x+w+1 );
            addch (ACS_VLINE);
    }

    move( y+h+1,x );
    addch (ACS_LLCORNER);   // lower left corner

    for (j = 0;  j < w;  ++j)
        addch (ACS_HLINE);
    addch (ACS_LRCORNER);   // lower right
}
like image 1
Grwon Avatar answered Nov 14 '22 03:11

Grwon