1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-11 01:10:25 +00:00

Make getline more useful.

This commit is contained in:
Calvin Rose 2019-01-31 12:34:22 -05:00
parent cb3b9dd76f
commit 7ba925c50a
3 changed files with 14 additions and 11 deletions

View File

@ -1403,8 +1403,11 @@ value, one key will be ignored."
(defn getline (defn getline
"Read a line from stdin into a buffer." "Read a line from stdin into a buffer."
[buf p] [buf p &]
(file/read stdin :line buf)) (default buf @"")
(when p (file/write stdout p))
(file/read stdin :line buf)
buf)
(defn run-context (defn run-context
"Run a context. This evaluates expressions of janet in an environment, "Run a context. This evaluates expressions of janet in an environment,

View File

@ -24,11 +24,11 @@
/* Common */ /* Common */
Janet janet_line_getter(int32_t argc, Janet *argv) { Janet janet_line_getter(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2); janet_arity(argc, 0, 2);
const uint8_t *str = janet_getstring(argv, 0); const char *str = (argc >= 1) ? (const char *) janet_getstring(argv, 0) : "";
JanetBuffer *buf = janet_getbuffer(argv, 1); JanetBuffer *buf = (argc >= 2) ? janet_getbuffer(argv, 1) : janet_buffer(10);
janet_line_get(str, buf); janet_line_get(str, buf);
return argv[0]; return janet_wrap_buffer(buf);
} }
static void simpleline(JanetBuffer *buffer) { static void simpleline(JanetBuffer *buffer) {
@ -55,8 +55,8 @@ void janet_line_deinit() {
; ;
} }
void janet_line_get(const uint8_t *p, JanetBuffer *buffer) { void janet_line_get(const char *p, JanetBuffer *buffer) {
fputs((const char *)p, stdout); fputs(p, stdout);
simpleline(buffer); simpleline(buffer);
} }
@ -444,8 +444,8 @@ static int checktermsupport() {
return 1; return 1;
} }
void janet_line_get(const uint8_t *p, JanetBuffer *buffer) { void janet_line_get(const char *p, JanetBuffer *buffer) {
prompt = (const char *)p; prompt = p;
buffer->count = 0; buffer->count = 0;
historyi = 0; historyi = 0;
if (!isatty(STDIN_FILENO) || !checktermsupport()) { if (!isatty(STDIN_FILENO) || !checktermsupport()) {

View File

@ -28,7 +28,7 @@
void janet_line_init(); void janet_line_init();
void janet_line_deinit(); void janet_line_deinit();
void janet_line_get(const uint8_t *p, JanetBuffer *buffer); void janet_line_get(const char *p, JanetBuffer *buffer);
Janet janet_line_getter(int32_t argc, Janet *argv); Janet janet_line_getter(int32_t argc, Janet *argv);
#endif #endif