cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
Term::Buffer Class Referencefinal

#include <cpp-terminal/buffer.hpp>

Inheritance diagram for Term::Buffer:

Public Types

enum class  Type : std::uint8_t { Unbuffered , LineBuffered , FullBuffered }
 

Public Member Functions

 Buffer (const Term::Buffer::Type &type=Term::Buffer::Type::LineBuffered, const std::streamsize &size=BUFSIZ)
 
 ~Buffer () override
 
 Buffer (const Buffer &)=delete
 
 Buffer (Buffer &&)=delete
 
Bufferoperator= (Buffer &&)=delete
 
Bufferoperator= (const Buffer &)=delete
 

Protected Member Functions

int_type underflow () override
 
int_type overflow (int c=std::char_traits< Term::Buffer::char_type >::eof()) override
 
int sync () override
 

Private Member Functions

void setType (const Term::Buffer::Type &type)
 
std::streambuf * setbuf (char *s, std::streamsize n) override
 

Private Attributes

std::string m_buffer
 
Term::Buffer::Type m_type {Term::Buffer::Type::LineBuffered}
 

Detailed Description

Definition at line 19 of file buffer.hpp.

Member Enumeration Documentation

◆ Type

enum class Term::Buffer::Type : std::uint8_t
strong
Enumerator
Unbuffered 
LineBuffered 
FullBuffered 

Definition at line 22 of file buffer.hpp.

Constructor & Destructor Documentation

◆ Buffer() [1/3]

Term::Buffer::Buffer ( const Term::Buffer::Type & type = Term::Buffer::Type::LineBuffered,
const std::streamsize & size = BUFSIZ )
explicit

Definition at line 47 of file buffer.cpp.

48{
49 setType(type);
50 switch(m_type)
51 {
52 case Type::Unbuffered: setbuf(nullptr, 0); break;
54 case Type::FullBuffered: setbuf(&m_buffer[0], size); break;
55 }
56}
void setType(const Term::Buffer::Type &type)
Definition buffer.cpp:58
Term::Buffer::Type m_type
Definition buffer.hpp:44
std::string m_buffer
Definition buffer.hpp:43
std::streambuf * setbuf(char *s, std::streamsize n) override
Definition buffer.cpp:60

◆ ~Buffer()

Term::Buffer::~Buffer ( )
override

Definition at line 149 of file buffer.cpp.

150{
151 //sync();
152}

◆ Buffer() [2/3]

Term::Buffer::Buffer ( const Buffer & )
delete

◆ Buffer() [3/3]

Term::Buffer::Buffer ( Buffer && )
delete

Member Function Documentation

◆ operator=() [1/2]

Buffer & Term::Buffer::operator= ( Buffer && )
delete

◆ operator=() [2/2]

Buffer & Term::Buffer::operator= ( const Buffer & )
delete

◆ overflow()

Term::Buffer::int_type Term::Buffer::overflow ( int c = std::char_traits<Term::Buffer::char_type>::eof())
overrideprotected

Definition at line 113 of file buffer.cpp.

114{
115 if(c != std::char_traits<Term::Buffer::char_type>::eof())
116 {
117 switch(m_type)
118 {
119 case Type::Unbuffered:
120 {
121 Term::Private::out.write(replace(c));
122 break;
123 }
125 {
126 m_buffer += replace(c);
127 if(static_cast<char>(c) == '\n')
128 {
130 m_buffer.clear();
131 }
132 break;
133 }
135 {
136 if(m_buffer.size() >= m_buffer.capacity())
137 {
139 m_buffer.clear();
140 }
141 m_buffer += replace(c);
142 break;
143 }
144 }
145 }
146 return c;
147}
std::size_t write(const std::string &str) const
Definition file.cpp:101
OutputFileHandler & out
Definition file.cpp:44

◆ setbuf()

std::streambuf * Term::Buffer::setbuf ( char * s,
std::streamsize n )
overrideprivate

Definition at line 60 of file buffer.cpp.

61{
62 if(s != nullptr) m_buffer.reserve(static_cast<std::size_t>(n));
63 return this;
64}

◆ setType()

void Term::Buffer::setType ( const Term::Buffer::Type & type)
private

Definition at line 58 of file buffer.cpp.

58{ m_type = type; }

◆ sync()

int Term::Buffer::sync ( )
overrideprotected

Definition at line 40 of file buffer.cpp.

41{
42 const int ret = Term::Private::out.write(m_buffer);
43 m_buffer.clear();
44 return ret;
45}

◆ underflow()

Term::Buffer::int_type Term::Buffer::underflow ( )
overrideprotected

Definition at line 66 of file buffer.cpp.

67{
68 try
69 {
70 //TODO Maybe use input function ?
71 m_buffer.clear();
73 {
74 do {
75 std::string ret{Term::Private::in.read()};
76 if(!ret.empty())
77 {
78 if(ret[0] == '\x7f' || ret[0] == '\b')
79 {
80 Term::Private::out.write("\b \b"); //Backspace is DEL, CTRL+Backspace is Backspace '\b'
81 if(!m_buffer.empty()) m_buffer.erase(m_buffer.size() - 1);
82 }
83 else if(ret[0] == '\033')
84 {
85 continue; // For now if it's escape sequence do nothing
86 }
87 else if(ret[0] <= 31 && ret[0] != '\t' && ret[0] != '\b' && ret[0] != 127 && ret[0] != ' ' && ret[0] != '\n' && ret[0] != '\r') { continue; }
88 else
89 {
91 m_buffer += ret;
92 }
93 }
94 } while(m_buffer.empty() || !newline_sequence(m_buffer));
96 }
97 else
98 {
99 do {
100 std::string ret{Term::Private::in.read()};
101 m_buffer += ret;
102 } while(m_buffer.empty());
103 }
104 setg(&m_buffer[0], &m_buffer[0], &m_buffer[0] + m_buffer.size());
105 return traits_type::to_int_type(m_buffer.at(0));
106 }
107 catch(...)
108 {
109 return traits_type::eof();
110 }
111}
bool has(const Option &option) const noexcept
Definition options.cpp:35
std::string read() const
Definition file.cpp:127
Term::Options getOptions() const noexcept
InputFileHandler & in
Definition file.cpp:43
@ Raw
Set terminal in raw mode.
Term::Terminal & terminal
Definition terminal.cpp:19

Member Data Documentation

◆ m_buffer

std::string Term::Buffer::m_buffer
private

Definition at line 43 of file buffer.hpp.

◆ m_type

Term::Buffer::Type Term::Buffer::m_type {Term::Buffer::Type::LineBuffered}
private

Definition at line 44 of file buffer.hpp.


The documentation for this class was generated from the following files: