Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
01-05-2013, 10:56 PM
#1
Senior Member
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657
Curses / Ncurses and modern-like text-based Linux Club Programming Talks
Hi Guys,
I am opening this thread to give a little "platform" within Linuxquestions to talk about programming to write text-based user interfaces in a terminal-independent manner.
If you are fan of Curses/Ncurses/and like please let us know.
In this thread, you can talk about cool programs that you have discovered, or made, ... questions even, or new information about releases that are in the field of text-based interfaces/programming.
I join you a cool realization of Paul : Worms made in C language.
link
Best Health & Programming Wishes
Last edited by Xeratul; 01-05-2013 at 10:58 PM .
01-08-2013, 01:18 PM
#2
Member
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664
Rep:
In the past, I was using ncurses. If you like ncurses, you could try this program displaying several windows.
Code:
/*
A MOD
Author: Patrick
Coded in VIM
From :
(c) Copyright Paul Griffiths 1999
Inspired by
Email: mail@paulgriffiths.net
Moving windows with ncurses and other stuffs.
*/
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
#include <time.h>
#define KEY_GVALUE 0x47
char * intprtkey(int ch);
int main(void) {
WINDOW * mainwin, * childwin, * awin;
int ch;
int egg;
time_t rawtime;
struct tm * timeinfo;
int input_v,hour,min,sec;
int posx = 1, posy = 1;
/* posx = (cols - width); */
/* posy = (rows - height); */
/* Set the dimensions and initial
position for our child window */
/* terminal height */
/* terminal width */
int width = 23, height = 7;
int rows = 25, cols = 80;
int x = (cols - width) / 2;
int y = (rows - height) / 2;
int maxx_o_2 = (cols - width) / 2;
int maxy_o_2 = (rows - height) / 2;
int eggy;
int awinx1 = 2, awiny1 = 2;
/* Initialize ncurses */
if ( (mainwin = initscr()) == NULL ) {
fprintf(stderr, "Error initializing ncurses.\n");
exit(EXIT_FAILURE);
}
// Ini For Fx keys
noecho();
keypad(mainwin, TRUE);
// Ini of colors
start_color();
/* Switch of echoing and enable keypad (for arrow keys) */
/* this line below cannot be removed ....<--- why? */
wclear(stdscr);
/* box(stdscr,0,0); */
// init_pair(1, COLOR_RED, COLOR_GREEN); box(stdscr,'|','-');
int n = 0;
if ( has_colors() && COLOR_PAIRS >= 13 ) {
// init_pair(pair number, foreground, background);
init_pair(0, COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(7, COLOR_BLUE, COLOR_WHITE);
init_pair(8, COLOR_WHITE, COLOR_RED);
init_pair(9, COLOR_BLACK, COLOR_GREEN);
init_pair(10, COLOR_BLUE, COLOR_YELLOW);
init_pair(11, COLOR_WHITE, COLOR_BLUE);
init_pair(12, COLOR_YELLOW, COLOR_BLUE);
init_pair(13, COLOR_BLACK, COLOR_CYAN);
}
color_set(0, NULL);box(stdscr,'|','-'); color_set(0, NULL);
mvaddstr(1, 10, "Press a key ('q' to quit)...");
mvprintw(2, 10, "You pressed: ");
color_set(0, NULL);
childwin = subwin(mainwin, height, width, y, x);
box(childwin, 0, 0);
mvwaddstr(childwin, 1, 4, "Move the window");
mvwaddstr(childwin, 2, 2, "with the arrow keys");
mvwaddstr(childwin, 3, 6, "or HOME/END");
mvwaddstr(childwin, 5, 3, "Press 'q' to quit");
color_set(0, NULL);
mvprintw(23, 2, "Ncurses DEMO (Keys: u,i,h..l,a..d)");
/* wrefresh does not fix it as according to
*http://cboard.cprogramming.com/c-programming/148685-ncurses-unable-move-windows-mvwin.html */
wrefresh(stdscr);
/* Loop until user hits 'q' to quit */
while ( (ch = getch()) != 'q' ) {
/* mvwin(childwin, y, x);
* this line does not work */
/* thus we add this ugly : */
switch ( ch ) {
case 'j':
if ( y >= 0 )
++y;
break;
case 'k':
if ( y > 0 )
--y;
break;
case KEY_UP:
if ( y > 0 )
--y;
break;
case KEY_DOWN:
if ( y < (rows - height) )
++y;
break;
case 'h':
if ( x > 0 )
--x;
break;
case 'l':
if ( x < (cols - width) )
++x;
break;
case KEY_LEFT:
if ( x > 0 )
--x;
break;
case KEY_RIGHT:
if ( x < (cols - width) )
++x;
break;
case 'g':
x = 0;
y = 0;
break;
case 'f':
x = 0;
y = 0;
break;
case KEY_HOME:
x = 0;
y = 0;
break;
/* shift g */
case KEY_GVALUE:
x = 0;
y = (rows - height);
break;
case KEY_END:
x = (cols - width);
y = (rows - height);
break;
case 's':
++posy;
break;
case 'w':
--posy;
break;
case 'd':
++posx;
break;
case 'u':
--awinx1;
break;
case 'i':
++awinx1;
break;
case KEY_F(1):
egg=1;
eggy=LINES - 3;
break;
case 'a':
--posx;
break;
}
wclear(stdscr);
// frame box
color_set(11, NULL);
box(stdscr,'|','-');
color_set(0, NULL);
// awin box mini
color_set(12, NULL);
awin = subwin(mainwin, 10, 10, awiny1, awinx1);
box(awin, 0, 0);
color_set(0, NULL);
mvaddstr(1, 10, "Press a key ('q' to quit)...");
mvprintw(2, 10, "You pressed: 0x%x (%s)", ch, intprtkey(ch));
time ( &rawtime );
timeinfo = localtime ( &rawtime );
color_set(8, NULL);
mvprintw(23, 2, "Ncurses DEMO (Keys: u,i,h..l,a..d)");
color_set(0, NULL);
mvprintw(3, 10, "The current date/time is: %s", asctime (timeinfo) );
mvprintw(9, 5, "X=%03d Y=%03d", x, y);
mvprintw(10, 5, "maxx/2=%03d maxy/2=%03d", maxx_o_2, maxy_o_2);
mvprintw(11, 5, "width=%03d height=%03d", width, height);
/* https://github.com/macton/pdcurses/blob/master/curses.h */
mvprintw(12, 5, "maxlines=%03d maxcols=%03d", LINES, COLS);
color_set(3, NULL);
mvprintw(23, 62, "Posx=%03d Posy=%03d", posx, posy);
color_set(0, NULL);
mvprintw(posy, posx, "X=%03d Y=%03d", x, y);
color_set(3, NULL);
mvprintw(posy, posx, "Posx=%03d Posy=%03d", posx, posy);
color_set(0, NULL);
if ( egg == 1) {
color_set(8, NULL);
mvprintw(eggy-1, 2, "I am a good student, isn't it?");
color_set(0, NULL);
egg = 0;
}
/* mvwin(childwin, y, x); <---- this does not work. must be replaced by : */
color_set(11, NULL);
childwin = subwin(mainwin, height, width, y, x);
box(childwin, 0, 0);
color_set(0, NULL);
mvwaddstr(childwin, 1, 4, "Move the window");
mvwaddstr(childwin, 2, 2, "with the arrow keys");
mvwaddstr(childwin, 3, 6, "or HOME/END");
mvwaddstr(childwin, 5, 3, "Press 'q' to quit");
refresh();
}
/* Clean up after ourselves */
delwin(childwin);
delwin(mainwin);
endwin();
refresh();
return EXIT_SUCCESS;
}
/* Struct to hold keycode/keyname information */
struct keydesc {
int code;
char name[20];
};
/* Returns a string describing a character passed to it */
char * intprtkey(int ch) {
/* Define a selection of keys we will handle */
static struct keydesc keys[] = { { KEY_UP, "Up arrow" },
{ KEY_DOWN, "Down arrow" },
{ KEY_LEFT, "Left arrow" },
{ KEY_RIGHT, "Right arrow" },
{ KEY_HOME, "Home" },
{ KEY_END, "End" },
{ KEY_BACKSPACE, "Backspace" },
{ KEY_IC, "Insert" },
{ KEY_DC, "Delete" },
{ KEY_NPAGE, "Page down" },
{ KEY_PPAGE, "Page up" },
{ KEY_F(1), "Function key 1" },
{ KEY_F(2), "Function key 2" },
{ KEY_F(3), "Function key 3" },
{ KEY_F(4), "Function key 4" },
{ KEY_F(5), "Function key 5" },
{ KEY_F(6), "Function key 6" },
{ KEY_F(7), "Function key 7" },
{ KEY_F(8), "Function key 8" },
{ KEY_F(9), "Function key 9" },
{ KEY_F(10), "Function key 10" },
{ KEY_F(11), "Function key 11" },
{ KEY_F(12), "Function key 12" },
{ -1, "<unsupported>" }
};
static char keych[2] = {0};
if ( isprint(ch) && !(ch & KEY_CODE_YES)) {
/* If a printable character */
keych[0] = ch;
return keych;
}
else {
/* Non-printable, so loop through our array of structs */
int n = 0;
do {
if ( keys[n].code == ch )
return keys[n].name;
n++;
} while ( keys[n].code != -1 );
return keys[n].name;
}
return NULL; /* We shouldn't get here */
}
All times are GMT -5. The time now is 06:02 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News