Skip to content

Commit

Permalink
Fix in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
flagarde committed Jul 11, 2023
1 parent 876c8d4 commit c9da894
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 51 deletions.
42 changes: 27 additions & 15 deletions cpp-terminal/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

#include "cpp-terminal/platforms/file.hpp"


std::string Term::Buffer::remplace(const int_type& c)
{
#if defined(_WIN32)
std::string ret;
if(static_cast<char>(c)=='\n') ret="\r\n";
else ret.push_back(static_cast<char>(c));
return ret;
#else
return m_s;
#endif
}

void Term::Buffer::setType(const Term::Buffer::Type& type) { m_type = type; }

/*std::streamsize Term::Buffer::xsputn(const char* s, std::streamsize n)
std::streamsize Term::Buffer::xsputn(const char* s, std::streamsize n)
{

if(n == 0 || s[0] == EOF) return 0;
Expand All @@ -13,15 +26,15 @@ void Term::Buffer::setType(const Term::Buffer::Type& type) { m_type = type; }
int i{0};
do
{
ret.push_back(static_cast<char>(s[i]));
if(m_type==Type::LineBuffered && (s[i]=='\n'||(m_s.size()>=m_s.capacity())))
ret+= remplace(s[i]);
if(m_type==Type::LineBuffered && (s[i]=='\n'||(m_s.size()+ret.size()>=m_s.capacity())))
{
Term::Private::out.write(m_s+ret);
m_s.clear();
}
++i;
}
while(ret.size() < static_cast<std::size_t>(n));
while(i < n);
switch(m_type)
{
case Type::Unbuffered:
Expand All @@ -39,12 +52,9 @@ void Term::Buffer::setType(const Term::Buffer::Type& type) { m_type = type; }
else m_s+=ret;
break;
}
case Type::LineBuffered:
m_s+=ret;
break;
}
return n;
} */
}

std::streambuf* Term::Buffer::setbuf(char* s, std::streamsize n)
{
Expand All @@ -61,14 +71,12 @@ Term::Buffer::int_type Term::Buffer::overflow(int_type c)
{
case Type::Unbuffered:
{
std::string t;
t += static_cast<char>(c);
Term::Private::out.write(t);
Term::Private::out.write(remplace(c));
break;
}
case Type::LineBuffered:
{
m_s.push_back(static_cast<char>(c));
m_s+=remplace(c);
if(static_cast<char>(c) == '\n')
{
Term::Private::out.write(m_s);
Expand All @@ -78,9 +86,13 @@ Term::Buffer::int_type Term::Buffer::overflow(int_type c)
}
case Type::FullBuffered:
{
if(m_s.size() == m_s.capacity()) Term::Private::out.write(m_s);
else
m_s.push_back(static_cast<char>(c));

if(m_s.size() >= m_s.capacity())
{
Term::Private::out.write(m_s);
m_s.clear();
}
m_s+=remplace(c);
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion cpp-terminal/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class Buffer : public std::streambuf
std::streambuf* setbuf(char* s, std::streamsize n) final;
virtual int_type overflow(int_type c) final;
virtual int sync() final;
//std::streamsize xsputn (const char* s, std::streamsize n);
std::streamsize xsputn (const char* s, std::streamsize n);
protected:
private:
std::string remplace(const int_type&);
std::string m_s;
Term::Buffer::Type m_type{Term::Buffer::Type::LineBuffered};
};
Expand Down
5 changes: 4 additions & 1 deletion cpp-terminal/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ void Term::TerminalInitializer::init()
if(m_counter++ == 0) new(&Term::terminal) Terminal();
}

Term::TerminalInitializer::TerminalInitializer() {}
Term::TerminalInitializer::TerminalInitializer()
{
init();
}

Term::TerminalInitializer::~TerminalInitializer()
{
Expand Down
8 changes: 4 additions & 4 deletions cpp-terminal/iostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ void Term::StreamInitializer::init()
if(m_counter++ == 0)
{
Term::m_terminalInitializer.init();
new(&Term::cout) TOstream(Term::Buffer::Type::LineBuffered);
new(&Term::cerr) TOstream(Term::Buffer::Type::LineBuffered);
new(&Term::clog) TOstream(Term::Buffer::Type::Unbuffered);
new(&Term::cin) TIstream(Term::Buffer::Type::LineBuffered);
new(&Term::cout) TOstream(Term::Buffer::Type::FullBuffered,BUFSIZ*5);
new(&Term::cerr) TOstream(Term::Buffer::Type::LineBuffered,BUFSIZ*5);
new(&Term::clog) TOstream(Term::Buffer::Type::Unbuffered,BUFSIZ*5);
new(&Term::cin) TIstream(Term::Buffer::Type::FullBuffered);
}
}

Expand Down
3 changes: 2 additions & 1 deletion cpp-terminal/platforms/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ Term::Private::FileInitializer::~FileInitializer()
int Term::Private::OutputFileHandler::write(const std::string& str)
{
#if defined(_WIN32)
DWORD dwCount;
//return ::_write(fd(), &str[0], str.size());
DWORD dwCount{0};
if(WriteConsole(handle(), &str[0], str.size(), &dwCount, nullptr) == 0) return -1;
#else
return ::write(fd(), &str[0], str.size());
Expand Down
2 changes: 1 addition & 1 deletion cpp-terminal/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ std::string Term::TIstream::file()
#endif
}

Term::TIstream::TIstream(const Term::Buffer::Type& type, const std::size_t& size) : m_stream(std::ifstream(file()))
Term::TIstream::TIstream(const Term::Buffer::Type& type, const std::size_t& size) : m_stream(std::move(std::ifstream(file())))
{
switch(type)
{
Expand Down
7 changes: 6 additions & 1 deletion cpp-terminal/style.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include "cpp-terminal/style.hpp"

std::string Term::style(const Term::Style& style) { return "\033[" + std::to_string((std::uint8_t)style) + 'm'; }
std::string Term::style(const Term::Style& style)
{
//https://unix.stackexchange.com/questions/212933/background-color-whitespace-when-end-of-the-terminal-reached
if(style==Term::Style::DEFAULT_BACKGROUND_COLOR) return "\033[" + std::to_string((std::uint8_t)style) + "m\033[K";
else return "\033[" + std::to_string((std::uint8_t)style) + 'm';
}
7 changes: 1 addition & 6 deletions cpp-terminal/terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
#include "cpp-terminal/options.hpp"
#include "cpp-terminal/terminfo.hpp"


namespace Term
{

/* Note: the code that uses Terminal must be inside try/catch block, otherwise
* the destructors will not be called when an exception happens and the
* terminal will not be left in a good state. Terminal uses exceptions when
* something goes wrong.
*/

class Terminal
{
public:
Expand Down
3 changes: 2 additions & 1 deletion examples/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ int main()
Term::cout << Term::color_bg(Term::Color(Term::Color::Name::BrightBlue).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::BrightMagenta).to3bits()) << " " << Term::color_bg(Term::Color(Term::Color::Name::BrightCyan).to3bits()) << " ";
Term::cout << Term::color_bg(Term::Color(Term::Color::Name::BrightWhite).to3bits()) << " " << Term::color_bg(Term::Color::Name::Default) << " ";
Term::cout << "*\n";

Term::cout << "Press any key to quit" << std::endl;
int i{0};
Term::cin>>i;
}
catch(const Term::Exception& re)
{
Expand Down
1 change: 1 addition & 0 deletions examples/kilo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "cpp-terminal/screen.hpp"
#include "cpp-terminal/style.hpp"
#include "cpp-terminal/terminal.hpp"
#include "cpp-terminal/iostream.hpp"
#include "cpp-terminal/tty.hpp"

#include <cstdarg>
Expand Down
1 change: 0 additions & 1 deletion examples/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

void render(int rows, int cols, int menuheight, int menuwidth, int menupos)
{
Term::cout << Term::clear_screen() << std::flush;
std::string scr;
scr.reserve(16 * 1024);

Expand Down
67 changes: 48 additions & 19 deletions examples/menu_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main()
// check if the terminal is capable of handling input
if(!Term::is_stdin_a_tty())
{
Term::cerr << "The terminal is not attached to a TTY and therefore can't catch user input. Exiting...\n";
Term::cout << "The terminal is not attached to a TTY and therefore can't catch user input. Exiting...\n";
return 1;
}
Term::terminal.setOptions(Term::Option::ClearScreen, Term::Option::NoSignalKeys, Term::Option::NoCursor, Term::Option::Raw);
Expand All @@ -56,29 +56,58 @@ int main()
std::size_t w{10};
bool on = true;
Term::Window scr(term_size.columns(), term_size.rows());
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);
while(on)
{
Term::cerr << render(scr, term_size.rows(), term_size.columns(), h, w, pos);
Term::Key key = Term::read_event();
switch(key)
Term::Event event = Term::read_event();
switch(event.type())
{
case Term::Key::ARROW_LEFT:
if(w > 10) w--;
break;
case Term::Key::ARROW_RIGHT:
if(w < term_size.columns() - 5) w++;
break;
case Term::Key::ARROW_UP:
if(pos > 1) pos--;
case Term::Event::Type::Key:
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);
switch(Term::Key(event))
{
case Term::Key::ARROW_LEFT:
if(w > 10) w--;
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);

break;
case Term::Key::ARROW_RIGHT:
if(w < term_size.columns() - 5) w++;
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);

break;
case Term::Key::ARROW_UP:
if(pos > 1) pos--;
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);

break;
case Term::Key::ARROW_DOWN:
if(pos < h) pos++;
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);

break;
case Term::Key::HOME:
pos = 1;
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);
break;
case Term::Key::END:
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);

pos = h;
break;
case Term::Key::q:
case Term::Key::ESC:
case Term::Key::CTRL_C:
on = false;
break;
default: break;
}
break;
case Term::Key::ARROW_DOWN:
if(pos < h) pos++;
case Term::Event::Type::Screen:
term_size = Term::Screen(event);
Term::cout << Term::clear_screen() << std::flush;
Term::cout << render(scr, term_size.rows(), term_size.columns(), h, w, pos);
break;
case Term::Key::HOME: pos = 1; break;
case Term::Key::END: pos = h; break;
case Term::Key::q:
case Term::Key::ESC:
case Term::Key::CTRL_C: on = false; break;
default: break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions examples/styles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ int main()

Term::cout << "Normal Text " << Term::Style::SUPERSCRIPT << "SUPERSCRIPT" << Term::Style::RESET_SUPERSCRIPT_SUBSCRIPT << std::endl;
Term::cout << "Normal Text " << Term::Style::SUBSCRIPT << "SUBSCRIPT" << Term::Style::RESET_SUPERSCRIPT_SUBSCRIPT << std::endl;
int i{0};
Term::cin>>i;
}

0 comments on commit c9da894

Please sign in to comment.