File tree Expand file tree Collapse file tree 3 files changed +51
-2
lines changed Expand file tree Collapse file tree 3 files changed +51
-2
lines changed Original file line number Diff line number Diff line change 9
9
10
10
#include " any_iostream.hpp"
11
11
12
+ #ifdef _MSC_VER
13
+ #include < io.h>
14
+ #else
15
+ #include < unistd.h>
16
+ #endif
17
+
18
+ namespace
19
+ {
20
+ bool
21
+ is_stdout_tty ()
22
+ {
23
+ #ifdef _MSC_VER
24
+ return _isatty (_fileno (stdout));
25
+ #else
26
+ return isatty (fileno (stdout));
27
+ #endif
28
+ }
29
+ } // namespace
30
+
31
+ any_ostream::any_ostream ()
32
+ : stream_{ &std::cout }
33
+ , is_tty_{ ::is_stdout_tty () }
34
+ {
35
+ }
36
+
12
37
any_ostream::any_ostream (core::string_view path)
13
38
{
14
39
if (path == " -" )
@@ -29,6 +54,12 @@ any_ostream::any_ostream(core::string_view path)
29
54
}
30
55
}
31
56
57
+ bool
58
+ any_ostream::is_tty () const noexcept
59
+ {
60
+ return is_tty_;
61
+ }
62
+
32
63
any_ostream::
33
64
operator std::ostream&()
34
65
{
Original file line number Diff line number Diff line change @@ -21,10 +21,16 @@ namespace core = boost::core;
21
21
class any_ostream
22
22
{
23
23
std::variant<std::ofstream, std::ostream*> stream_;
24
+ bool is_tty_ = false ;
24
25
25
26
public:
27
+ any_ostream ();
28
+
26
29
any_ostream (core::string_view path);
27
30
31
+ bool
32
+ is_tty () const noexcept ;
33
+
28
34
operator std::ostream&();
29
35
30
36
template <typename T>
Original file line number Diff line number Diff line change @@ -504,8 +504,20 @@ request(
504
504
{
505
505
for (auto cb : parser.pull_body ())
506
506
{
507
- body_output << core::string_view{
507
+ auto chunk = core::string_view{
508
508
static_cast <const char *>(cb.data ()), cb.size () };
509
+
510
+ if (body_output.is_tty () &&
511
+ chunk.find (char {0 }) != core::string_view::npos)
512
+ {
513
+ std::cerr <<
514
+ " Warning: Binary output can mess up your terminal.\n "
515
+ " Warning: Use \" --output -\" to tell burl to output it to your terminal anyway, or\n "
516
+ " Warning: consider \" --output <FILE>\" to save to a file.\n " ;
517
+ co_return ;
518
+ }
519
+
520
+ body_output << chunk;
509
521
parser.consume_body (cb.size ());
510
522
}
511
523
@@ -750,7 +762,7 @@ main(int argc, char* argv[])
750
762
{
751
763
if (vm.count (" output" ))
752
764
return any_ostream{ vm.at (" output" ).as <std::string>() };
753
- return any_ostream{ " - " };
765
+ return any_ostream{};
754
766
}();
755
767
756
768
auto header_output = [&]() -> std::optional<any_ostream>
You can’t perform that action at this time.
0 commit comments