1
1
#include < cmath>
2
2
#include < cstdint>
3
+ #include < filesystem>
3
4
#include < iostream>
4
5
#include < ostream>
5
6
#include < string_view>
@@ -12,12 +13,20 @@ namespace scip_clang {
12
13
13
14
ProgressReporter::ProgressReporter (bool active, std::string_view msg,
14
15
size_t totalCount)
15
- : message(msg), totalCount(totalCount), countWidth(), active(active) {
16
+ : message(msg), totalCount(totalCount), countWidth(), active(active),
17
+ isTty (false ) {
16
18
if (this ->totalCount == 0 ) {
17
19
countWidth = 1 ;
18
20
} else {
19
21
countWidth = std::log10 (double (this ->totalCount )) + 1 ;
20
22
}
23
+ #if __linux__ || __APPLE__
24
+ std::error_code ec;
25
+ auto status = std::filesystem::status (" /dev/stdout" , ec);
26
+ if (!ec) {
27
+ this ->isTty = (status.type () == std::filesystem::file_type::character);
28
+ }
29
+ #endif
21
30
}
22
31
23
32
void ProgressReporter::report (size_t count, std::string_view extraData) const {
@@ -26,14 +35,19 @@ void ProgressReporter::report(size_t count, std::string_view extraData) const {
26
35
}
27
36
int maxExtraWidth = 256 ;
28
37
auto backspaceCount = std::max (maxExtraWidth - int (extraData.size ()), 0 );
29
- fmt::print (" \r [{1:>{0}}/{2:>{0}}] {3} {4:<{5}}{6:\b <{7}}" , countWidth, count,
30
- this ->totalCount , this ->message , extraData, maxExtraWidth, " " ,
31
- backspaceCount);
38
+ if (this ->isTty ) {
39
+ fmt::print (" \r [{1:>{0}}/{2:>{0}}] {3} {4:<{5}}{6:\b <{7}}" , countWidth,
40
+ count, this ->totalCount , this ->message , extraData, maxExtraWidth,
41
+ " " , backspaceCount);
42
+ } else {
43
+ fmt::print (" [{1:>{0}}/{2:>{0}}] {3} {4}\n " , countWidth, count,
44
+ this ->totalCount , this ->message , extraData);
45
+ }
32
46
std::flush (std::cout);
33
47
}
34
48
35
49
ProgressReporter::~ProgressReporter () {
36
- if (this ->active ) {
50
+ if (this ->active && this -> isTty ) {
37
51
fmt::print (" \n " );
38
52
}
39
53
}
0 commit comments