From 4590204701232f3bee50daf69a0d3e62b026c648 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 26 Dec 2021 15:57:13 -0500 Subject: goldfishterm: Don’t hard-code std::cout/STDOUT_FILENO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make terminal-handling code purer by requiring the user pass TERM, a termios struct, and an appropriate ostream rather than querying them from the environment. This eliminates some error-handling code and makes duplicate syscalls less likely. --- goldfishterm/simple.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'goldfishterm/simple.h') diff --git a/goldfishterm/simple.h b/goldfishterm/simple.h index 48a7789..0228749 100644 --- a/goldfishterm/simple.h +++ b/goldfishterm/simple.h @@ -17,7 +17,7 @@ #include -#include +#include #include #include "goldfishterm/internal/string_capability.h" @@ -33,15 +33,14 @@ enum class CursorVisibility { kVeryVisible, }; -// Looks up escape sequences for the terminal described in the TERM environment -// variable, and allows sending those escape sequences to standard output. +// Looks up escape sequences for the specified terminal and allows sending those +// escape sequences to the specified ostream. // -// This class is thread-safe, provided you don't mutate the TERM environment -// variable while its constructor is running. +// This class is thread-safe. class SimpleTerminalOutput final { public: - explicit SimpleTerminalOutput(); - explicit SimpleTerminalOutput(const termios&); + explicit SimpleTerminalOutput(absl::string_view terminal_name, const termios&, + std::ostream&); SimpleTerminalOutput(const SimpleTerminalOutput&) noexcept = default; SimpleTerminalOutput& operator=(const SimpleTerminalOutput&) noexcept = @@ -49,9 +48,9 @@ class SimpleTerminalOutput final { SimpleTerminalOutput(SimpleTerminalOutput&&) noexcept = default; SimpleTerminalOutput& operator=(SimpleTerminalOutput&&) noexcept = default; - void Write(absl::string_view s) { std::cout.write(s.data(), s.size()); } + void Write(absl::string_view s) { out_.write(s.data(), s.size()); } - void Flush() { std::cout.flush(); } + void Flush() { out_.flush(); } // Rings the bell. On some terminals, this may flash the screen instead. void Beep() { Emit(StringCapability::kBell); } @@ -74,6 +73,7 @@ class SimpleTerminalOutput final { TerminfoEntry terminfo_; int baud_; + std::ostream& out_; }; } // namespace goldfishterm -- cgit v1.2.3