Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pipe_cin to examples dir #325

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ cppterminal_example(SOURCE slow_events)
cppterminal_example(SOURCE utf8)
cppterminal_example(SOURCE attach_console WIN32)
cppterminal_example(SOURCE attach_console_minimal WIN32)
cppterminal_example(SOURCE pipe_cin)
80 changes: 80 additions & 0 deletions examples/pipe_cin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* cpp-terminal
* C++ library for writing multi-platform terminal applications.
*
* SPDX-FileCopyrightText: 2019-2023 cpp-terminal
*
* SPDX-License-Identifier: MIT
*/

#include "cpp-terminal/color.hpp"
#include "cpp-terminal/terminal.hpp"

#include <iostream>
#include <string>
#include <sys/select.h>

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-12.0.1 (c++17)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-12.0.1 (c++11)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-13.0.1 (c++11)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-11.1.0 (c++17)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-12.0.1 (c++20)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-11.1.0 (c++14)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-14.0.6 (c++17)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-11.1.0 (c++11)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-15.0.2 (c++14)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-15.0.2 (c++11)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-15.0.2 (c++17)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-13.0.1 (c++20)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-12.0.1 (c++14)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-14.0.6 (c++11)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-14.0.6 (c++14)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-11.1.0 (c++20)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-13.0.1 (c++14)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-13.0.1 (c++17)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-14.0.6 (c++20)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / windows-2019 clang-15.0.2 (c++20)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang32 (c++17)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang32 (c++11)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang32 (c++14)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang32 (c++20)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang64 (c++14)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang64 (c++17)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw32 (c++14)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang64 (c++20)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw32 (c++11)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw64 (c++17)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw32 (c++20)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / ucrt64 (c++17)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / clang64 (c++11)

'sys/select.h' file not found

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw64 (c++14)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / ucrt64 (c++14)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw64 (c++20)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw32 (c++17)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / ucrt64 (c++11)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / mingw64 (c++11)

sys/select.h: No such file or directory

Check failure on line 15 in examples/pipe_cin.cpp

View workflow job for this annotation

GitHub Actions / ucrt64 (c++20)

sys/select.h: No such file or directory
#include <unistd.h>

namespace
{

bool isStdinEmpty()
{
struct timeval tv
{
0, 0
};
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
return select(STDIN_FILENO + 1, &rfds, nullptr, nullptr, &tv) == 0;
}

std::string ReadStdin()
{
if(isStdinEmpty()) { return "stdin is empty"; }
std::string stdin_result;
char c;
while((c = static_cast<char>(std::fgetc(stdin))) != EOF)
{
if(c == '\n') break;
stdin_result += c;
}
return stdin_result;
}

bool isCinPipe() { return isatty(fileno(stdin)) == 0; }

std::string ReadCin()
{
if(!isCinPipe()) { return "std::cin isn't an unamed pipe"; }
std::string cin_result;
char c;
while((c = static_cast<char>(std::cin.get())) != '\n') { cin_result += c; }
return cin_result;
}

std::ostream& PrintOutColor(std::ostream& ostr, std::string const& str, Term::Color::Name color)
{
ostr << "\"" << Term::color_fg(color) << str << Term::color_fg(Term::Color::Name::Default) << "\"" << std::endl;
return ostr;
}

} // namespace

int main()
{
std::string cinBuff{};

std::cout << "Checking if std::cin is empty" << std::endl;
cinBuff = ReadCin();
std::cout << "std::cin => ";
PrintOutColor(std::cout, cinBuff, Term::Color::Name::Red);

std::cout << "Checking if stdin is empty" << std::endl;
cinBuff = ReadStdin();
std::cout << "stdin => ";
PrintOutColor(std::cout, cinBuff, Term::Color::Name::Red);

return 0;
}
Loading