From: andy Date: Fri, 24 Aug 2012 09:39:37 +0000 (+1200) Subject: First commit. X-Git-Url: https://pd.if.org/git/?p=pd_readline;a=commitdiff_plain;h=26fe4e09c6c3c250334fdeed60ce3061febecde2 First commit. --- 26fe4e09c6c3c250334fdeed60ce3061febecde2 diff --git a/.history b/.history new file mode 100644 index 0000000..aa10a34 --- /dev/null +++ b/.history @@ -0,0 +1,17 @@ +ls +git add . +git commit -a -m "Added a C version of the editor." +git push origin master +cd pdeditor +./pdeditor +cd pdeditor +./pdeditor +mkdir foo +make -f makefile +clear +./struct_array +cd struct_array +python +cd struct_array +./struct_array + diff --git a/README b/README new file mode 100644 index 0000000..a69390f --- /dev/null +++ b/README @@ -0,0 +1,9 @@ + + ***** README - pd_readline ***** + + This repo is for the (eventual) storage of a public-domain +readline-and-command-history implementation. + + - mooseman + + diff --git a/pdmenu.c b/pdmenu.c new file mode 100644 index 0000000..34f3404 --- /dev/null +++ b/pdmenu.c @@ -0,0 +1,130 @@ + + +/* pdmenu.c */ +/* This code is aimed at helping those who would like to */ +/* create menus without using the curses library. */ +/* It creates a count variable (which could be used to */ +/* keep track of which item is highlighted). Using the */ +/* up and down arrow keys increments and decrements the */ +/* count variable. Finally, when you press Enter, the */ +/* value of the count variable is printed and the program */ +/* exits. */ +/* This code is released to the public domain. */ +/* "Share and enjoy...." ;) */ + + +#include +#include +#include /* For getch() */ + +/* This implementation of getch() is from here - */ +/* http://wesley.vidiqatch.org/ */ +/* Thanks, Wesley! */ +static struct termios old, new; + +/* Initialize new terminal i/o settings */ +void initTermios(int echo) { + tcgetattr(0, &old); /* grab old terminal i/o settings */ + new = old; /* make new settings same as old settings */ + new.c_lflag &= ~ICANON; /* disable buffered i/o */ + new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ + tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */ +} + + +/* Restore old terminal i/o settings */ +void resetTermios(void) { + tcsetattr(0, TCSANOW, &old); +} + + +/* Read 1 character - echo defines echo mode */ +char getch_(int echo) { + char ch; + initTermios(echo); + ch = getchar(); + resetTermios(); + return ch; +} + + +/* Read 1 character without echo */ +char getch(void) { + return getch_(0); +} + + +/* Read 1 character with echo */ +char getche(void) { + return getch_(1); +} + + + + +int main(void) +{ + + printf("Public Domain Menu Program \n"); + printf("\nUse the up and down arrow keys then press Enter\n"); + + int menunum = 0; + + while(1) + { + + int key = getch(); + + /* Up arrow is 27, 91, 65. ( ESC [ A ) */ + /* Down arrow is 27, 91, 66. ( ESC [ B ) */ + /* Right arrow is 27, 91, 67. ( ESC [ C ) */ + /* Left arrow is 27, 91, 68. ( ESC [ D ) */ + if(key == 27) + { key = getch(); + if(key == 91) + key = getch(); + if(key == 65) + { puts("You pressed up arrow! \n"); + menunum-=1; + printf("Menunum is now %d \n", menunum); + } + else if(key == 66) + { puts("You pressed down arrow! \n"); + menunum+=1; + printf("Menunum is now %d \n", menunum); + } + else if(key == 67) + { puts("You pressed right arrow! \n"); + } + else if(key == 68) + { puts("You pressed left arrow! \n"); + } + } + + /* A few other keys (for testing purposes ) */ + else if(key == 97) + { puts("You pressed the \"a\" key! \n"); + } + else if(key == 98) + { puts("You pressed the \"b\" key! \n"); + } + else if(key == 99) + { puts("You pressed the \"c\" key! \n"); + } + else if(key == 100) + { puts("You pressed the \"d\" key! \n"); + } + + + /* The Enter key exits. Enter is 10 decimal */ + else if(key == 10) + { printf("You pressed ENTER! You chose item %d \n", menunum); + break; } + } + + return 0; +} + + + +