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;
JSONNodeKind_Str,
JSONNodeKind_Obj,
JSONNodeKind_Arr,
+ JSONNodeKind_COUNT
} JSONNodeKind;
struct JSONNode;
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");
JSONNode * value = ptr->next;
Indent(indent);
printf("\"%s\": ", key);
- JSONNodePrint(value);
+ JSONNodePrint(value, currNode);
if (ptr->next != NULL)
ptr = ptr->next->next;
else
JSONNode * ptr = node->children;
while (ptr != NULL) {
JSONNode * value = ptr;
- JSONNodePrint(value);
+ JSONNodePrint(value, currNode);
ptr = ptr->next;
printf("%s", (ptr == NULL ? "" : ", "));
}
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
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--;
*i += c - '0';
}
}
- g_DrawStr = "";
}
void
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
int c = GetChar();
JSONNode * result = node;
+ g_CurrNode = node;
if (parent == NULL)
g_DrawNode = result;
JSONNode * newNode;
newNode = JSONNodeNewStr("");
+ g_CurrNode = newNode;
JSONNodePush(result, newNode);
GetStr(newNode);
JSONNodePush(result, newNode);
GetNode(result, newNode);
}
+ g_CurrNode = result;
GetChar();
break;
}
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;
}
vt100ClearScreen();
vt100CursorHome();
- JSONNodePrint(n);
+ JSONNodePrint(n, NULL);
printf("\n");
// JSONFree(n);