]> pd.if.org Git - pd_readline/commitdiff
First commit.
authorandy <andy@obsidian.(none)>
Fri, 24 Aug 2012 09:39:37 +0000 (21:39 +1200)
committerandy <andy@obsidian.(none)>
Fri, 24 Aug 2012 09:39:37 +0000 (21:39 +1200)
.history [new file with mode: 0644]
README [new file with mode: 0644]
pdmenu.c [new file with mode: 0644]

diff --git a/.history b/.history
new file mode 100644 (file)
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 (file)
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 (file)
index 0000000..34f3404
--- /dev/null
+++ b/pdmenu.c
@@ -0,0 +1,130 @@
+\r
+\r
+/*  pdmenu.c                                                */  \r
+/*  This code is aimed at helping those who would like to   */ \r
+/*  create menus without using the curses library.          */ \r
+/*  It creates a count variable (which could be used to     */ \r
+/*  keep track of which item is highlighted). Using the     */ \r
+/*  up and down arrow keys increments and decrements the    */ \r
+/*  count variable. Finally, when you press Enter, the      */ \r
+/*  value of the count variable is printed and the program  */ \r
+/*  exits.                                                  */       \r
+/*  This code is released to the public domain.             */ \r
+/*  "Share and enjoy...."  ;)                               */  \r
+\r
+\r
+#include <string.h>   \r
+#include <stdio.h> \r
+#include <termios.h>  /* For getch()  */  \r
+\r
+/* This implementation of getch() is from here - */ \r
+/* http://wesley.vidiqatch.org/                  */ \r
+/* Thanks, Wesley!                               */  \r
+static struct termios old, new;\r
+\r
+/* Initialize new terminal i/o settings */\r
+void initTermios(int echo) {\r
+    tcgetattr(0, &old); /* grab old terminal i/o settings */\r
+    new = old; /* make new settings same as old settings */\r
+    new.c_lflag &= ~ICANON; /* disable buffered i/o */\r
+    new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */\r
+    tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */\r
+}\r
+\r
+\r
+/* Restore old terminal i/o settings */\r
+void resetTermios(void) {\r
+    tcsetattr(0, TCSANOW, &old);\r
+}\r
+\r
+\r
+/* Read 1 character - echo defines echo mode */\r
+char getch_(int echo) {\r
+    char ch;\r
+    initTermios(echo);\r
+    ch = getchar();\r
+    resetTermios();\r
+    return ch;\r
+}\r
+\r
+\r
+/* Read 1 character without echo */\r
+char getch(void) {\r
+    return getch_(0);\r
+}\r
+\r
+\r
+/* Read 1 character with echo */\r
+char getche(void) {\r
+    return getch_(1);\r
+} \r
+\r
+\r
+\r
+\r
+int main(void)\r
+{\r
+       \r
+  printf("Public Domain Menu Program \n");              \r
+  printf("\nUse the up and down arrow keys then press Enter\n"); \r
+  \r
+  int menunum = 0;            \r
+  \r
+  while(1) \r
+    {  \r
+                   \r
+      int key = getch(); \r
+                                                                       \r
+      /* Up arrow is 27, 91, 65.    ( ESC [ A )   */   \r
+      /* Down arrow is 27, 91, 66.  ( ESC [ B )   */ \r
+      /* Right arrow is 27, 91, 67. ( ESC [ C )   */ \r
+      /* Left arrow is 27, 91, 68.  ( ESC [ D )   */              \r
+      if(key == 27)  \r
+          {   key = getch(); \r
+              if(key == 91) \r
+              key = getch(); \r
+              if(key == 65)                              \r
+               { puts("You pressed up arrow! \n"); \r
+                        menunum-=1; \r
+                 printf("Menunum is now %d \n", menunum);  \r
+               }  \r
+              else if(key == 66)                              \r
+               { puts("You pressed down arrow! \n"); \r
+                        menunum+=1; \r
+                 printf("Menunum is now %d \n", menunum);  \r
+               }  \r
+              else if(key == 67)                              \r
+               { puts("You pressed right arrow! \n");                   \r
+               }  \r
+              else if(key == 68)                              \r
+               { puts("You pressed left arrow! \n");                    \r
+               }                \r
+          }\r
+              \r
+   /* A few other keys (for testing purposes ) */  \r
+     else if(key == 97) \r
+            { puts("You pressed the \"a\" key! \n");                    \r
+            }                    \r
+     else if(key == 98) \r
+            { puts("You pressed the \"b\" key! \n");                    \r
+            }   \r
+     else if(key == 99) \r
+            { puts("You pressed the \"c\" key! \n");                    \r
+            }\r
+     else if(key == 100) \r
+            { puts("You pressed the \"d\" key! \n");                    \r
+            }        \r
+                                          \r
+                                                 \r
+    /* The Enter key exits. Enter is 10 decimal */       \r
+        else if(key == 10)  \r
+               { printf("You pressed ENTER! You chose item %d \n", menunum);  \r
+          break; }  \r
+    }                \r
+                                      \r
+       return 0;\r
+}  \r
+\r
+\r
+\r
+\r