int linenoiseColumns(void) { struct current current; enableRawMode(¤t); getWindowSize(¤t); disableRawMode(¤t); return current.cols; }
int linenoiseLines(void) { struct current current; enableRawMode (¤t); getWindowSize (¤t); disableRawMode (¤t); return current.rows; }
/* At exit we'll try to fix the terminal to the initial conditions. */ static void linenoiseAtExit(void) { #ifdef _WIN32 SetConsoleMode(hIn, consolemode); CloseHandle(hOut); CloseHandle(hIn); #else disableRawMode(STDIN_FILENO); #endif freeHistory(); }
static int linenoiseRaw(char* buf, size_t buflen, const char* prompt) { int count = -1; if (buflen != 0) { if (enableRawMode() == -1) return -1; count = linenoisePrompt(buf, buflen, prompt); disableRawMode(); printf("\n"); } return count; }
char *linenoise(const char *prompt) { shouldTerminate = 0; if(requestedInteractiveTermination()){ return NULL; } int count; struct current current; char buf[LINENOISE_MAX_LINE]; if (enableRawMode(¤t) == -1) { printf("%s", prompt); fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) { return NULL; } count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { current.buf = buf; current.bufmax = sizeof(buf); current.len = 0; current.chars = 0; current.pos = 0; current.prompt = prompt; count = linenoisePrompt(¤t); disableRawMode(¤t); printf("\n"); if (count == -1) { return NULL; } } return my_strdup(buf); }
char *linenoise(const char *prompt) { int count; struct current current; char buf[LINENOISE_MAX_LINE]; if (enableRawMode(¤t) == -1) { printf("%s", prompt); fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) { return NULL; } count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { current.buf = buf; current.bufmax = sizeof(buf); current.len = 0; current.chars = 0; current.pos = 0; current.prompt = prompt; current.capture = NULL; count = linenoiseEdit(¤t); disableRawMode(¤t); printf("\n"); free(current.capture); if (count == -1) { return NULL; } } return strdup(buf); }
static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) { int fd = STDIN_FILENO; int count; if (buflen == 0) { errno = EINVAL; return -1; } if (!isatty(STDIN_FILENO)) { if (fgets(buf, buflen, stdin) == NULL) return -1; count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { if (enableRawMode(fd) == -1) return -1; count = linenoisePrompt(fd, buf, buflen, prompt); disableRawMode(fd); printf("\n"); } return count; }
/* This function calls the line editing function linenoiseEdit() using * the STDIN file descriptor set in raw mode. */ static int linenoiseRaw(char *buf, size_t buflen, char *prompt) { int count; if (buflen == 0) { errno = EINVAL; return -1; } if (!isatty(STDIN_FILENO)) { /* Not a tty: read from file / pipe. */ if (fgets(buf, buflen, stdin) == NULL) return -1; count = strlen(buf); if (count && buf[count-1] == '\n') { count--; buf[count] = '\0'; } } else { /* Interactive editing. */ if (enableRawMode(STDIN_FILENO) == -1) return -1; count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt); disableRawMode(STDIN_FILENO); printf("\n"); } return count; }
/* This special mode is used by linenoise in order to print scan codes * on screen for debugging / development purposes. It is implemented * by the linenoise_example program using the --keycodes option. */ void linenoisePrintKeyCodes(void) { char quit[4]; printf("Linenoise key codes debugging mode.\n" "Press keys to see scan codes. Type 'quit' at any time to exit.\n"); if (enableRawMode(STDIN_FILENO) == -1) return; memset(quit,' ',4); while(1) { char c; int nread; nread = read(STDIN_FILENO,&c,1); if (nread <= 0) continue; memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */ quit[sizeof(quit)-1] = c; /* Insert current char on the right. */ if (memcmp(quit,"quit",sizeof(quit)) == 0) break; printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c, (int)c); printf("\r"); /* Go left edge manually, we are in raw mode. */ fflush(stdout); } disableRawMode(STDIN_FILENO); }
/* At exit we'll try to fix the terminal to the initial conditions. */ static void linenoiseAtExit(void) { disableRawMode(STDIN_FILENO); freeHistory(); }
/* Called at exit to avoid remaining in raw mode. */ void editorAtExit(void) { disableRawMode(STDIN_FILENO); }