]> gitweb.ps.run Git - iftint/blobdiff - main2.c
notes.md, changes in GetNode()
[iftint] / main2.c
diff --git a/main2.c b/main2.c
index 12f99d4f5daaf8d5ae6e27fd43c78822158ab214..aeb0c5586d7daa90902f8d1ce203bd3c47c6459a 100644 (file)
--- a/main2.c
+++ b/main2.c
 #define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE)))
 
 
-// getch()
+// Util
+
+bool
+charInString(char c, const char * str) {
+    for (int i = 0; i < strlen(str); i++)
+        if (c == str[i])
+            return true;
+    return false;
+}
+
+bool
+isNewline(char c) {
+    return c == '\n' || c == '\r';
+}
+bool
+isBackspace(char c) {
+    return c == 8 || c == 127;
+}
+
+
+// Key defines
+
+#define KEY_CTRL_C 3
+#define KEY_BACKSPACE1 8
+#define KEY_BACKSPACE2 127
+
+
+// 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 +59,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 );
@@ -90,17 +105,31 @@ void vt100CursorHome() { vt100Escape("[H"); }
 void vt100CursorPos(int v, int h) { vt100Escape("[%d;%dH", v, h); }
 void vt100SaveCursor() { vt100Escape("7"); }
 void vt100RestoreCursor() { vt100Escape("8"); }
+// void vt100GetCursor(int * v, int * h) {
+//     *v = *h = 0;
+//     printf("\033[6n");
+//     getch(); getch();
+//     int c;
+//     while ((c = getch()) != ';')
+//         *v = (10*(*v)+(c-'0'));
+//     while ((c = getch()) != 'R')
+//         *h = (10*(*h)+(c-'0'));
+// }
 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
 }
+void vt100EnableAlternateBuffer() { vt100Escape("[?1049h"); }
+void vt100DisableAlternateBuffer() { vt100Escape("[?1049l"); }
 
 
 // JSON
@@ -111,6 +140,7 @@ typedef enum {
     JSONNodeKind_Str,
     JSONNodeKind_Obj,
     JSONNodeKind_Arr,
+    JSONNodeKind_COUNT
 } JSONNodeKind;
 
 struct JSONNode;
@@ -118,78 +148,59 @@ typedef struct JSONNode {
     JSONNodeKind kind;
     size_t data;
     struct JSONNode * parent;
-    struct JSONNode * children;
+    struct JSONNode * firstChild;
+    int childCount;
+    struct JSONNode * prev;
     struct JSONNode * next;
 } JSONNode;
 
 JSONNode *
-JSONNodeNew(JSONNodeKind kind, size_t data) {
+JSONNodeNew(JSONNodeKind kind) {
     JSONNode * result = NEW(JSONNode);
     result->kind = kind;
-    result->data = data;
     return result;
 }
 
-JSONNode *
-JSONNodeNewNul() {
-    return JSONNodeNew(JSONNodeKind_Nul, (size_t)NULL);
-}
-
-JSONNode *
-JSONNodeNewInt(int i) {
-    return JSONNodeNew(JSONNodeKind_Int, (size_t)i);
-}
-
-JSONNode *
-JSONNodeNewStr(const char * str) {
-    return JSONNodeNew(JSONNodeKind_Str, (size_t)str);
-}
-
-JSONNode *
-JSONNodeNewObj() {
-    return JSONNodeNew(JSONNodeKind_Obj, (size_t)NULL);
-}
-
-JSONNode *
-JSONNodeNewArr() {
-    return JSONNodeNew(JSONNodeKind_Arr, (size_t)NULL);
-}
-
 JSONNode *
 JSONNodePush(JSONNode * this, JSONNode * that) {
-    if (this->children == NULL) {
-        this->children = that;
+    if (this->firstChild == NULL) {
+        this->firstChild = that;
     }
     else {
-        JSONNode * lastNode = this->children;
+        JSONNode * lastNode = this->firstChild;
         while (lastNode->next != NULL)
             lastNode = lastNode->next;
         lastNode->next = that;
+        that->prev = lastNode;
     }
+    this->childCount++;
     that->parent = this;
-    that->next = NULL;
     
     return that;
 }
 
-void
-JSONNodePop(JSONNode * this) {
-    if (this != NULL) {
-        JSONNode * ptr = this->children;
-
-        if (ptr == NULL) { // no children
-            JSONNodePop(this->parent);
-        }
-        else if (ptr->next == NULL) { // one child
-            this->children = NULL;
-            
-        }
-        else { // more than one child
-            while (ptr->next->next != NULL)
-                ptr = ptr->next;
-            ptr->next = NULL;
-        }
+JSONNode *
+JSONNodeRemove(JSONNode * node) {
+    if (node->prev == NULL) { // first child
+        node->parent->firstChild = node->next;
+        if (node->next != NULL)
+            node->next->prev = NULL;
+        return node->parent;
     }
+    else { // second child
+        node->prev->next = node->next;
+        if (node->next != NULL)
+            node->next->prev = node->prev;
+        return node->prev;
+    }
+}
+
+bool
+JSONNodeEditable(JSONNode * node) {
+    if (node == NULL) return false;
+    else if (node->kind == JSONNodeKind_Int) return true;
+    else if (node->kind == JSONNodeKind_Str) return true;
+    return false;
 }
 
 void
@@ -199,22 +210,26 @@ Indent(int indent) {
 }
 
 void
-JSONNodePrint(JSONNode * node) {
+JSONNodePrint(JSONNode * node, JSONNode * currNode) {
     if (node == NULL)
         return;
     
     static int indent;
     if (node->parent == NULL)
         indent = 0;
-    
+
+    if (currNode == node) {
+        vt100SaveCursor();
+    }
+
     switch (node->kind) {
     case JSONNodeKind_Nul: {
         printf("null");
         break;
     }
     case JSONNodeKind_Int: {
-        int i = (int)node->data;
-        printf("%d", i);
+        char * str = (char *)node->data;
+        printf("%s", (str == NULL || strlen(str) == 0) ? "0" : str);
         break;
     }
     case JSONNodeKind_Str: {
@@ -224,19 +239,17 @@ JSONNodePrint(JSONNode * node) {
     }
     case JSONNodeKind_Obj: {
         printf("{\n");
-        JSONNode * ptr = node->children;
+        JSONNode * ptr = node->firstChild;
         indent++;
         while (ptr != NULL) {
-            char * key = (char *)ptr->data;
-            JSONNode * value = ptr->next;
             Indent(indent);
-            printf("\"%s\": ", key);
-            JSONNodePrint(value);
+            JSONNodePrint(ptr, currNode);
+            printf(": ");
+            JSONNodePrint(ptr->firstChild, currNode);
             if (ptr->next != NULL)
-                ptr = ptr->next->next;
-            else
-                ptr = NULL;
-            printf("%s\n", (ptr == NULL ? "" : ","));
+                printf(",");
+            ptr = ptr->next;
+            printf("\n");
         }
         indent--;
         Indent(indent);
@@ -244,15 +257,20 @@ JSONNodePrint(JSONNode * node) {
         break;
     }
     case JSONNodeKind_Arr: {
-        printf("[ ");
-        JSONNode * ptr = node->children;
+        printf("[\n");
+        JSONNode * ptr = node->firstChild;
+        indent++;
         while (ptr != NULL) {
-            JSONNode * value = ptr;
-            JSONNodePrint(value);
+            Indent(indent);
+            JSONNodePrint(ptr, currNode);
+            if (ptr->next != NULL)
+                printf(",");
+            printf("\n");
             ptr = ptr->next;
-            printf("%s", (ptr == NULL ? "" : ", "));
         }
-        printf(" ]");
+        indent--;
+        Indent(indent);
+        printf("]");
         break;
     }
     }
@@ -262,22 +280,17 @@ JSONNodePrint(JSONNode * node) {
 // Input
 
 JSONNode * g_DrawNode = NULL;
-const char * g_DrawStr = "";
+JSONNode * g_CurrNode = NULL;
 
 void
 Draw(void) {
     vt100ClearScreen();
     vt100CursorHome();
     
-    if (g_DrawNode != NULL)
-        JSONNodePrint(g_DrawNode);
-
-    int v, h;
-    vt100GetScreenSize(&v, &h);
-    vt100CursorPos(v, 0);
-    printf("> %s", g_DrawStr);
-
-    vt100CursorPos(v, strlen(g_DrawStr) + 3);
+    if (g_DrawNode != NULL) {
+        JSONNodePrint(g_DrawNode, g_CurrNode);
+        vt100RestoreCursor();
+    }
 }
 
 int
@@ -289,105 +302,127 @@ GetChar() {
 
 int
 PeekChar() {
-    int c = GetChar();
-    ungetch(c);
+    Draw();
+    int c = peekch();
     return c;
 }
 
-int
-GetInt() {
-    static char intStr[16];
-    int intStrLen = 0;
-    int result = 0;
-    int c;
-    g_DrawStr = intStr;
-    while ((c = GetChar()), (c != '\r') && (c != '\n')) {
-        if ((c == 8 || c == 127) && intStrLen > 0) {
-            intStrLen--;
-            intStr[intStrLen] = '\0';
-            result /= 10;
-        }
-        else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) {
-            intStr[intStrLen++] = c;
-            intStr[intStrLen] = '\0';
-            result *= 10;
-            result += c - '0';
-        }
-    }
-    g_DrawStr = "";
-    return result;
-}
+typedef bool(*CharPredicateFunc)(char, int);
 
-char *
-GetStr() {
-    char * str = NEWARR(char, 16);
-    int strLen = 0;
-    int c;
-    g_DrawStr = str;
-    while ((c = GetChar()), (c != '\r') && (c != '\n')) {
-        if ((c == 8 || c == 127) && strLen > 0) {
-            strLen--;
-            str[strLen] = '\0';
-        }
-        else if (strLen < 16 - 1) {
-            str[strLen++] = c;
-            str[strLen] = '\0';
-        }
-    }
-    g_DrawStr = "";
-    return str;
-}
+bool predStr(char c, int i) { return c >= 'a' && c <= 'z'; }
+bool predInt(char c, int i) { return c >= '0' && c <= '9'; }
 
 JSONNode *
-GetNode(JSONNode * parent) {
-    int c = GetChar();
-
-    JSONNode * result = JSONNodeNewNul();
-    
-    if (parent == NULL)
-        g_DrawNode = result;
-
-    if (parent != NULL && result != NULL)
-        JSONNodePush(parent, result);
+GetNode() {
+    JSONNode * result = NULL;
+    JSONNode * node = NULL;
+    int c;
+    int strLen = 0;
 
-    switch (c) {
-    case 'i': {
-        result->kind = JSONNodeKind_Int;
-        result->data = (size_t)GetInt();
-        break;
-    }
-    case 's': {
-        result->kind = JSONNodeKind_Str;
-        result->data = (size_t)GetStr();
-        break;
-    }
-    case 'o': {
-        result->kind = JSONNodeKind_Obj;
-        while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
-            JSONNodePush(result, JSONNodeNewStr(GetStr()));
+    bool editingNode = false;
+
+    while (true) {
+        c = GetChar();
+
+        if (c == KEY_CTRL_C)
+            break;
+
+        /*
+
+        - editing
+          - primitive -> input
+          - obj/arr   -> new node inside
+        - not editing
+          - hjkl      -> movement
+          - isoa
+            - parent=obj -> new key/value node after
+            - parent=arr -> new node after
+            - else       -> nothing
+        */
+
+        if (isNewline(c)) {
+            if (editingNode) {
+                editingNode = false;
+                if (node->parent != NULL && node->parent->kind == JSONNodeKind_Obj && node->firstChild != NULL) {
+                    node = node->firstChild;
+                    editingNode = true;
+                }
+                else if (node->parent != NULL && node->parent->kind == JSONNodeKind_Str && node->parent->parent != NULL) {
+                    node = node->parent->parent;
+                    editingNode = true;
+                }
+            }
+            else {
+                if (node->parent != NULL)
+                    node = node->parent;
+            }
+
+            g_CurrNode = node;
+            
+            continue;
+        }
 
-            JSONNodePush(result, GetNode(result));
+        if (editingNode) {
+            if (JSONNodeEditable(node)) {
+                if (node->data == (size_t)NULL) {
+                    node->data = (size_t)NEWARR(char, 16);
+                    strLen = 0;
+                }
+                
+                char * str = (char *)node->data;
+
+                if (isBackspace(c)) {
+                    str[strLen-1] = '\0';
+                    strLen--;
+                }
+                else if (strLen < 16 - 1) {
+                    str[strLen] = c;
+                    str[strLen+1] = '\0';
+                    strLen++;
+                }
+            }
+            else {
+                if (node->kind == JSONNodeKind_Obj) {
+                    /**/ if (c == 'i') { node = JSONNodePush(node, JSONNodePush(JSONNodeNew(JSONNodeKind_Str), JSONNodeNew(JSONNodeKind_Int))->parent); }
+                    else if (c == 's') { node = JSONNodePush(node, JSONNodePush(JSONNodeNew(JSONNodeKind_Str), JSONNodeNew(JSONNodeKind_Str))->parent); }
+                    else if (c == 'o') { node = JSONNodePush(node, JSONNodePush(JSONNodeNew(JSONNodeKind_Str), JSONNodeNew(JSONNodeKind_Obj))->parent); }
+                    else if (c == 'a') { node = JSONNodePush(node, JSONNodePush(JSONNodeNew(JSONNodeKind_Str), JSONNodeNew(JSONNodeKind_Arr))->parent); }
+                }
+                else if (node->kind == JSONNodeKind_Arr) {
+                    /**/ if (c == 'i') { node = JSONNodePush(node, JSONNodeNew(JSONNodeKind_Int)); }
+                    else if (c == 's') { node = JSONNodePush(node, JSONNodeNew(JSONNodeKind_Str)); }
+                    else if (c == 'o') { node = JSONNodePush(node, JSONNodeNew(JSONNodeKind_Obj)); }
+                    else if (c == 'a') { node = JSONNodePush(node, JSONNodeNew(JSONNodeKind_Arr)); }
+                }
+                g_CurrNode = node;
+            }
         }
-        GetChar();
-        break;
-    }
-    case 'a': {
-        result->kind = JSONNodeKind_Arr;
-        while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
-            JSONNodePush(result, GetNode(result));
+        else {
+            if (node == NULL) {
+                /**/ if (c == 'i') { node = JSONNodeNew(JSONNodeKind_Int); editingNode = true; }
+                else if (c == 's') { node = JSONNodeNew(JSONNodeKind_Str); editingNode = true; }
+                else if (c == 'o') { node = JSONNodeNew(JSONNodeKind_Obj); editingNode = true; }
+                else if (c == 'a') { node = JSONNodeNew(JSONNodeKind_Arr); editingNode = true; }
+                g_DrawNode = g_CurrNode = result = node;
+            }
+            // else if (node->kind == JSONNodeKind_Str && node->parent != NULL && node->parent->kind == JSONNodeKind_Obj) {
+                
+            // }
+            else {
+                /**/ if (c == 'i' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Int)); editingNode = true; }
+                else if (c == 's' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Str)); editingNode = true; }
+                else if (c == 'o' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Obj)); editingNode = true; }
+                else if (c == 'a' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Arr)); editingNode = true; }
+                else if (c == 'h') { if (node->prev       != NULL) g_CurrNode = node = node->prev; }
+                else if (c == 'l') { if (node->next       != NULL) g_CurrNode = node = node->next; }
+                else if (c == 'k') { if (node->parent     != NULL) g_CurrNode = node = node->parent; }
+                else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; }
+                else if (c == ' ') { editingNode = true; if (JSONNodeEditable(node) && (void *)node->data != NULL) strLen = strlen((char *)node->data); }
+                else if (isBackspace(c)) { g_CurrNode = node = JSONNodeRemove(node); }
+
+                g_CurrNode = node;
+            }
         }
-        GetChar();
-        break;
-    }
-    case 8:
-    case 127:
-        JSONNodePop(parent);
-        result = GetNode(parent);
-        break;
-    case 't':
-        result->kind = JSONNodeKind_Int;
-        result->data = (size_t)GetChar();
-        break;
     }
 
     return result;
@@ -397,15 +432,13 @@ GetNode(JSONNode * parent) {
 
 
 int main() {
-    Draw();
+    vt100EnableAlternateBuffer();
 
-    JSONNode * n = GetNode(NULL);
-    //JSONNode * n = TestNode();
+    JSONNode * n = GetNode();
+    
+    vt100DisableAlternateBuffer();
 
-    vt100ClearScreen();
-    vt100CursorHome();
-    JSONNodePrint(n);
-    printf("\n");
+    JSONNodePrint(n, NULL);
 
     // JSONFree(n);