diff options
Diffstat (limited to 'goldfishterm/simple.h')
-rw-r--r-- | goldfishterm/simple.h | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/goldfishterm/simple.h b/goldfishterm/simple.h new file mode 100644 index 0000000..3f5cc9f --- /dev/null +++ b/goldfishterm/simple.h @@ -0,0 +1,71 @@ +// Copyright 2021 Benjamin Barenblat +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +#ifndef EC_GOLDFISHTERM_SIMPLE_H_ +#define EC_GOLDFISHTERM_SIMPLE_H_ + +#include <termios.h> + +#include <iostream> +#include <vector> + +#include "goldfishterm/internal/string_capability.h" +#include "goldfishterm/terminfo.h" +#include "third_party/abseil/absl/strings/string_view.h" + +namespace goldfishterm { + +// Looks up escape sequences for the terminal described in the TERM environment +// variable, and allows sending those escape sequences to standard output. +// +// This class is thread-safe, provided you don't mutate the TERM environment +// variable while its constructor is running. +class SimpleTerminalOutput final { + public: + explicit SimpleTerminalOutput(); + explicit SimpleTerminalOutput(const termios&); + + SimpleTerminalOutput(const SimpleTerminalOutput&) noexcept = default; + SimpleTerminalOutput& operator=(const SimpleTerminalOutput&) noexcept = + default; + SimpleTerminalOutput(SimpleTerminalOutput&&) noexcept = default; + SimpleTerminalOutput& operator=(SimpleTerminalOutput&&) noexcept = default; + + void Write(absl::string_view s) { std::cout.write(s.data(), s.size()); } + + void Flush() { std::cout.flush(); } + + // Rings the bell. On some terminals, this may flash the screen instead. + void Beep() { Emit(StringCapability::kBell); } + + // Moves the cursor to the beginning of the current line. + void BeginningOfLine() { Emit(StringCapability::kCarriageReturn); } + + // Clears from the cursor to the end of the current line. + void ClearToEndOfLine() { Emit(StringCapability::kClrEol); } + + // Moves the cursor down one line. + void CursorDown() { Emit(StringCapability::kCursorDown); } + + private: + void Emit(StringCapability, + std::vector<goldfishterm_internal::StringCapabilityParameter> = {}); + + TerminfoEntry terminfo_; + int baud_; +}; + +} // namespace goldfishterm + +#endif // EC_GOLDFISHTERM_SIMPLE_H_ |