chore: clean separation between is_tty() and is_fancy() for terminal

This commit is contained in:
Marcus Holland-Moritz 2024-01-19 12:39:31 +01:00
parent 6994f9691e
commit b9f8e61229
8 changed files with 35 additions and 16 deletions

View File

@ -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

View File

@ -103,7 +103,7 @@ std::string logger::all_level_names() {
stream_logger::stream_logger(std::shared_ptr<terminal const> 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)

View File

@ -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

View File

@ -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;
}
}

View File

@ -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";
}

View File

@ -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);

View File

@ -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 "<cr>"; }
@ -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)); }

View File

@ -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();