💄 Adds basic formatting
This commit is contained in:
parent
74e99cf19a
commit
07d45752cc
62
main.c
62
main.c
@ -2,21 +2,71 @@
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
const int indent = 2;
|
||||
int scope = 0;
|
||||
|
||||
void print_indent() {
|
||||
for (int i = 0; i < indent * scope; i++) {
|
||||
putchar(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void ln() {
|
||||
putchar('\n');
|
||||
print_indent();
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int scope = 0;
|
||||
bool in_string = false;
|
||||
bool escape_next = false;
|
||||
|
||||
char c;
|
||||
while (read(STDIN_FILENO, &c, 1) > 0) {
|
||||
putchar(c);
|
||||
switch (c) {
|
||||
case '\"':
|
||||
if (!escape_next) {
|
||||
in_string = !in_string;
|
||||
}
|
||||
|
||||
if (c == '{' || c == '[') {
|
||||
putchar(c);
|
||||
break;
|
||||
case '\\':
|
||||
escape_next = true;
|
||||
|
||||
putchar(c);
|
||||
break;
|
||||
case '{':
|
||||
case '[':
|
||||
putchar(c);
|
||||
scope++;
|
||||
putchar('\n');
|
||||
}
|
||||
else if (c == '}' || c == ']') {
|
||||
|
||||
ln();
|
||||
break;
|
||||
case '}':
|
||||
case ']':
|
||||
scope--;
|
||||
ln();
|
||||
|
||||
putchar(c);
|
||||
break;
|
||||
case ',':
|
||||
putchar(c);
|
||||
if (!in_string) {
|
||||
ln();
|
||||
}
|
||||
|
||||
break;
|
||||
case ':':
|
||||
putchar(c);
|
||||
if (!in_string) {
|
||||
putchar(' ');
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
putchar(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user