WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Tty.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8#include <utility>
9
10#include <uv.h>
11
12#include "wpi/net/uv/Stream.hpp"
13
14namespace wpi::net::uv {
15
16class Loop;
17class Tty;
18
19/**
20 * TTY handle.
21 * TTY handles represent a stream for the console.
22 */
23class Tty final : public StreamImpl<Tty, uv_tty_t> {
24 struct private_init {};
25
26 public:
27 explicit Tty(const private_init&) {}
28 ~Tty() noexcept override = default;
29
30 /**
31 * Create a TTY handle.
32 *
33 * @param loop Loop object where this handle runs.
34 * @param fd File descriptor, usually 0=stdin, 1=stdout, 2=stderr
35 * @param readable Specifies if you plan on calling StartRead(). stdin is
36 * readable, stdout is not.
37 */
38 static std::shared_ptr<Tty> Create(Loop& loop, uv_file fd, bool readable);
39
40 /**
41 * Create a TTY handle.
42 *
43 * @param loop Loop object where this handle runs.
44 * @param fd File descriptor, usually 0=stdin, 1=stdout, 2=stderr
45 * @param readable Specifies if you plan on calling StartRead(). stdin is
46 * readable, stdout is not.
47 */
48 static std::shared_ptr<Tty> Create(const std::shared_ptr<Loop>& loop,
49 uv_file fd, bool readable) {
50 return Create(*loop, fd, readable);
51 }
52
53 /**
54 * Set the TTY using the specified terminal mode.
55 *
56 * @param mode terminal mode
57 */
59 int err = uv_tty_set_mode(GetRaw(), mode);
60 if (err < 0) {
62 }
63 }
64
65 /**
66 * Reset TTY settings to default values for the next process to take over.
67 * Typically called when the program exits.
68 */
70
71 /**
72 * Gets the current window size.
73 * @return Window size (pair of width and height).
74 */
75 std::pair<int, int> GetWindowSize() {
76 int width = 0, height = 0;
77 Invoke(&uv_tty_get_winsize, GetRaw(), &width, &height);
78 return std::pair{width, height};
79 }
80};
81
82} // namespace wpi::net::uv
bool Invoke(F &&f, Args &&... args) const
Definition Handle.hpp:265
void ReportError(int err) const
Report an error.
Definition Handle.hpp:250
Event loop.
Definition Loop.hpp:35
uv_tty_t * GetRaw() const noexcept
Definition Stream.hpp:323
StreamImpl()
Definition Stream.hpp:328
TTY handle.
Definition Tty.hpp:23
Tty(const private_init &)
Definition Tty.hpp:27
void SetMode(uv_tty_mode_t mode)
Set the TTY using the specified terminal mode.
Definition Tty.hpp:58
std::pair< int, int > GetWindowSize()
Gets the current window size.
Definition Tty.hpp:75
void ResetMode()
Reset TTY settings to default values for the next process to take over.
Definition Tty.hpp:69
~Tty() noexcept override=default
static std::shared_ptr< Tty > Create(Loop &loop, uv_file fd, bool readable)
Create a TTY handle.
Definition StringMap.hpp:773
Definition Errors.hpp:112
Definition Prepare.hpp:14
uv_tty_mode_t
Definition uv.h:795
UV_EXTERN int uv_tty_set_mode(uv_tty_t *, uv_tty_mode_t mode)
UV_EXTERN int uv_tty_reset_mode(void)
UV_EXTERN int uv_tty_get_winsize(uv_tty_t *, int *width, int *height)