Skip to content

Commit 6ea8790

Browse files
committed
added functions for reading stdin as complete string and added comments
1 parent 0823146 commit 6ea8790

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

cpp-terminal/input.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <cpp-terminal/input.hpp>
33
#include <thread>
44
#include "private/platform.hpp"
5+
#include <cpp-terminal/base.hpp>
6+
57

68
int Term::read_key() {
79
int key{};
@@ -183,3 +185,22 @@ int Term::read_key0() {
183185
return c;
184186
}
185187
}
188+
189+
// returns the whole input from STDIN as string
190+
std::string Term::read_stdin() {
191+
std::string file;
192+
char c;
193+
while (true) {
194+
c = Private::read_raw_stdin();
195+
if (c == 0x04) { // check for end of transmission signal
196+
return file;
197+
} else {
198+
file += c;
199+
}
200+
}
201+
}
202+
std::string Term::read_stdin_alone() {
203+
// temporarily enable raw mode
204+
Term::Terminal term(false, true, false, false);
205+
return Term::read_stdin();
206+
}

cpp-terminal/input.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <string>
23

34
namespace Term {
45
enum Key {
@@ -40,11 +41,24 @@ enum Key {
4041
};
4142

4243
// Waits for a key press, translates escape codes
44+
// if Term:Terminal is not enabling the keyboard it'll loop for infinity
4345
int read_key();
4446

4547
// If there was a key press, returns the translated key from escape codes,
46-
// otherwise returns 0. If the escape code is not supported, returns a
48+
// otherwise returns 0. If the escape code is not supported it returns a
4749
// negative number.
50+
// if Term::Terminal is not enabling the keyboard it'll always return 0
4851
int read_key0();
4952

53+
// returns the stdin as a string
54+
// waits until the EOT signal is send
55+
// if Term::Terminal is not enabling the keyboard this function will wait until
56+
// the user presses CTRL+D (which sends the EOT signal)
57+
std::string read_stdin();
58+
59+
// returns stdin as a string, Term::Terminal is used to enable input to make the
60+
// function non-blocking, use Term::read_stdin() when Term::Terminal is already
61+
// created
62+
std::string read_stdin_alone();
63+
5064
} // namespace Term

0 commit comments

Comments
 (0)