1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 02:59:54 +00:00

Add color options to repl.

This commit is contained in:
bakpakin 2017-07-02 14:42:30 -04:00
parent db89a6c7ee
commit 7a9e991c41
3 changed files with 37 additions and 21 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# Target # Target
/client/gst /client/gst
gst
# Tags # Tags
tags tags

View File

@ -8,7 +8,7 @@ VERSION=\"0.0.0-beta\"
CFLAGS=-std=c99 -Wall -Wextra -I./include -g -DGST_VERSION=$(VERSION) CFLAGS=-std=c99 -Wall -Wextra -I./include -g -DGST_VERSION=$(VERSION)
PREFIX=/usr/local PREFIX=/usr/local
GST_TARGET=client/gst GST_TARGET=gst
GST_CORELIB=core/libgst.a GST_CORELIB=core/libgst.a
GST_INTERNAL_HEADERS=$(addprefix core/, cache.h) GST_INTERNAL_HEADERS=$(addprefix core/, cache.h)
GST_HEADERS=$(addprefix include/gst/, gst.h) GST_HEADERS=$(addprefix include/gst/, gst.h)
@ -39,7 +39,7 @@ run: $(GST_TARGET)
./$(GST_TARGET) ./$(GST_TARGET)
debug: $(GST_TARGET) debug: $(GST_TARGET)
gdb $(GST_TARGET) gdb ./$(GST_TARGET)
valgrind: $(GST_TARGET) valgrind: $(GST_TARGET)
valgrind --leak-check=full -v ./$(GST_TARGET) valgrind --leak-check=full -v ./$(GST_TARGET)
@ -52,7 +52,7 @@ clean:
rm vgcore.* || true rm vgcore.* || true
test: $(GST_TARGET) test: $(GST_TARGET)
$(GST_TARGET) gsttests/basic.gst ./$(GST_TARGET) gsttests/basic.gst
valtest: $(GST_TARGET) valtest: $(GST_TARGET)
valgrind --leak-check=full -v ./$(GST_TARGET) gsttests/basic.gst valgrind --leak-check=full -v ./$(GST_TARGET) gsttests/basic.gst

View File

@ -24,6 +24,19 @@
#include <stdio.h> #include <stdio.h>
#include <gst/gst.h> #include <gst/gst.h>
static int client_strequal(const char *a, const char *b) {
while (*a)
if (*a++ != *b++) return 0;
return *a == *b;
}
#define GST_CLIENT_HELP 1
#define GST_CLIENT_VERBOSE 2
#define GST_CLIENT_VERSION 4
#define GST_CLIENT_REPL 8
#define GST_CLIENT_NOCOLOR 16
#define GST_CLIENT_UNKNOWN 32
/* Simple read line functionality */ /* Simple read line functionality */
static char *gst_getline() { static char *gst_getline() {
char *line = malloc(100); char *line = malloc(100);
@ -123,7 +136,7 @@ static int debug_run(Gst *vm, FILE *in) {
} }
/* A simple repl */ /* A simple repl */
static int debug_repl(Gst *vm) { static int debug_repl(Gst *vm, uint64_t flags) {
char *buffer, *reader; char *buffer, *reader;
GstParser p; GstParser p;
buffer = reader = NULL; buffer = reader = NULL;
@ -135,7 +148,11 @@ static int debug_repl(Gst *vm) {
if (p.status == GST_PARSER_ERROR || p.status == GST_PARSER_FULL) if (p.status == GST_PARSER_ERROR || p.status == GST_PARSER_FULL)
break; break;
if (!reader || *reader == '\0') { if (!reader || *reader == '\0') {
printf("\x1B[32m>>\x1B[0m "); if (flags & GST_CLIENT_NOCOLOR) {
printf(">>> ");
} else {
printf("\x1B[33m>>>\x1B[0m ");
}
if (buffer) if (buffer)
free(buffer); free(buffer);
buffer = gst_getline(); buffer = gst_getline();
@ -157,23 +174,15 @@ static int debug_repl(Gst *vm) {
continue; continue;
} }
if (!debug_compile_and_run(vm, gst_parse_consume(&p), vm->ret)) { if (!debug_compile_and_run(vm, gst_parse_consume(&p), vm->ret)) {
printf("%s\n", gst_description(vm, vm->ret)); if (flags & GST_CLIENT_NOCOLOR) {
printf("%s\n", gst_description(vm, vm->ret));
} else {
printf("\x1B[36m%s\x1B[0m\n", gst_description(vm, vm->ret));
}
} }
} }
} }
static int client_strequal(const char *a, const char *b) {
while (*a)
if (*a++ != *b++) return 0;
return *a == *b;
}
#define GST_CLIENT_HELP 1
#define GST_CLIENT_VERBOSE 2
#define GST_CLIENT_VERSION 4
#define GST_CLIENT_REPL 8
#define GST_CLIENT_UNKNOWN 16
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
Gst vm; Gst vm;
int status = -1; int status = -1;
@ -196,6 +205,8 @@ int main(int argc, const char **argv) {
flags |= GST_CLIENT_VERBOSE; flags |= GST_CLIENT_VERBOSE;
} else if (client_strequal(arg + 2, "repl")) { } else if (client_strequal(arg + 2, "repl")) {
flags |= GST_CLIENT_REPL; flags |= GST_CLIENT_REPL;
} else if (client_strequal(arg + 2, "nocolor")) {
flags |= GST_CLIENT_NOCOLOR;
} else { } else {
flags |= GST_CLIENT_UNKNOWN; flags |= GST_CLIENT_UNKNOWN;
} }
@ -207,15 +218,18 @@ int main(int argc, const char **argv) {
case 'h': case 'h':
flags |= GST_CLIENT_HELP; flags |= GST_CLIENT_HELP;
break; break;
case 'V': case 'v':
flags |= GST_CLIENT_VERSION; flags |= GST_CLIENT_VERSION;
break; break;
case 'v': case 'V':
flags |= GST_CLIENT_VERBOSE; flags |= GST_CLIENT_VERBOSE;
break; break;
case 'r': case 'r':
flags |= GST_CLIENT_REPL; flags |= GST_CLIENT_REPL;
break; break;
case 'c':
flags |= GST_CLIENT_NOCOLOR;
break;
default: default:
flags |= GST_CLIENT_UNKNOWN; flags |= GST_CLIENT_UNKNOWN;
break; break;
@ -233,6 +247,7 @@ int main(int argc, const char **argv) {
" -h --help : Shows this information.\n" " -h --help : Shows this information.\n"
" -V --verbose : Show more output.\n" " -V --verbose : Show more output.\n"
" -r --repl : Launch a repl after all files are processed.\n" " -r --repl : Launch a repl after all files are processed.\n"
" -c --nocolor : Don't use VT100 color codes in the repl.\n"
" -v --version : Print the version number and exit.\n\n", " -v --version : Print the version number and exit.\n\n",
argv[0]); argv[0]);
return 0; return 0;
@ -258,7 +273,7 @@ int main(int argc, const char **argv) {
} }
if (!fileRead || (flags & GST_CLIENT_REPL)) { if (!fileRead || (flags & GST_CLIENT_REPL)) {
status = debug_repl(&vm); status = debug_repl(&vm, flags);
} }
gst_deinit(&vm); gst_deinit(&vm);