Thread: ncurses example program conversion to C++

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,077

    ncurses example program conversion to C++

    I have decide to make an simple C++ and shell script program to help me with git commands.

    My current request is for you to look for things not done correctly for C++. I tried to use const where ever it makes sense already.

    Code started out life here ncurses_programs/simple_key.c at master * cpressey/ncurses_programs * GitHub or some place with almost exactly the same code.

    My current code
    Code:
    #include <stdio.h>
    #include <curses.h>
    
    // based on https://github.com/cpressey/ncurses_programs/blob/master/basics/simple_key.c
    
    const int TERMINAL_WIDTH=80;
    const int TERMINAL_HEIGHT=24;
    
    const int WIDTH=50;
    const int HEIGHT=9;
    const int MENU_TOP_OFFSET=1;
    const int MENU_LEFT_OFFSET=2;
    
    const int START_X = (TERMINAL_WIDTH - WIDTH) / 2;
    const int START_Y = (TERMINAL_HEIGHT - HEIGHT) / 2;
    
    const char *choices[] =
    {
        "git push --help",
        "git push",
        "git push origin",
        "git pull --rebase",
        "git pull --rebase && git push",
        "git checkout master && ... && git push origin",
        "Exit",
    };
    int const n_choices = sizeof(choices) / sizeof(char *);
    void print_menu(WINDOW *menu_win, const int highlight);
    
    int main()
    {
        WINDOW *menu_win;
        int highlight = n_choices;
        int choice = 0;
        int c;
    
        initscr();
        clear();
        noecho();
        cbreak();
    
        menu_win = newwin(HEIGHT, WIDTH, START_Y, START_X);
        keypad(menu_win, TRUE);
        mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
        refresh();
        print_menu(menu_win, highlight);
        while(1)
        {
            c = wgetch(menu_win);
            switch(c)
            {
            case KEY_UP:
                if(highlight == 1)
                    highlight = n_choices;
                else
                    --highlight;
                break;
            case KEY_DOWN:
                if(highlight == n_choices)
                    highlight = 1;
                else
                    ++highlight;
                break;
            case 10:
                choice = highlight;
                break;
            default:
                refresh();
                break;
            }
            print_menu(menu_win, highlight);
            if(choice != 0)
                break;
        }
        clrtoeol();
        refresh();
        endwin();
        if (choice >= n_choices)
        {
            return 0;
        }
        else
        {
            return choice;
        }
    }
    
    void print_menu(WINDOW *menu_win, const int highlight)
    {
        int y = MENU_TOP_OFFSET;
    
        box(menu_win, 0, 0);
        for(int i = 0; i < n_choices; ++i)
        {
            if(highlight == i + 1)
            {
                /* High light the present choice */
                wattron(menu_win, A_REVERSE);
                mvwprintw(menu_win, y, MENU_LEFT_OFFSET, "%s", choices[i]);
                wattroff(menu_win, A_REVERSE);
            }
            else
                mvwprintw(menu_win, y, MENU_LEFT_OFFSET, "%s", choices[i]);
            ++y;
        }
        wrefresh(menu_win);
    }
    I am using Win7 and plan to run the program from an "Git for Windows" command prompt.

    Short term goal: Have shell call exe and execute the correct command.
    Long term goal: Have the command executed by the program.
    Very long term goal: Port to an GUI framework.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,077
    Changed case 10 to
    Code:
    case '\n':
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to tell ncurses program terminal size
    By apawamajawa in forum Linux Programming
    Replies: 1
    Last Post: 07-20-2011, 02:04 AM
  2. ncurses program help with arrows keys
    By georgio in forum Game Programming
    Replies: 2
    Last Post: 11-29-2010, 11:43 AM
  3. Program Conversion Help
    By magda3227 in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 01:24 PM
  4. program conversion.
    By nesagsar in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 04:04 PM
  5. Yet another lame program -- ncurses problem
    By w00tw00tkab00t in forum C Programming
    Replies: 2
    Last Post: 01-08-2006, 02:48 PM

Tags for this Thread