aboutsummaryrefslogtreecommitdiff
path: root/goldfishterm/simple.h
blob: 3f5cc9f8788ad8564b1f25d11f9358dde194d78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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_