aboutsummaryrefslogtreecommitdiff
path: root/ttwhy/terminal/scoped_attributes.cppm
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2026-09-22 22:47:33 +0200
committerFelix Morgner <felix.morgner@gmail.com>2026-09-22 22:47:33 +0200
commitd05b9343f69a0841043963088e0003368e205bd9 (patch)
tree8cde07ff86d27cbc808a0a149b89d0024b4a586c /ttwhy/terminal/scoped_attributes.cppm
parent22f6534081acfabdab4956d337604a21e7da5b64 (diff)
downloadttwhy-master.tar.xz
ttwhy-master.zip
lib: restructure into vertical slicesHEADmaster
Diffstat (limited to 'ttwhy/terminal/scoped_attributes.cppm')
-rw-r--r--ttwhy/terminal/scoped_attributes.cppm135
1 files changed, 135 insertions, 0 deletions
diff --git a/ttwhy/terminal/scoped_attributes.cppm b/ttwhy/terminal/scoped_attributes.cppm
new file mode 100644
index 0000000..2ca34a3
--- /dev/null
+++ b/ttwhy/terminal/scoped_attributes.cppm
@@ -0,0 +1,135 @@
+module;
+
+#include <termios.h>
+#include <unistd.h>
+
+#include <cerrno>
+#include <system_error>
+#include <utility>
+
+export module ttwhy.terminal:scoped_attributes;
+
+namespace ttwhy::terminal
+{
+
+ auto static read_attributes(int file_descriptor) -> termios
+ {
+ auto active_attributes = termios{};
+ if (::tcgetattr(file_descriptor, &active_attributes))
+ {
+ throw std::system_error(errno, std::system_category(), "failed to read termios attributes");
+ }
+ return active_attributes;
+ }
+
+ auto static get_line_discipline_flag(int file_descriptor, int flag) -> bool
+ {
+ auto active_attributes = read_attributes(file_descriptor);
+ return active_attributes.c_lflag & flag;
+ }
+
+ auto static set_line_discipline_flag(int file_descriptor, int flag, bool enabled) -> void
+ {
+ auto active_attributes = read_attributes(file_descriptor);
+
+ if ((active_attributes.c_lflag & flag) == enabled)
+ {
+ return;
+ }
+
+ if (enabled)
+ {
+ active_attributes.c_lflag = active_attributes.c_lflag | flag;
+ }
+ else
+ {
+ active_attributes.c_lflag = active_attributes.c_lflag & ~flag;
+ }
+
+ if (::tcsetattr(file_descriptor, TCSANOW, &active_attributes))
+ {
+ throw std::system_error(errno, std::system_category(), "failed to write termios attributes");
+ }
+ }
+
+ //! Store the current TC attributes and restore them on destruction.
+ export struct scoped_attributes
+ {
+ explicit scoped_attributes(int file_descriptor)
+ : m_file_descriptor{-1}
+ , m_attributes{}
+ {
+ if (isatty(file_descriptor))
+ {
+ m_file_descriptor = dup(file_descriptor);
+ m_attributes = read_attributes(m_file_descriptor);
+ }
+ }
+
+ scoped_attributes(scoped_attributes const &) = delete;
+
+ scoped_attributes(scoped_attributes && other)
+ : m_file_descriptor{std::exchange(other.m_file_descriptor, -1)}
+ , m_attributes{std::move(other.m_attributes)}
+ {}
+
+ ~scoped_attributes()
+ {
+ if (m_file_descriptor > 0)
+ {
+ ::tcsetattr(m_file_descriptor, TCSANOW, &m_attributes);
+ ::close(m_file_descriptor);
+ }
+ }
+
+ auto operator=(scoped_attributes const &) -> scoped_attributes & = delete;
+
+ auto operator=(scoped_attributes && other) -> scoped_attributes &
+ {
+ std::ranges::swap(m_file_descriptor, other.m_file_descriptor);
+ std::ranges::swap(m_attributes, other.m_attributes);
+ return *this;
+ }
+
+ //! Check if canonical mode is currently active
+ [[nodiscard]] auto canonical_mode() const noexcept -> bool
+ {
+ return get_line_discipline_flag(m_file_descriptor, ICANON);
+ }
+
+ //! Set whether canonical mode is active or not.
+ auto canonical_mode(bool enabled) & -> scoped_attributes &
+ {
+ set_line_discipline_flag(m_file_descriptor, ICANON, enabled);
+ return *this;
+ }
+
+ auto canonical_mode(bool enabled) && -> scoped_attributes
+ {
+ set_line_discipline_flag(m_file_descriptor, ICANON, enabled);
+ return std::move(*this);
+ }
+
+ [[nodiscard]] auto echo() const noexcept -> bool
+ {
+ return get_line_discipline_flag(m_file_descriptor, ECHO);
+ }
+
+ auto echo(bool enabled) & -> scoped_attributes &
+ {
+ set_line_discipline_flag(m_file_descriptor, ECHO, enabled);
+ return *this;
+ }
+
+ auto echo(bool enabled) && -> scoped_attributes
+ {
+ set_line_discipline_flag(m_file_descriptor, ECHO, enabled);
+ return std::move(*this);
+ }
+
+ private:
+ int m_file_descriptor;
+ termios m_attributes;
+ };
+
+} // namespace ttwhy::terminal