]> gitweb.ps.run Git - iftint/blobdiff - main2.c
object editing, press space to edit existing primitive node
[iftint] / main2.c
diff --git a/main2.c b/main2.c
index 80f6a171febeda78e83f076dd945ec8fcce965af..3dcbf8448b9ac57a6760cf51b6fc67875e946c74 100644 (file)
--- a/main2.c
+++ b/main2.c
@@ -34,6 +34,13 @@ isBackspace(char c) {
 }
 
 
 }
 
 
+// Key defines
+
+#define KEY_CTRL_C 3
+#define KEY_BACKSPACE1 8
+#define KEY_BACKSPACE2 127
+
+
 // getch
 
 #ifdef _WIN32
 // getch
 
 #ifdef _WIN32
@@ -188,6 +195,14 @@ JSONNodeRemove(JSONNode * node) {
     }
 }
 
     }
 }
 
+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
 Indent(int indent) {
     for (int i = 0; i < indent; i++)
 void
 Indent(int indent) {
     for (int i = 0; i < indent; i++)
@@ -229,15 +244,11 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) {
         while (ptr != NULL) {
             Indent(indent);
             JSONNodePrint(ptr, currNode);
         while (ptr != NULL) {
             Indent(indent);
             JSONNodePrint(ptr, currNode);
+            printf(": ");
+            JSONNodePrint(ptr->firstChild, currNode);
+            if (ptr->next != NULL)
+                printf(",");
             ptr = ptr->next;
             ptr = ptr->next;
-
-            if (ptr != NULL) {
-                printf(": ");
-                JSONNodePrint(ptr, currNode);
-                if (ptr->next != NULL)
-                    printf(",");
-                ptr = ptr->next;
-            }
             printf("\n");
         }
         indent--;
             printf("\n");
         }
         indent--;
@@ -308,64 +319,86 @@ GetNode() {
     int c;
     int strLen = 0;
 
     int c;
     int strLen = 0;
 
+    typedef enum {
+        InputState_Initial,
+        InputState_Primitive,
+        InputState_Object,
+        InputState_KeyValue,
+        InputState_Array,
+    } InputState;
+    InputState inputState;
+
     bool editingNode = false;
 
     while (true) {
         c = GetChar();
 
     bool editingNode = false;
 
     while (true) {
         c = GetChar();
 
-        if (c == 3)
+        if (c == KEY_CTRL_C)
             break;
 
         if (isNewline(c)) {
             if (node == NULL || node->parent == NULL)
                 break;
 
             break;
 
         if (isNewline(c)) {
             if (node == NULL || node->parent == NULL)
                 break;
 
-            editingNode = false;
-            g_CurrNode = node = node->parent;
+            if (node->parent->kind == JSONNodeKind_Obj) // editing obj key
+                g_CurrNode = node = node->firstChild;
+            else if (node->parent->kind == JSONNodeKind_Str) // editing obj value
+                g_CurrNode = node = node->parent->parent;
+            else
+                g_CurrNode = node = node->parent;
+            
+            editingNode = JSONNodeEditable(node);
             
             continue;
         }
 
             
             continue;
         }
 
-        if (! editingNode) {
+        if (JSONNodeEditable(node) && editingNode) {
+            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 {
             JSONNode * old = node;
 
             /**/ 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); }
             else if (c == 'a') { node = JSONNodeNew(JSONNodeKind_Arr); }
             JSONNode * old = node;
 
             /**/ 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); }
             else if (c == 'a') { node = JSONNodeNew(JSONNodeKind_Arr); }
-            else if (isBackspace(c) && node != NULL) {
-                g_CurrNode = node = JSONNodeRemove(node);
-                continue;
-            }
             else if (c == 'h') { if (node->prev != NULL) g_CurrNode = node = node->prev; continue; }
             else if (c == 'l') { if (node->next != NULL) g_CurrNode = node = node->next; continue; }
             else if (c == 'k') { if (node->parent != NULL) g_CurrNode = node = node->parent; continue; }
             else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; continue; }
             else if (c == 'h') { if (node->prev != NULL) g_CurrNode = node = node->prev; continue; }
             else if (c == 'l') { if (node->next != NULL) g_CurrNode = node = node->next; continue; }
             else if (c == 'k') { if (node->parent != NULL) g_CurrNode = node = node->parent; continue; }
             else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; continue; }
+            else if (c == ' ') { if (JSONNodeEditable(node)) editingNode = true; continue; }
+            else if (isBackspace(c) && node != NULL) { g_CurrNode = node = JSONNodeRemove(node); continue; }
             else { continue; }
 
             else { continue; }
 
-            g_CurrNode = node;
-
-            if (old != NULL)
+            if (old != NULL) {
+                // new node was added to an object
+                if (old->kind == JSONNodeKind_Obj) {
+                    JSONNode * keyNode = JSONNodeNew(JSONNodeKind_Str);
+                    JSONNodePush(keyNode, node);
+                    node = keyNode;
+                    editingNode = true;
+                }
                 JSONNodePush(old, node);
                 JSONNodePush(old, node);
-            else
+            }
+            else {
                 g_DrawNode = result = node;
                 g_DrawNode = result = node;
-        }
-        else {
-            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++;
-            }
+            g_CurrNode = node;
         }
     }
 
         }
     }