]> gitweb.ps.run Git - iftint/blobdiff - main2.c
fix peekchar input
[iftint] / main2.c
diff --git a/main2.c b/main2.c
index 51c5ad4975c359f22dddb65beb005f4edc178ff1..104c2a47a021ad8b347aea2f563e184ac29f5804 100644 (file)
--- a/main2.c
+++ b/main2.c
 #define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE)))
 
 
-// getch()
+// getch
 
 #ifdef _WIN32
+#include <windows.h>
 #include <conio.h>
 #else
+#include <sys/ioctl.h>
 #include <termios.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -30,21 +32,7 @@ int getch(void)
     int ch;
     tcgetattr( STDIN_FILENO, &oldattr );
     newattr = oldattr;
-    newattr.c_lflag &= ~( ICANON | ECHO );
-    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
-    ch = getchar();
-    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
-    return ch;
-}
-
-/* reads from keypress, echoes */
-int getche(void)
-{
-    struct termios oldattr, newattr;
-    int ch;
-    tcgetattr( STDIN_FILENO, &oldattr );
-    newattr = oldattr;
-    newattr.c_lflag &= ~( ICANON );
+    newattr.c_lflag &= ~( ICANON | ECHO ); // no ECHO for echo(?)
     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
     ch = getchar();
     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
@@ -91,15 +79,17 @@ void vt100CursorPos(int v, int h) { vt100Escape("[%d;%dH", v, h); }
 void vt100SaveCursor() { vt100Escape("7"); }
 void vt100RestoreCursor() { vt100Escape("8"); }
 void vt100GetScreenSize(int * v, int * h) {
-    *v = *h = 0;
-    vt100CursorPos(1000000, 1000000);
-    printf("\033[6n");
-    getch(); getch();
-    int c;
-    while ((c = getch()) != ';')
-        *v = (10*(*v)+(c-'0'));
-    while ((c = getch()) != 'R')
-        *h = (10*(*h)+(c-'0'));
+#ifdef _WIN32
+    CONSOLE_SCREEN_BUFFER_INFO csbi;
+    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
+    *h = csbi.srWindow.Right - csbi.srWindow.Left + 1;
+    *v = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
+#else
+    struct winsize w;
+    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
+    *h = w.ws_row;
+    *v = w.ws_col;
+#endif
 }
 
 
@@ -289,8 +279,8 @@ GetChar() {
 
 int
 PeekChar() {
-    int c = GetChar();
-    ungetch(c);
+    Draw();
+    int c = peekch();
     return c;
 }
 
@@ -364,20 +354,20 @@ GetNode(JSONNode * parent) {
     }
     case 'o': {
         result->kind = JSONNodeKind_Obj;
-        while ((c = peekch()), (c != '\r') && (c != '\n')) {
+        while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
             JSONNodePush(result, JSONNodeNewStr(GetStr()));
 
             JSONNodePush(result, GetNode(result));
         }
-        getch();
+        GetChar();
         break;
     }
     case 'a': {
         result->kind = JSONNodeKind_Arr;
-        while ((c = peekch()), (c != '\r') && (c != '\n')) {
+        while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
             JSONNodePush(result, GetNode(result));
         }
-        getch();
+        GetChar();
         break;
     }
     case 8: