Pages

Showing posts with label ncurses-devel. Show all posts
Showing posts with label ncurses-devel. Show all posts

Wednesday, June 9, 2021

Fedora 34 : Programming with the ncurses library - part 003.

This is a simple example with ncurses library to see how can define an array and used with random colors:
This example is build with this command:
[mythcat@desk ncursesProject]$ gcc test_008.c -o test_008 -lncurses
This is source code:
#include <ncurses.h>
#include <stdlib.h> 

int main(void) {
    
    initscr();
    
    start_color();
    
    char colors[8][20] = {
    "COLOR_BLACK",
    "COLOR_RED",
    "COLOR_GREEN",
    "COLOR_YELLOW",
    "COLOR_BLUE",
    "COLOR_MAGENTA",
    "COLOR_CYAN",
    "COLOR_WHITE"
    };
    
    int n;
    for ( n=0 ; n<16 ; ++n )
    {
        int aleator1 = rand() % 256 + 1;
        int result1 = *colors[aleator1];
        int aleator2 = rand() % 256 + 1;
        int result2 = *colors[aleator2];
        //printw("%d",result1);
        init_pair(n, result1, result2);
        attron(COLOR_PAIR(n));
        printw("Hello word!\n");
    }
    
    refresh();

    getch();

    endwin();
}

Tuesday, May 25, 2021

Fedora 33 : Programming with the ncurses library - part 002.

Another example with this library, see the screenshot:

#include <ncurses.h&gt 

WINDOW *win;
int startx, starty, width, height;
int cport;


WINDOW *makewin(int h, int w, int y, int x)
{
    WINDOW *lwin;

    lwin = newwin(h, w, y, x);
    box(lwin, 0 , 0);
    wrefresh(lwin);

    return lwin;
}

void dewin(WINDOW *lwin)
{
    wborder(lwin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
    wrefresh(lwin);
    delwin(lwin);
}

void getInput(){
    win = makewin(height, width, starty, startx);
    wbkgd(win, COLOR_PAIR(1));
    mvwprintw(win, 0, 8, "Question 1 : ");
    mvwprintw(win, 2, 4, "What is your age?");
    mvwprintw(win, 3, 4, "dd/mm/yy:");
    wrefresh(win);
    wscanw(win, "%d", &cport);
}

int main()
{
    initscr();
    cbreak();
    keypad(stdscr, TRUE);
    start_color();
    init_pair(1,COLOR_WHITE, COLOR_BLACK);
    init_pair(2,COLOR_WHITE, COLOR_BLUE);
    bkgd(COLOR_PAIR(2));
    refresh();
    
    height = 6;
    width = 30;
    starty = (LINES - height) / 2;
    startx = (COLS - width) / 2;
    
    getInput();
    getch();

    dewin(win);
    endwin();
    return 0;
}

Sunday, May 23, 2021

Fedora 33 : Programming with the ncurses library - part 001.

I started learning C and C ++ many years ago. Around 2004, the virtualization and S.D.K. class system for the Windows operating system confused me and was not to my advantage. Later I realized that it is more advantageous and faster to create your own tools with other programming languages. It turned out that I'm right and today I'm going to show you how simple it is to use C programming on the Linux Fedora 33 distro.
This library is easy to install on the Fedora Linux distro:
[root@desk mythcat]# dnf install ncurses-devel
Last metadata expiration check: 2:33:29 ago on Sun 23 May 2021 07:15:17 PM EEST.
Package ncurses-devel-6.2-3.20200222.fc33.x86_64 is already installed.
Package ncurses-devel-6.2-3.20200222.fc33.i686 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
I created a folder and with the vim editor, I created my file called test_001.c for the first test.
[mythcat@desk ~]$ mkdir ncursesProject
[mythcat@desk ~]$ cd ncursesProject/
[mythcat@desk ncursesProject]$ vim test_001.c
This is the content to initiate and display a simple text with this library:
#include <ncurses.h>
 
int main(void){ 
        initscr();                      /* start curses mode */
        printw("Hello World !");        /* print Hello World */
        refresh();                      /* send to the real screen */
        getch();                        /* wait for user input */
        endwin();                       /* end curses mode */
        return 0;
} 
I used cc and gcc commands for C compilers to test this example and it works great:
[mythcat@desk ncursesProject]$ cc -o test_001 test_001.c -lncurses
[mythcat@desk ncursesProject]$ ls -l
total 32
-rwxr-xr-x 1 mythcat mythcat 25312 May 23 22:14 test_001
-rw-r--r-- 1 mythcat mythcat   386 May 23 22:14 test_001.c
[mythcat@desk ncursesProject]$ ./test_001 
[mythcat@desk ncursesProject]$ gcc -o test_001_gcc test_001.c -lncurses
[mythcat@desk ncursesProject]$ ./test_001_gcc 
#include <ncurses.h>

int main() { 
    // create the pointer to the window
    WINDOW *w;
    // define the data
    char list[4][8] = { "","Festila", "George", "Catalin"};
    char item[8];
    int ch, i = 0, width = 8;
    initscr(); // initialize Ncurses
    // create the new window with ncurses
    w = newwin( 6, 11, 0, 0 ); 
    box( w, 0, 0 ); // sets default borders for the window
    // now print all the menu items and highlight the first one
    for(i=0; i<4; i++) 
        {
        if( i != 0 ) 
            // highlights the first item.
            wattron( w, A_STANDOUT ); 
        else
            // highlights the next item.
            wattroff( w, A_STANDOUT );
            sprintf(item, "%-8s",  list[i]);
            mvwprintw( w, i+1, 2, "%s", item );
            //wrefresh( w );
        }
        wrefresh( w ); // update the terminal screen
        i = 0;
        noecho(); // disable echoing of characters on the screen
        keypad( w, TRUE ); // enable keyboard input for the window.
        curs_set(0); // hide the default screen cursor.
        
    // get the input if is not q key presed then run
    while(( ch = wgetch(w)) != 'q')
    { 
        // right pad with spaces to make the items appear with even width.
        sprintf(item, "%-8s",  list[i]); 
        mvwprintw( w, i+1, 2, "%s", item ); 

        // use a variable to increment or decrement the value based on the input.
        switch( ch ) 
        {
            case KEY_UP:
            i--; // increment 
            // stop the number of count list items -1
            i = ( i<0 ) ? 3 : i;
            break;
            case KEY_DOWN:
            i++; // increment
            // stop the number of count list items 
            i = ( i>2 ) ? 3 : i;
            break;
        }
    
        // now highlight the next item in the list.
        wattron( w, A_STANDOUT );
        sprintf(item, "%-8s",  list[i]);
        mvwprintw( w, i+1, 2, "%s", item);
        wattroff( w, A_STANDOUT );
    }
    // delete window
    delwin( w );
    // stop the window
    endwin();
}
This is the result: