]> gitweb.ps.run Git - iftint/commitdiff
change node editing (wip)#
authorPatrick <patrick.schoenberger@posteo.de>
Fri, 4 Aug 2023 18:29:28 +0000 (19:29 +0100)
committerPatrick <patrick.schoenberger@posteo.de>
Fri, 4 Aug 2023 18:29:28 +0000 (19:29 +0100)
main2.c

diff --git a/main2.c b/main2.c
index 96aba10d0a905313a361756b37abaf1ef117aa39..d8f25817bcb6f465a31b4991175ed9dee97d4e4a 100644 (file)
--- a/main2.c
+++ b/main2.c
@@ -78,6 +78,16 @@ 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) {
 #ifdef _WIN32
     CONSOLE_SCREEN_BUFFER_INFO csbi;
@@ -101,6 +111,7 @@ typedef enum {
     JSONNodeKind_Str,
     JSONNodeKind_Obj,
     JSONNodeKind_Arr,
+    JSONNodeKind_COUNT
 } JSONNodeKind;
 
 struct JSONNode;
@@ -188,15 +199,18 @@ Indent(int indent) {
         printf("  ");
 }
 
+static int currV = 0;
+static int currH = 0;
+
 void
-JSONNodePrint(JSONNode * node) {
+JSONNodePrint(JSONNode * node, JSONNode * currNode) {
     if (node == NULL)
         return;
     
     static int indent;
     if (node->parent == NULL)
         indent = 0;
-    
+
     switch (node->kind) {
     case JSONNodeKind_Nul: {
         printf("null");
@@ -221,7 +235,7 @@ JSONNodePrint(JSONNode * node) {
             JSONNode * value = ptr->next;
             Indent(indent);
             printf("\"%s\": ", key);
-            JSONNodePrint(value);
+            JSONNodePrint(value, currNode);
             if (ptr->next != NULL)
                 ptr = ptr->next->next;
             else
@@ -238,7 +252,7 @@ JSONNodePrint(JSONNode * node) {
         JSONNode * ptr = node->children;
         while (ptr != NULL) {
             JSONNode * value = ptr;
-            JSONNodePrint(value);
+            JSONNodePrint(value, currNode);
             ptr = ptr->next;
             printf("%s", (ptr == NULL ? "" : ", "));
         }
@@ -246,28 +260,34 @@ JSONNodePrint(JSONNode * node) {
         break;
     }
     }
+
+    if (currNode == node) {
+       int currOffsets[JSONNodeKind_COUNT];
+       currOffsets[JSONNodeKind_Nul] = 0;
+       currOffsets[JSONNodeKind_Int] = 0;
+       currOffsets[JSONNodeKind_Str] = 1;
+       currOffsets[JSONNodeKind_Obj] = 1;
+       currOffsets[JSONNodeKind_Arr] = 2;
+        vt100GetCursor(&currV, &currH);
+       currH -= currOffsets[node->kind];
+    }
 }
 
 
 // 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);
+       vt100CursorPos(currV, currH);
+    }
 }
 
 int
@@ -292,7 +312,6 @@ GetInt(JSONNode * node) {
     size_t * i = &node->data;
     
     int c;
-    g_DrawStr = intStr;
     while ((c = GetChar()), (c != '\r') && (c != '\n')) {
         if ((c == 8 || c == 127) && intStrLen > 0) {
             intStrLen--;
@@ -306,7 +325,6 @@ GetInt(JSONNode * node) {
             *i += c - '0';
         }
     }
-    g_DrawStr = "";
 }
 
 void
@@ -317,18 +335,17 @@ GetStr(JSONNode * node) {
     char * str = (char *)node->data;
     
     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] = c;
+           strLen++;
             str[strLen] = '\0';
         }
     }
-    g_DrawStr = "";
 }
 
 void
@@ -336,6 +353,7 @@ GetNode(JSONNode * parent, JSONNode * node) {
     int c = GetChar();
 
     JSONNode * result = node;
+    g_CurrNode = node;
 
     if (parent == NULL)
         g_DrawNode = result;
@@ -360,6 +378,7 @@ GetNode(JSONNode * parent, JSONNode * node) {
            JSONNode * newNode;
            
            newNode = JSONNodeNewStr("");
+           g_CurrNode = newNode;
             JSONNodePush(result, newNode);
            GetStr(newNode);
 
@@ -367,6 +386,7 @@ GetNode(JSONNode * parent, JSONNode * node) {
             JSONNodePush(result, newNode);
            GetNode(result, newNode);
         }
+       g_CurrNode = result;
         GetChar();
         break;
     }
@@ -374,9 +394,11 @@ GetNode(JSONNode * parent, JSONNode * node) {
         result->kind = JSONNodeKind_Arr;
         while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
            JSONNode * newNode = JSONNodeNewNul();
+           g_CurrNode = newNode;
             JSONNodePush(result, newNode);
            GetNode(result, newNode);
         }
+       g_CurrNode = result;
         GetChar();
         break;
     }
@@ -407,7 +429,7 @@ int main() {
 
     vt100ClearScreen();
     vt100CursorHome();
-    JSONNodePrint(n);
+    JSONNodePrint(n, NULL);
     printf("\n");
 
     // JSONFree(n);