mirror of
https://github.com/pocketpy/pocketpy
synced 2026-08-05 21:57:11 +08:00
change to WINDOWS native API for tty-awareness
This commit is contained in:
parent
453a66158c
commit
4d74b66ffc
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
// importing io.h for tty detection in replinput
|
// importing io.h for tty detection in replinput
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
#include <io.h>
|
#include <windows.h>
|
||||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
@ -75,18 +75,23 @@ bool pk_wrapper__self(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
// macro for tty-sensitiveness
|
// macro for tty-sensitiveness
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
#define istty(fd) _isatty(_fileno(fd))
|
static inline int isatty_win(DWORD std_handle_id) {
|
||||||
|
HANDLE handle = GetStdHandle(std_handle_id); DWORD mode;
|
||||||
|
if (handle == INVALID_HANDLE_VALUE || handle == NULL) return 0;
|
||||||
|
return GetConsoleMode(handle, &mode) != 0;
|
||||||
|
}
|
||||||
|
#define istty isatty_win(STD_INPUT_HANDLE)
|
||||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||||
#define istty(fd) isatty(fileno(fd))
|
#define istty isatty(0)
|
||||||
#else
|
#else
|
||||||
#define istty(fd) 1
|
#define istty 1
|
||||||
#endif
|
#endif
|
||||||
int py_replinput(char* buf, int max_size) {
|
int py_replinput(char* buf, int max_size) {
|
||||||
buf[0] = '\0'; // reset first char because we check '@' at the beginning
|
buf[0] = '\0'; // reset first char because we check '@' at the beginning
|
||||||
|
|
||||||
int size = 0;
|
int size = 0;
|
||||||
bool multiline = false;
|
bool multiline = false;
|
||||||
if (istty(stdin)) printf(">>> ");
|
if (istty) printf(">>> ");
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
int c = pk_current_vm->callbacks.getchr();
|
int c = pk_current_vm->callbacks.getchr();
|
||||||
|
|||||||
16
src2/main.c
16
src2/main.c
@ -10,13 +10,17 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
#if defined(_WIN32) || defined(_WIN64)
|
||||||
#include <io.h>
|
static inline int isatty_win(DWORD std_handle_id) {
|
||||||
#define istty(fd) _isatty(_fileno(fd))
|
HANDLE handle = GetStdHandle(std_handle_id); DWORD mode;
|
||||||
|
if (handle == INVALID_HANDLE_VALUE || handle == NULL) return 0;
|
||||||
|
return GetConsoleMode(handle, &mode) != 0;
|
||||||
|
}
|
||||||
|
#define istty isatty_win(STD_INPUT_HANDLE)
|
||||||
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
#elif defined(__linux__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#define istty(fd) isatty(fileno(fd))
|
#define istty isatty(STDIN_FILENO)
|
||||||
#else
|
#else
|
||||||
#define istty(fd) 1
|
#define istty 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static char* readfile(const char* path, int* data_size) {
|
static char* readfile(const char* path, int* data_size) {
|
||||||
@ -96,7 +100,7 @@ int main(int argc, char** argv) {
|
|||||||
if(profile) printf("Warning: --profile is ignored in REPL mode.\n");
|
if(profile) printf("Warning: --profile is ignored in REPL mode.\n");
|
||||||
if(debug) printf("Warning: --debug is ignored in REPL mode.\n");
|
if(debug) printf("Warning: --debug is ignored in REPL mode.\n");
|
||||||
|
|
||||||
if (istty(stdin)) {
|
if (istty) {
|
||||||
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
|
||||||
printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
|
printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
@ -110,7 +114,7 @@ int main(int argc, char** argv) {
|
|||||||
while(true) {
|
while(true) {
|
||||||
int size = py_replinput(buf, sizeof(buf));
|
int size = py_replinput(buf, sizeof(buf));
|
||||||
if(size == -1) { // Ctrl-D (i.e. EOF)
|
if(size == -1) { // Ctrl-D (i.e. EOF)
|
||||||
if (istty(stdin)) printf("\n");
|
if (istty) printf("\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
assert(size < sizeof(buf));
|
assert(size < sizeof(buf));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user