Skip to content
Merged
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
33 changes: 31 additions & 2 deletions src/console.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "consolepp/console.hpp"

#include <boost/asio/error.hpp>
#include <boost/make_unique.hpp>
#include <sys/ioctl.h>
#include <termios.h>
Expand Down Expand Up @@ -67,6 +68,35 @@ extent get_console_size(int descriptor)
return {window_size.ws_col, window_size.ws_row};
}

// ==========================================================================
// WRITE_ALL
// ==========================================================================
void write_all(boost::asio::posix::stream_descriptor &stream, bytes const &data)
{
auto const *position = data.data();
auto remaining = data.size();

while (remaining != 0)
{
auto error = boost::system::error_code{};
auto const bytes_written = stream.write_some(
boost::asio::const_buffer{position, remaining}, error);

if (error == boost::asio::error::interrupted)
{
continue;
}

if (error)
{
throw boost::system::system_error{error, "write_some"};
}

position += bytes_written;
remaining -= bytes_written;
}
}

} // namespace

// ==========================================================================
Expand Down Expand Up @@ -150,8 +180,7 @@ void console::write(bytes data)
{
if (!data.empty())
{
stream_.write_some(
boost::asio::const_buffer{&*data.begin(), data.size()});
write_all(stream_, data);
}
}

Expand Down
Loading