aboutsummaryrefslogtreecommitdiff
path: root/src/ui/terminal/line.cc
blob: 8f156cb6a214698bd8882d79d49b61d9c5fd6c98 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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.

#include "src/ui/terminal/line.h"

#include <errno.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>

#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <string>
#include <system_error>
#include <thread>
#include <type_traits>

#include "third_party/abseil/absl/strings/str_cat.h"
#include "third_party/abseil/absl/strings/string_view.h"
#include "third_party/abseil/absl/synchronization/mutex.h"

namespace ec {

namespace {

constexpr absl::string_view kBeginningOfLine = "\r";

int CheckedCall(const char* what_arg, int r) {
  if (r < 0) {
    throw std::system_error(errno, std::generic_category(), what_arg);
  }
  return r;
}

bool SigwinchBlocked() {
  sigset_t current_blocked;
  CheckedCall("pthread_sigmask",
              pthread_sigmask(/*how=*/0, /*set=*/nullptr, &current_blocked));
  return CheckedCall("sigismember", sigismember(&current_blocked, SIGWINCH));
}

sigset_t SigsetContaining(int signal) {
  sigset_t set;
  sigemptyset(&set);
  CheckedCall("sigaddset", sigaddset(&set, signal));
  return set;
}

int TerminalColumns() {
  winsize size;
  CheckedCall("ioctl", ioctl(STDOUT_FILENO, TIOCGWINSZ, &size));
  return size.ws_col;
}

absl::string_view ClearToEol() noexcept {
  static const std::string kSequence =
      absl::StrCat("\x1b[K", std::string(3, '\0') /* VT100 padding */);
  return kSequence;
}

}  // namespace

TerminalLine::TerminalLine() {
  if (!SigwinchBlocked()) {
    throw std::logic_error("TerminalLine constructed without SIGWINCH blocked");
  }

  EnterRawMode();

  sigwinch_watcher_ = std::thread([this] {
    sigset_t sigwinch = SigsetContaining(SIGWINCH);
    int received;
    while (true) {
      sigwait(&sigwinch, &received);
      ReportSigwinch();
    }
  });
  ReportSigwinch();  // initialize state reset on SIGWINCH
}

TerminalLine::~TerminalLine() noexcept {
  static_assert(std::is_same_v<std::thread::native_handle_type, pthread_t>);
  pthread_cancel(sigwinch_watcher_.native_handle());
  sigwinch_watcher_.join();

  ExitRawMode();

  // Move the cursor to the start of the next line.
  std::cout << '\n';
}

void TerminalLine::SetLine(std::string text) {
  absl::MutexLock lock(&mu_);
  line_ = std::move(text);
}

void TerminalLine::Refresh() {
  absl::MutexLock lock(&mu_);
  if (line_.size() < columns_) {
    // We can fit the whole line and the cursor on the screen at once.
    WriteRaw(absl::StrCat(kBeginningOfLine, line_, ClearToEol()));
  } else {
    auto to_display = std::min<int>(line_.size(), columns_ - 4);
    WriteRaw(
        absl::StrCat(kBeginningOfLine, "...",
                     absl::string_view(&*line_.end() - to_display, to_display),
                     ClearToEol()));
  }
}

char TerminalLine::GetChar() {
  while (true) {
    char c;
    int r = read(STDIN_FILENO, &c, 1);
    if (r > 0) {
      return c;
    } else if (r == 0) {  // EOF
      return kControlD;
    } else if (errno == EINTR) {
      continue;
    } else {
      throw std::system_error(errno, std::generic_category(), "read");
    }
  }
}

void TerminalLine::Beep() {
  absl::MutexLock lock(&mu_);
  WriteRaw("\a");
}

void TerminalLine::PrintLine(absl::string_view message) {
  {
    absl::MutexLock lock(&mu_);
    WriteRaw(absl::StrCat("\r\n", message, "\r\n"));
  }
  Refresh();
}

void TerminalLine::EnterRawMode() {
  CheckedCall("tcgetattr", tcgetattr(STDIN_FILENO, &original_termios_));

  current_termios_ = original_termios_;
  current_termios_.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  current_termios_.c_oflag &= ~(OPOST);
  current_termios_.c_cflag |= CS8;
  current_termios_.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  CheckedCall("tcsetattr",
              tcsetattr(STDIN_FILENO, TCSAFLUSH, &current_termios_));

  // tcsetattr returns successfully if _any_ of its changes were successful, so
  // check again to make sure everything went through.
  termios actual;
  CheckedCall("tcgetattr", tcgetattr(STDIN_FILENO, &actual));
  if (actual.c_iflag & (BRKINT | ICRNL | INPCK | ISTRIP | IXON) ||
      actual.c_oflag & OPOST || (actual.c_cflag & CS8) != CS8 ||
      actual.c_lflag & (ECHO | ICANON | IEXTEN | ISIG)) {
    throw std::runtime_error("tcsetattr: could not apply all settings");
  }
}

void TerminalLine::ExitRawMode() noexcept {
  tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios_);
}

void TerminalLine::ReportSigwinch() {
  {
    absl::MutexLock lock(&mu_);
    columns_ = TerminalColumns();
  }
  Refresh();
}

void TerminalLine::WriteRaw(absl::string_view bytes) {
  while (true) {
    int r = write(STDOUT_FILENO, bytes.data(), bytes.size());
    if (r >= 0) {
      return;
    } else if (errno == EINTR) {
      continue;
    } else {
      throw std::system_error(errno, std::generic_category(), "write");
    }
  }
}

void BlockSigwinch() {
  sigset_t sigwinch = SigsetContaining(SIGWINCH);
  CheckedCall("pthread_sigmask",
              pthread_sigmask(SIG_BLOCK, &sigwinch, /*oldset=*/nullptr));
}

}  // namespace ec