mirror of
https://github.com/pocketpy/pocketpy
synced 2026-08-05 21:57:11 +08:00
make py_replinput tty sensitive
This commit is contained in:
parent
21ff0751dc
commit
453a66158c
@ -1,6 +1,13 @@
|
||||
#include "pocketpy/interpreter/vm.h"
|
||||
#include <assert.h>
|
||||
|
||||
// importing io.h for tty detection in replinput
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <io.h>
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void pk_print_stack(VM* self, py_Frame* frame, Bytecode byte) {
|
||||
return;
|
||||
if(frame == NULL || !self->main || py_isnil(self->main)) return;
|
||||
@ -66,12 +73,20 @@ bool pk_wrapper__self(int argc, py_Ref argv) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// macro for tty-sensitiveness
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#define istty(fd) _isatty(_fileno(fd))
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||
#define istty(fd) isatty(fileno(fd))
|
||||
#else
|
||||
#define istty(fd) 1
|
||||
#endif
|
||||
int py_replinput(char* buf, int max_size) {
|
||||
buf[0] = '\0'; // reset first char because we check '@' at the beginning
|
||||
|
||||
int size = 0;
|
||||
bool multiline = false;
|
||||
printf(">>> ");
|
||||
if (istty(stdin)) printf(">>> ");
|
||||
|
||||
while(true) {
|
||||
int c = pk_current_vm->callbacks.getchr();
|
||||
@ -107,6 +122,7 @@ int py_replinput(char* buf, int max_size) {
|
||||
buf[size] = '\0';
|
||||
return size;
|
||||
}
|
||||
#undef istty
|
||||
|
||||
py_Ref py_name2ref(py_Name name) {
|
||||
assert(name != NULL);
|
||||
|
||||
27
src2/main.c
27
src2/main.c
@ -9,6 +9,16 @@
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <io.h>
|
||||
#define istty(fd) _isatty(_fileno(fd))
|
||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||
#include <unistd.h>
|
||||
#define istty(fd) isatty(fileno(fd))
|
||||
#else
|
||||
#define istty(fd) 1
|
||||
#endif
|
||||
|
||||
static char* readfile(const char* path, int* data_size) {
|
||||
FILE* f = fopen(path, "rb");
|
||||
if(f == NULL) return NULL;
|
||||
@ -86,19 +96,21 @@ int main(int argc, char** argv) {
|
||||
if(profile) printf("Warning: --profile is ignored in REPL mode.\n");
|
||||
if(debug) printf("Warning: --debug is ignored in REPL mode.\n");
|
||||
|
||||
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
||||
printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
|
||||
if (istty(stdin)) {
|
||||
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
||||
printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
|
||||
#ifndef NDEBUG
|
||||
printf(" (DEBUG)");
|
||||
printf(" (DEBUG)");
|
||||
#endif
|
||||
printf("\n");
|
||||
printf("https://github.com/pocketpy/pocketpy\n");
|
||||
printf("Type \"exit()\" to exit.\n");
|
||||
printf("\n");
|
||||
printf("https://github.com/pocketpy/pocketpy\n");
|
||||
printf("Type \"exit()\" to exit.\n");
|
||||
}
|
||||
|
||||
while(true) {
|
||||
int size = py_replinput(buf, sizeof(buf));
|
||||
if(size == -1) { // Ctrl-D (i.e. EOF)
|
||||
printf("\n");
|
||||
if (istty(stdin)) printf("\n");
|
||||
break;
|
||||
}
|
||||
assert(size < sizeof(buf));
|
||||
@ -160,3 +172,4 @@ int main(int argc, char** argv) {
|
||||
if(debug) py_debugger_exit(code);
|
||||
return code;
|
||||
}
|
||||
#undef istty
|
||||
Loading…
x
Reference in New Issue
Block a user