diff --git a/include/dwarfs/terminal.h b/include/dwarfs/terminal.h index d30c9c6f..f2319c25 100644 --- a/include/dwarfs/terminal.h +++ b/include/dwarfs/terminal.h @@ -67,7 +67,8 @@ class terminal { static void setup(); virtual size_t width() const = 0; - virtual bool is_fancy(std::ostream& os) const = 0; + virtual bool is_tty(std::ostream& os) const = 0; + virtual bool is_fancy() const = 0; virtual std::string_view color(termcolor color, termstyle style = termstyle::NORMAL) const = 0; virtual std::string diff --git a/src/dwarfs/logger.cpp b/src/dwarfs/logger.cpp index 3ef24a4d..67310b58 100644 --- a/src/dwarfs/logger.cpp +++ b/src/dwarfs/logger.cpp @@ -103,7 +103,7 @@ std::string logger::all_level_names() { stream_logger::stream_logger(std::shared_ptr term, std::ostream& os, logger_options const& logopts) : os_(os) - , color_(term->is_fancy(os)) + , color_(term->is_tty(os) && term->is_fancy()) , enable_stack_trace_{getenv_is_enabled("DWARFS_LOGGER_STACK_TRACE")} , with_context_(logopts.with_context ? logopts.with_context.value() : logopts.threshold >= logger::VERBOSE) diff --git a/src/dwarfs/terminal.cpp b/src/dwarfs/terminal.cpp index 5f68b60c..a1648ede 100644 --- a/src/dwarfs/terminal.cpp +++ b/src/dwarfs/terminal.cpp @@ -167,7 +167,7 @@ class terminal_windows : public terminal_ansi { return csbi.srWindow.Right - csbi.srWindow.Left + 1; } - bool is_fancy(std::ostream& os) const override { + bool is_tty(std::ostream& os) const override { if (&os == &std::cout) { return ::_isatty(::_fileno(stdout)); } @@ -176,6 +176,8 @@ class terminal_windows : public terminal_ansi { } return false; } + + bool is_fancy() const override { return true; } }; #else @@ -188,11 +190,7 @@ class terminal_posix : public terminal_ansi { return w.ws_col; } - bool is_fancy(std::ostream& os) const override { - auto term = ::getenv("TERM"); - if (!term || term[0] == '\0' || std::string_view(term) == "dumb") { - return false; - } + bool is_tty(std::ostream& os) const override { if (&os == &std::cout) { return ::isatty(::fileno(stdout)); } @@ -201,6 +199,15 @@ class terminal_posix : public terminal_ansi { } return false; } + + bool is_fancy() const override { + // TODO: we might want to use the iolayer here + if (auto term = ::getenv("TERM")) { + std::string_view term_sv(term); + return !term_sv.empty() && term_sv != "dumb"; + } + return false; + } }; #endif diff --git a/src/dwarfs/tool.cpp b/src/dwarfs/tool.cpp index cb1546bd..a8a45a96 100644 --- a/src/dwarfs/tool.cpp +++ b/src/dwarfs/tool.cpp @@ -90,9 +90,10 @@ void add_common_options(po::options_description& opts, #ifdef DWARFS_BUILTIN_MANPAGE void show_manpage(manpage::document doc, iolayer const& iol) { - auto const fancy = iol.term->is_fancy(iol.out); - auto content = render_manpage(doc, iol.term->width(), fancy); - if (!show_in_pager(content)) { + bool is_tty = iol.term->is_tty(iol.out); + auto content = + render_manpage(doc, iol.term->width(), is_tty && iol.term->is_fancy()); + if (!is_tty || !show_in_pager(content)) { iol.out << content; } } diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index f316a490..bd7955e6 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -896,7 +896,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { if (no_progress) { progress_mode = "none"; } - if (progress_mode != "none" && !iol.term->is_fancy(iol.err)) { + if (progress_mode != "none" && !iol.term->is_tty(iol.err)) { progress_mode = "simple"; } diff --git a/test/test_helpers.h b/test/test_helpers.h index 9fb40602..ed93b845 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -165,10 +165,12 @@ class test_terminal : public terminal { test_terminal(std::ostream& out, std::ostream& err); void set_fancy(bool fancy) { fancy_ = fancy; } + void set_is_tty(bool is_tty) { is_tty_ = is_tty; } void set_width(size_t width) { width_ = width; } size_t width() const override; - bool is_fancy(std::ostream& os) const override; + bool is_tty(std::ostream& os) const override; + bool is_fancy() const override; std::string_view color(termcolor color, termstyle style) const override; std::string colored(std::string text, termcolor color, bool enable, termstyle style) const override; @@ -180,6 +182,7 @@ class test_terminal : public terminal { std::ostream* out_; std::ostream* err_; bool fancy_{false}; + bool is_tty_{false}; size_t width_{80}; }; @@ -244,6 +247,7 @@ class test_iolayer { void use_real_terminal(bool use); void set_in(std::string in); + void set_terminal_is_tty(bool is_tty); void set_terminal_fancy(bool fancy); void set_terminal_width(size_t width); diff --git a/test/test_iolayer.cpp b/test/test_iolayer.cpp index 98e68c57..eef47731 100644 --- a/test/test_iolayer.cpp +++ b/test/test_iolayer.cpp @@ -216,9 +216,9 @@ test_terminal::test_terminal(std::ostream& out, std::ostream& err) size_t test_terminal::width() const { return width_; } -bool test_terminal::is_fancy(std::ostream& os) const { - return fancy_ && (&os == out_ || &os == err_); -} +bool test_terminal::is_tty(std::ostream& /*os*/) const { return is_tty_; } + +bool test_terminal::is_fancy() const { return fancy_; } std::string_view test_terminal::carriage_return() const { return ""; } @@ -344,6 +344,9 @@ void test_iolayer::use_real_terminal(bool use) { real_term_ = terminal::create(); } +void test_iolayer::set_terminal_is_tty(bool is_tty) { + term_->set_is_tty(is_tty); +} void test_iolayer::set_terminal_fancy(bool fancy) { term_->set_fancy(fancy); } void test_iolayer::set_terminal_width(size_t width) { term_->set_width(width); } void test_iolayer::set_in(std::string in) { in_.str(std::move(in)); } diff --git a/test/tool_main_test.cpp b/test/tool_main_test.cpp index 4f5d4a44..ae03030a 100644 --- a/test/tool_main_test.cpp +++ b/test/tool_main_test.cpp @@ -692,6 +692,7 @@ TEST_P(term_logging_test, end_to_end) { { mkdwarfs_tester t; + t.iol->set_terminal_is_tty(fancy); t.iol->set_terminal_fancy(fancy); t.os->set_access_fail("/somedir/ipsum.py"); // trigger an error EXPECT_EQ(2, t.run("-l1 -i / -o - --categorize --num-workers=8 -S 22 " @@ -1419,6 +1420,7 @@ TEST(mkdwarfs_test, invalid_filter_debug_mode) { TEST(mkdwarfs_test, invalid_progress_mode) { mkdwarfs_tester t; + t.iol->set_terminal_is_tty(true); t.iol->set_terminal_fancy(true); EXPECT_NE(0, t.run({"-i", "/", "-o", "-", "--progress=grmpf"})); EXPECT_THAT(t.err(), ::testing::HasSubstr("invalid progress mode")); @@ -1689,6 +1691,7 @@ TEST_P(mkdwarfs_progress_test, basic) { auto t = mkdwarfs_tester::create_empty(); + t.iol->set_terminal_is_tty(true); t.iol->set_terminal_fancy(true); t.add_root_dir();