nngn
Loading...
Searching...
No Matches
terminal.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_TERMINAL_TERMINAL_H
2#define NNGN_GRAPHICS_TERMINAL_TERMINAL_H
3
4#include "math/vec2.h"
5#include "utils/log.h"
6#include "utils/utils.h"
7
8namespace nngn::term {
9
10using nngn::uvec2;
11
13class Terminal {
14public:
22 Terminal(int fd);
23 ~Terminal(void);
25 auto size(void) const { return this->m_size; }
27 auto pixel_size(void) const { return this->pixel; }
28 bool init(void);
30 std::tuple<bool, bool> update_size(void);
32 bool write(std::size_t n, const char *p) const;
34 template<typename T>
35 bool write(const T &v) const { return this->write(v.size(), v.data()); }
37 bool flush(void) const;
38 bool drain(void) const;
39private:
41 int fd = -1;
42 int tty_fd = -1;
44 std::FILE *f = nullptr;
47};
48
49inline Terminal::Terminal(int fd_) : fd(fd_) {}
50
51inline bool Terminal::write(std::size_t n, const char *p) const {
52 constexpr std::size_t max = 1;
53 for(std::size_t w = 0; n; n -= w, p += static_cast<std::ptrdiff_t>(w)) {
54 w = fwrite(p, 1, std::min(n, max), this->f);
55// if(w != n) {
56// nngn::Log::l() << w << ' ' << n << '\n';
57// return false;
58// }
59// if(ferror(this->f) && errno != EAGAIN)
60// return nngn::Log::perror("fwrite"), false;
61 }
62 return true;
63}
64
65}
66
67#endif
Terminal(void)=default
Handles interactions with the output terminal.
Definition terminal.h:13
auto pixel_size(void) const
Size of the terminal in pixels.
Definition terminal.h:27
uvec2 pixel
Definition terminal.h:46
int tty_fd
Definition terminal.h:42
uvec2 m_size
Definition terminal.h:45
bool write(std::size_t n, const char *p) const
Outputs the entire contents of a buffer.
Definition terminal.h:51
std::tuple< bool, bool > update_size(void)
Asks the OS for the terminal size.
bool drain(void) const
Terminal(int fd)
Creates an object for a given TTY.
Definition terminal.h:49
bool flush(void) const
Synchronizes the output file descriptor.
bool write(const T &v) const
Outputs the entire contents of a container.
Definition terminal.h:35
std::FILE * f
fopened version of fd.
Definition terminal.h:44
auto size(void) const
Size of the terminal in characters.
Definition terminal.h:25
local n
Definition dump_lights.lua:5
#define T(f0, f1, f2)
Definition const.h:6
v[1]
Definition math.lua:22
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
local w
Definition strict.lua:12
std::chrono::duration< float, typename T::period > fd
Definition timing.cpp:8