Added check for tty output

This commit is contained in:
Bob Polis
2023-08-07 16:48:33 +02:00
parent 826cd8c92b
commit b80f093c87
4 changed files with 79 additions and 34 deletions

View File

@ -11,7 +11,8 @@
using namespace sc;
term::term(int fd) {
if (isatty(fd)) {
_isatty = isatty(fd);
if (_isatty) {
throw_if_min1(ioctl(fd, TIOCGWINSZ, &_ws));
}
switch (fd) {
@ -22,11 +23,11 @@ term::term(int fd) {
}
int term::rows() const {
return _ws.ws_row;
return _isatty ? _ws.ws_row : 0;
}
int term::cols() const {
return _ws.ws_col;
return _isatty ? _ws.ws_col : 0;
}
void term::progress(int prefixlen,
@ -34,6 +35,8 @@ void term::progress(int prefixlen,
double cur,
double total) const
{
if (!_isatty) return;
// use unicode to make nice bar
const char* bar[8] = {
u8"\u258F", u8"\u258E", u8"\u258D", u8"\u258C",