| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
#define _POSIX_SOURCE |
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
#if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \ |
| 12 |
defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__) |
| 13 |
#include <io.h> |
| 14 |
#define NOMINMAX |
| 15 |
#include <windows.h> |
| 16 |
#define isatty _isatty |
| 17 |
#else |
| 18 |
#include <sys/ioctl.h> |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
int term_width(void) |
| 24 |
{ |
| 25 |
int cols = 80; |
| 26 |
if (isatty(fileno(stderr))) { |
| 27 |
#ifdef TIOCGSIZE |
| 28 |
struct ttysize ts; |
| 29 |
ioctl(STDIN_FILENO, TIOCGSIZE, &ts); |
| 30 |
cols = ts.ts_cols; |
| 31 |
#elif defined(_MSC_VER) |
| 32 |
CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 33 |
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); |
| 34 |
cols = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 35 |
#elif defined(TIOCGWINSZ) |
| 36 |
struct winsize ts; |
| 37 |
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts); |
| 38 |
cols = ts.ws_col; |
| 39 |
#endif /* TIOCGSIZE */ |
| 40 |
} |
| 41 |
return cols; |
| 42 |
} |