Volume 3 Issue 5, May 2004 Curses,
Part 1 |
Copyright (C) 2004 by Steve Litt. All rights reserved. Materials from guest authors copyrighted by them and licensed for perpetual use to Linux Productivity Magazine. All rights reserved to the copyright holder, except for items specifically marked otherwise (certain free software source code, GNU/GPL, etc.). All material herein provided "As-Is". User assumes all risk and responsibility for any outcome.
|
and Rapid Learning: Secret Weapon of the Successful Technologist by Steve Litt |
[ Troubleshooters.Com
| Back Issues |Troubleshooting Professional Magazine
]
Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian Kernighan |
CONTENTS
News Mag |
Submission URL or
Email
address |
Comments |
Slashdot |
http://slashdot.org/submit.pl |
Just fill in the short form. |
LinuxToday |
http://linuxtoday.com/contribute.php3 |
Just fill in the short form. |
Linux Weekly News |
lwn@lwn.net |
Just tell them the URL, why you like it. |
NewsForge |
webmaster@linux.com |
Just tell them the URL, why you like it. |
VarLinux |
http://www.varlinux.org/vlox/html/modules/news/submit.php |
Just fill in the short form. |
LinuxInsider.Com, Newsfactor Network |
http://www.newsfactor.com/perl/contact_form.pl?to=contact |
Just tell them the URL, why you like it. |
The Linux Knowledge Portal |
webmaster@linux-knowledge-portal.org |
Just tell them the URL, why you like it. |
OS News |
http://www.osnews.com/submit.php |
Just tell them the URL, why you like it. |
DesktopLinux |
http://www.desktoplinux.com/cgi-bin/news_post.cgi |
Only for LPM issues involving the Linux desktop,
not
for programming or server issues. |
"GNU/Linux" is probably the most accurate moniker one can give to this operating system. Please be aware that in all of Troubleshooters.Com, when I say "Linux" I really mean "GNU/Linux". I completely believe that without the GNU project, without the GNU Manifesto and the GNU/GPL license it spawned, the operating system the press calls "Linux" never would have happened.
I'm part of the press and there are times when it's easier to say "Linux" than explain to certain audiences that "GNU/Linux" is the same as what the press calls "Linux". So I abbreviate. Additionally, I abbreviate in the same way one might abbreviate the name of a multi-partner law firm. But make no mistake about it. In any article in Troubleshooting Professional Magazine, in the whole of Troubleshooters.Com, and even in the technical books I write, when I say "Linux", I mean "GNU/Linux".
There are those who think FSF is making too big a deal of this.
Nothing
could be farther from the truth. The GNU General Public License,
combined
with Richard Stallman's GNU Manifesto and the resulting GNU-GPL
License,
are the only reason we can enjoy this wonderful alternative to
proprietary
operating systems, and the only reason proprietary operating systems
aren't
even more flaky than they are now.
For practical purposes, the license requirements of "free software" and
"open
source" are almost identical. Generally speaking, a license that
complies
with one complies with the other. The difference between these two is a
difference
in philosophy. The "free software" crowd believes the most important
aspect
is freedom. The "open source" crowd believes the most important aspect
is
the practical marketplace advantage that freedom produces.
I think they're both right. I wouldn't use the software without the
freedom
guaranteeing me the right to improve the software, and the guarantee
that
my improvements will not later be withheld from me. Freedom is
essential.
And so are the practical benefits. Because tens of thousands of
programmers
feel the way I do, huge amounts of free software/open source is
available,
and its quality exceeds that of most proprietary software.
In summary, I use the terms "Linux" and "GNU/Linux" interchangably,
with
the former being an abbreviation for the latter. I usually use the
terms
"free software" and "open source" interchangably, as from a licensing
perspective
they're very similar. Occasionally I'll prefer one or the other
depending
if I'm writing about freedom, or business advantage.
#include <ncurses.h> |
gcc -lncurses test.c |
#include <ncurses.h> |
#include <ncurses.h> |
#include <ncurses.h> |
move(y, x); |
mvaddch(12, 10, 'L'); |
attron(WA_REVERSE); /* temporarily make reverse video */What if you want to change the color and attributes of a portion of the screen, without writing anything? Meet the chgat() function:
/* print reverse video stuff here */
attroff(WA_REVERSE); /* restore regular video */
#include <ncurses.h> |
stdscr
function |
General
window function |
Purpose |
addch(c) | waddch(windowPointer, c) | Place character at current cursor |
addstr(string) | waddstr(windowPointer, string) | Place string at current cursor |
printw(format, ...) | wprintw(windowPointer, format, ...) | Place formatted string at
current cursor |
move(y, x) | wmove(windowPointer, y, x) | Move cursor to y, x |
mvaddch(y, x, c) | mvwaddch(windowPointer, y, x, c) | Place character at y, x |
mvaddstr(y, x, string) | mvwaddstr(windowPointer, y, x, string) | Place string at y, x |
mvprintw(y, x, format, ...) | mvwprintw(windowPointer, y, x, format, ...) | Place formatted string at y, x |
attrset(colorpair) |
wattrset(windowPointer,
colorpair) |
Replaces all current attributes
with colorpair |
attron(colorpair) | wattron(windowPointer, colorpair) | Or's colorpair with current
attributes |
attroff(colorpair) | wattroff(windowPointer, colorpair) | Turn off colorpair attribute,
leave others alone |
chgat(length, attr, colorindex) mvchgat(y, x, length, attr, colorindex) |
chwgat(window, length, attr, colorindex) |
Change attribute and color of
part of a line without overwriting the existing text. |
getch() | wgetch(windowPointer) | Acquire keystroke |
getstr(buffer) | wgetstr(windowPointer, buffer) | Acquire line from user |
getnstr(buffer) | wgetnstr(windowPointer, buffer) | Acquire line, limited to n chars |
scanw(format, ...) |
wscanw(format, ...) |
Acquire formatted input |
mvgetch(y, x) | mvwgetch(windowPointer, y, x) | Move to y, x, then acquire char |
mvgetstr(y, x, buffer) | mvwgetstr(windowPointer, y, x, buffer) | Move to y, n, then acquire string |
mvgetnstr(y, x, buffer) | mvwgetnstr(windowPointer, y, x, buffer) | Move to y, n, then acquire
limited string |
mvwscanw(y, x, format, ...) |
mvwscanw(windowPointer, y, x,
format, ...) |
Move to y, n, then acquire
formatted input |
#include <ncurses.h> |
#include <ncurses.h>
|
#include <ncurses.h> |
![]() |
The base
window |
![]() |
The small
window appears |
![]() |
The base
window is moved to the front, so the small window is buried. |
![]() |
The small
window is moved back to the front. |
![]() |
The small
window has been destroyed. |
{ Foreground and background color constants } |
#include <ncurses.h> |
![]() |
The base
window |
![]() |
The small
window appears |
![]() |
The base
window is moved to the front, so the small window is buried. |
![]() |
The small
window is moved back to the front. |
![]() |
The small
window has been destroyed. |
|
typedef struct |
MATCHSTRUCT *getMatch(MENU *pmenu, char c) |
int currentChoiceNo = item_index(current_item(pmenu));Once the three totals are filled, the MATCHSTRUCT pointer is returned so that the calling function has access to the totals for use in deducing whether to directly execute the choice, or deducing where to move the highlight.
int runMenu(char *choices[]) |
#include <curses.h> |
Example
graphic |
Legend |
|
![]() |
|
Blue=Base Window |
Dark red=Border
window |
||
Light
red=User Interface window |
Some Menu Window Terminology |
|
Term |
Meaning |
Border Window |
This is a window whose only duty
is to hold unchanging text and line graphics necessary to make the menu
look good and communicate information like title and prompts to the
user. As a programmer, you size this window large enough to hold the User Interface Window, the outside line box, the title, and any prompts needed to guide the user. It obviously cannot be larger than the main screen (stdscr), whose size can be queried with getmaxyx(windowpointer,maxy,maxx). Moreover, to give the user context as to where he or she is in the app, this window should be smaller than the main screen. This window is created with the newwin() function, making it totally independent of the main window. This is important, because this independence means that once the Border Window is destroyed, the text which it and its derived windows covered becomes visible again. The following code creates a pointer to a Border Window called wBorder. This window is 22 lines high, 40 columns wide, and is positioned roughly in the center of an 80x25 screen. This would be appropriate for a menu of 10 items and several prompts, or a menu of 20 or more items (or a picklist) without prompts. height=22; width=40; y=2; x=20; wBorder = newwin(height, width, y, x); The Border Window is associated with its menu via the set_menu_sub() function. set_menu_win(MENU *, WINDOW *) |
User Interface Window | This is a window to contain the
lines of the menu. These lines appear at the upper left of this window.
This window is associated with the MENU * structure using: set_menu_sub(MENU *, WINDOW *) This window is created with size and position relative to the Border Window, so that the border and title properly surround the menu's lines. It is created with the derwin() function, because it is a window derived from the Border Window. The following code positions the User Interface Window such that it is 2 characters smaller than the Border Window in both directions, and it places the User Interface Window such that it is centered inside the Border Window: wUI = derwin(wBorder, height-2, width-2, 2, 2); |
Menu Window |
If you've read Pradeep Padala's
outstanding NCURSES Programming HOWTO,
you've seen this term. It's his term for what I call a Border Window. I
imagine he named it this because this window is associated with the
menu with the set_menu_win()
function. Personally, I think it's clearer to think of this window as a Border Window. |
Menu Subwindow |
Pradeep Padala's NCURSES Programming HOWTO,
uses this term to describe what I call a User Interface Window. I
imagine he named it this because this window is associated with the
menu with the set_menu_sub()
function. Personally, I think it's clearer to think of this window as a User Interface Window. |
#include <curses.h> /* Necessary for all Curses programs */ |
Any article submitted to Linux Productivity Magazine must be licensed with the Open Publication License, which you can view at http://opencontent.org/openpub/. At your option you may elect the option to prohibit substantive modifications. However, in order to publish your article in Linux Productivity Magazine, you must decline the option to prohibit commercial use, because Linux Productivity Magazine is a commercial publication.
Obviously, you must be the copyright holder and must be legally able to so license the article. We do not currently pay for articles.
Troubleshooters.Com reserves the right to edit any submission for clarity or brevity, within the scope of the Open Publication License. If you elect to prohibit substantive modifications, we may elect to place editors notes outside of your material, or reject the submission, or send it back for modification. Any published article will include a two sentence description of the author, a hypertext link to his or her email, and a phone number if desired. Upon request, we will include a hypertext link, at the end of the magazine issue, to the author's website, providing that website meets the Troubleshooters.Com criteria for links and that the author's website first links to Troubleshooters.Com. Authors: please understand we can't place hyperlinks inside articles. If we did, only the first article would be read, and we can't place every article first.
Submissions should be emailed to Steve Litt's email address, with subject line Article Submission. The first paragraph of your message should read as follows (unless other arrangements are previously made in writing):
Copyright (c) 2003 by <your name>. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, version Draft v1.0, 8 June 1999 (Available at http://www.troubleshooters.com/openpub04.txt/ (wordwrapped for readability at http://www.troubleshooters.com/openpub04_wrapped.txt). The latest version is presently available at http://www.opencontent.org/openpub/).
Open Publication License Option A [ is | is not] elected, so this document [may | may not] be modified. Option B is not elected, so this material may be published for commercial purposes.
After that paragraph, write the title, text of the article, and a two sentence description of the author.