Try to get the number of rows in the current terminal, or assume 24 * if it fails. */
| 977 | /* Try to get the number of rows in the current terminal, or assume 24 |
| 978 | * if it fails. */ |
| 979 | int getRows(int ifd, int ofd) { |
| 980 | #ifdef _WIN32 |
| 981 | CONSOLE_SCREEN_BUFFER_INFO b; |
| 982 | |
| 983 | if (!GetConsoleScreenBufferInfo(hOut, &b)) |
| 984 | return 24; |
| 985 | return b.srWindow.Bottom - b.srWindow.Top; |
| 986 | #else |
| 987 | struct winsize ws; |
| 988 | |
| 989 | if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_row == 0) { |
| 990 | /* ioctl() failed. Try to query the terminal itself. */ |
| 991 | int start, rows; |
| 992 | |
| 993 | /* Get the initial position so we can restore it later. */ |
| 994 | start = getCursorPosition(ifd, ofd); |
| 995 | if (start == -1) |
| 996 | goto failed; |
| 997 | |
| 998 | /* Go to bottom margin and get position. */ |
| 999 | if (write(ofd, "\x1b[999B", 6) != 6) |
| 1000 | goto failed; |
| 1001 | rows = getCursorPosition(ifd, ofd); |
| 1002 | if (rows == -1) |
| 1003 | goto failed; |
| 1004 | |
| 1005 | /* Restore position. */ |
| 1006 | if (rows > start) { |
| 1007 | char seq[32]; |
| 1008 | snprintf(seq, 32, "\x1b[%dA", rows - start); |
| 1009 | if (write(ofd, seq, strlen(seq)) == -1) { |
| 1010 | /* Can't recover... */ |
| 1011 | } |
| 1012 | } |
| 1013 | return rows; |
| 1014 | } else { |
| 1015 | return ws.ws_row; |
| 1016 | } |
| 1017 | |
| 1018 | failed: |
| 1019 | return 24; |
| 1020 | #endif |
| 1021 | } |
| 1022 | |
| 1023 | /* Clear the screen. Used to handle ctrl+l */ |
| 1024 | void linenoiseClearScreen(void) { |
no test coverage detected