LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-05-2013, 10:56 PM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
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.
 
Old 01-08-2013, 01:18 PM   #2
patrick295767
Member
 
Registered: Feb 2006
Distribution: FreeBSD, Linux, Slackware, LFS, Gparted
Posts: 664

Rep: Reputation: 138Reputation: 138
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  */


}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a curses OR ncurses front end for id3v2 OR eyeD3 OR similar CLI id3 editor? SharpyWarpy Linux - Software 10 04-16-2010 05:57 PM
how to print text in color by ncurses without opening a ncurses window Greatrebel Programming 0 12-20-2006 09:15 AM
Tutorial neede for VIsual Programming on Linux (having knowledge of Text based C/C++) dhirsolo Programming 3 08-02-2006 01:05 PM
Equivalents of the getch() from conio.h in curses and ncurses aneeshm Programming 1 09-11-2004 07:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration