diff options
author | Benjamin Barenblat <bbarenblat@gmail.com> | 2021-12-26 15:57:13 -0500 |
---|---|---|
committer | Benjamin Barenblat <bbarenblat@gmail.com> | 2021-12-26 15:57:13 -0500 |
commit | 4590204701232f3bee50daf69a0d3e62b026c648 (patch) | |
tree | 5d7ad9da3f97f51b37b6331b8c57d02030c21280 /goldfishterm/simple.h | |
parent | 56f7d79efdc8ad26f5e8e5508eb616144f04fea3 (diff) |
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.
Diffstat (limited to 'goldfishterm/simple.h')
-rw-r--r-- | goldfishterm/simple.h | 18 |
1 files changed, 9 insertions, 9 deletions
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 <termios.h> -#include <iostream> +#include <ostream> #include <vector> #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 |