aboutsummaryrefslogtreecommitdiff
path: root/goldfishterm/simple.h
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbarenblat@gmail.com>2021-12-26 13:53:22 -0500
committerGravatar Benjamin Barenblat <bbarenblat@gmail.com>2021-12-26 14:55:31 -0500
commit4b49b1d0cc23f909d1be89cf8f816f820b343e0a (patch)
tree331616bf0cf1643e9abec6a49e2ead10e58ed0aa /goldfishterm/simple.h
parent520bbd892ada57a5c93680eae3c9d7eb691073af (diff)
Don’t hard-code escape sequences
Instead of hard-coding VT100-compatible escape sequences, parse the system terminfo database and read escape sequences from it. This is both more flexible (it should work well on more terminals) and more efficient (it won’t insert padding on terminals that don’t need it).
Diffstat (limited to 'goldfishterm/simple.h')
-rw-r--r--goldfishterm/simple.h71
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_