WPILibC++ 2023.4.3
Tty.h
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#ifndef WPINET_UV_TTY_H_
6#define WPINET_UV_TTY_H_
7
8#include <uv.h>
9
10#include <memory>
11#include <utility>
12
13#include "wpinet/uv/Stream.h"
14
15namespace wpi::uv {
16
17class Loop;
18class Tty;
19
20/**
21 * TTY handle.
22 * TTY handles represent a stream for the console.
23 */
24class Tty final : public StreamImpl<Tty, uv_tty_t> {
25 struct private_init {};
26
27 public:
28 explicit Tty(const private_init&) {}
29 ~Tty() noexcept override = default;
30
31 /**
32 * Create a TTY handle.
33 *
34 * @param loop Loop object where this handle runs.
35 * @param fd File descriptor, usually 0=stdin, 1=stdout, 2=stderr
36 * @param readable Specifies if you plan on calling StartRead(). stdin is
37 * readable, stdout is not.
38 */
39 static std::shared_ptr<Tty> Create(Loop& loop, uv_file fd, bool readable);
40
41 /**
42 * Create a TTY handle.
43 *
44 * @param loop Loop object where this handle runs.
45 * @param fd File descriptor, usually 0=stdin, 1=stdout, 2=stderr
46 * @param readable Specifies if you plan on calling StartRead(). stdin is
47 * readable, stdout is not.
48 */
49 static std::shared_ptr<Tty> Create(const std::shared_ptr<Loop>& loop,
50 uv_file fd, bool readable) {
51 return Create(*loop, fd, readable);
52 }
53
54 /**
55 * Set the TTY using the specified terminal mode.
56 *
57 * @param mode terminal mode
58 */
60 int err = uv_tty_set_mode(GetRaw(), mode);
61 if (err < 0) {
62 ReportError(err);
63 }
64 }
65
66 /**
67 * Reset TTY settings to default values for the next process to take over.
68 * Typically called when the program exits.
69 */
71
72 /**
73 * Gets the current window size.
74 * @return Window size (pair of width and height).
75 */
76 std::pair<int, int> GetWindowSize() {
77 int width = 0, height = 0;
78 Invoke(&uv_tty_get_winsize, GetRaw(), &width, &height);
79 return std::make_pair(width, height);
80 }
81};
82
83} // namespace wpi::uv
84
85#endif // WPINET_UV_TTY_H_
void ReportError(int err) const
Report an error.
Definition: Handle.h:236
bool Invoke(F &&f, Args &&... args) const
Definition: Handle.h:251
Event loop.
Definition: Loop.h:37
Definition: Stream.h:307
uv_tty_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Stream.h:322
TTY handle.
Definition: Tty.h:24
~Tty() noexcept override=default
static std::shared_ptr< Tty > Create(Loop &loop, uv_file fd, bool readable)
Create a TTY handle.
Tty(const private_init &)
Definition: Tty.h:28
void SetMode(uv_tty_mode_t mode)
Set the TTY using the specified terminal mode.
Definition: Tty.h:59
void ResetMode()
Reset TTY settings to default values for the next process to take over.
Definition: Tty.h:70
std::pair< int, int > GetWindowSize()
Gets the current window size.
Definition: Tty.h:76
Definition: StdDeque.h:50
Definition: ParallelTcpConnector.h:22
uv_tty_mode_t
Definition: uv.h:734
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)
int uv_file
Definition: win.h:239