Skip to content

Commit 06d35d0

Browse files
alimpfardawesomekling
authored andcommitted
Userland/js: Use the new line editor in repl
We now get cursor movements for free! and we're rid of that icky `free` call, yay.
1 parent 6f407ff commit 06d35d0

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

Userland/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ APPS = ${SRCS:.cpp=}
44

55
EXTRA_CLEAN = $(APPS)
66

7-
LIB_DEPS = Web GUI Gfx Audio Protocol IPC Thread Pthread PCIDB Markdown JS Core
7+
LIB_DEPS = Web GUI Gfx Audio Protocol IPC Thread Pthread PCIDB Markdown JS Core LineEdit
88

99
include ../Makefile.common
1010

Userland/js.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,25 @@
3737
#include <LibJS/Runtime/Object.h>
3838
#include <LibJS/Runtime/PrimitiveString.h>
3939
#include <LibJS/Runtime/Value.h>
40+
#include <LibLineEdit/LineEditor.h>
4041
#include <stdio.h>
4142

4243
bool dump_ast = false;
44+
static LineEditor editor {};
4345

4446
String read_next_piece()
4547
{
4648
StringBuilder piece;
4749
int level = 0;
50+
StringBuilder prompt_builder;
4851

4952
do {
50-
if (level == 0)
51-
fprintf(stderr, "> ");
52-
else
53-
fprintf(stderr, ".%*c", 4 * level + 1, ' ');
54-
55-
char* line = nullptr;
56-
size_t allocated_size = 0;
57-
ssize_t nread = getline(&line, &allocated_size, stdin);
58-
if (nread < 0) {
59-
if (errno == 0) {
60-
// Explicit EOF; stop reading. Print a newline though, to make
61-
// the next prompt (or the shell prompt) appear on the next
62-
// line.
63-
fprintf(stderr, "\n");
64-
break;
65-
} else {
66-
perror("getline");
67-
exit(1);
68-
}
69-
}
53+
prompt_builder.clear();
54+
prompt_builder.append("> ");
55+
for (auto i = 0; i < level; ++i)
56+
prompt_builder.append(" ");
57+
58+
String line = editor.get_line(prompt_builder.build());
7059

7160
piece.append(line);
7261
auto lexer = JS::Lexer(line);
@@ -87,8 +76,6 @@ String read_next_piece()
8776
break;
8877
}
8978
}
90-
91-
free(line);
9279
} while (level > 0);
9380

9481
return piece.to_string();
@@ -207,6 +194,7 @@ int main(int argc, char** argv)
207194
interpreter.global_object().put("global", &interpreter.global_object());
208195

209196
if (script_path == nullptr) {
197+
editor.initialize();
210198
repl(interpreter);
211199
} else {
212200
auto file = Core::File::construct(script_path);

0 commit comments

Comments
 (0)