Skip to content

Instantly share code, notes, and snippets.

@alexparkjw
Last active September 16, 2020 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexparkjw/8fcb281d962d3a65a92ec8c1e576bd89 to your computer and use it in GitHub Desktop.
Save alexparkjw/8fcb281d962d3a65a92ec8c1e576bd89 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#define NofEL(x ) (sizeof(x ) / sizeof(*(x ) ) )
#define _ARR(a ) (a ), NofEL(a )
#define _CLEAR(w ) werase(w ); wrefresh(w )
#define __POS(p ) p.h, p.w, p.y, p.x
typedef struct Pos{ int h, w, y, x; }structPos;
void printMenu (WINDOW * winCur, int hl, const char *tm[], int n );
void printInsert(WINDOW * winCur, char *str, const char *type );
const char *textMenu[] ={"1. Insert", "2. Search", "3. Remove", "4. Lists", "5. Help", "6. Quit" };
int main(void) { initscr(); clear(); noecho(); cbreak(); curs_set(FALSE); nodelay(stdscr, true); scrollok(stdscr, true);
char *tempBuff = (char* )malloc(sizeof(char )*512 );
int terHeight, terWidth, highlight=1, elemSize=NofEL(textMenu);
getmaxyx(stdscr, terHeight, terWidth );
structPos mPos = {terHeight/2, terWidth/2, terHeight/4, terWidth/4 };
structPos lPos = {terHeight/2, terWidth/2, terHeight/4, 0 };
structPos rPos = {terHeight/2, terWidth/2, terHeight/4, terWidth/2 };
structPos uPos = {terHeight/2, terWidth/2, 0 , terWidth/4 };
structPos dPos = {terHeight/2, terWidth/2, terHeight/2, terWidth/4 };
structPos fPos = {terHeight/1, terWidth/1, 0 , 0 };
structPos luPos = {terHeight/2, terWidth/2, 0 , 0 };
structPos ruPos = {terHeight/2, terWidth/2, 0 , terWidth/2 };
structPos ldPos = {terHeight/2, terWidth/2, terHeight/2, 0 };
structPos rdPos = {terHeight/2, terWidth/2, terHeight/2, terWidth/2 };
WINDOW *winMenu = newwin(__POS(mPos ) ); keypad(winMenu , TRUE );
WINDOW *winInsert = newwin(__POS(luPos) ); keypad(winInsert, TRUE );
WINDOW *winSearch = newwin(__POS(ruPos) ); keypad(winSearch, TRUE );
WINDOW *winRemove = newwin(__POS(ldPos) ); keypad(winRemove, TRUE );
WINDOW *winLists = newwin(__POS(rdPos) ); keypad(winLists , TRUE );
WINDOW *winHelp = newwin(__POS(fPos ) ); keypad(winHelp , TRUE );
while(TRUE) {
wrefresh(winMenu); printMenu(winMenu, highlight, _ARR(textMenu ) );
int cho = wgetch(winMenu);
switch(cho) {
case 'k': case KEY_UP: if(highlight == 1) highlight = elemSize; else --highlight; break;
case 'j': case KEY_DOWN: if(highlight == elemSize ) highlight = 1; else ++highlight; break;
case '\n':
if (highlight == 1 ) { _CLEAR(winMenu ); printInsert(winInsert, tempBuff, "insert" ); } /* if 1 */
else if(highlight == 2 ) { _CLEAR(winMenu ); printInsert(winSearch, tempBuff, "search" ); } /* else if 2*/
else if(highlight == 3 ) { _CLEAR(winMenu ); printInsert(winRemove, tempBuff, "remove" ); } /* else if 3*/
else if(highlight == 4 ) { _CLEAR(winMenu ); printInsert(winLists , tempBuff, "lists" ); } /* else if 4*/
else if(highlight == 5 ) { _CLEAR(winMenu ); printInsert(winHelp , tempBuff, "help" ); } /* else if 5*/
else if(highlight == 6 ) { goto end; } /* else if 6*/
default: refresh( ); } /* switch */ } /* while */
end: clrtoeol(); refresh(); endwin(); return 0; } /* main */
void printMenu(WINDOW *winCur, int hl, const char *tm[], int n ) { box(winCur, 0, 0); wrefresh(winCur );
for(int i=0,y=2,x=2; i<n; i++, y++) {
if(hl == i + 1) { wattron(winCur, A_REVERSE); mvwprintw(winCur, y, x, "%s", tm[i] ); wattroff(winCur, A_REVERSE); } /* if */
else mvwprintw(winCur, y, x, "%s", tm[i] ); } /* for */
wrefresh(winCur); return; } /* printMenu */
void printInsert(WINDOW *winCur, char *str, const char *type ) { box(winCur, 0, 0); wrefresh(winCur);
int ch, i=0, y=2, x=2;
mvwprintw(winCur, y++, x, "Enter Value for %s : ", type);
while( (ch = wgetch(winCur ) ) != '\n' ) {
switch (ch) {
case 127: mvwprintw(winCur, y, --x, " \b" ); i--; break;
default: str[i] = ch; mvwprintw(winCur, y, x++, "%c", str[i]); i++; } /* switch */
if(i < 0 ) i = 0; } /* while */
str[i]='\0';
if(str[0] != '\0') { mvwprintw(winCur, y+1, 2, "Inserted %s", str);
/* root = insertNode(root, str, key, strCmp); */
while( (ch = wgetch(winCur ) ) != '\n') {; } } /* if */
werase(winCur ); wrefresh(winCur ); } /* printInsert */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment