read all available text in fifo each time the fd is signaled

This commit is contained in:
Maxime Coste 2014-05-15 19:11:59 +01:00
parent 34e1c2ddd5
commit aa48179131

View File

@ -2,6 +2,8 @@
#include "event_manager.hh" #include "event_manager.hh"
#include <sys/select.h>
namespace Kakoune namespace Kakoune
{ {
@ -27,30 +29,41 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
Buffer* buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo); Buffer* buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
auto watcher = new FDWatcher(fd, [buffer, scroll](FDWatcher& watcher) { auto watcher = new FDWatcher(fd, [buffer, scroll](FDWatcher& watcher) {
constexpr size_t buffer_size = 1024 * 16; constexpr size_t buffer_size = 2048;
char data[buffer_size]; char data[buffer_size];
ssize_t count = read(watcher.fd(), data, buffer_size); const int fifo = watcher.fd();
auto pos = buffer->end()-1; timeval tv{ 0, 0 };
fd_set rfds;
bool prevent_scrolling = pos == buffer->begin() and not scroll; ssize_t count = 0;
if (prevent_scrolling) do
++pos;
buffer->insert(pos, count > 0 ? String(data, data+count)
: "*** kak: fifo closed ***\n");
if (prevent_scrolling)
{ {
buffer->erase(buffer->begin(), buffer->begin()+1); count = read(fifo, data, buffer_size);
buffer->insert(buffer->end(), "\n"); auto pos = buffer->end()-1;
bool prevent_scrolling = pos == buffer->begin() and not scroll;
if (prevent_scrolling)
++pos;
buffer->insert(pos, count > 0 ? String(data, data+count)
: "*** kak: fifo closed ***\n");
if (prevent_scrolling)
{
buffer->erase(buffer->begin(), buffer->begin()+1);
buffer->insert(buffer->end(), "\n");
}
FD_ZERO(&rfds);
FD_SET(fifo, &rfds);
} }
while (count > 0 and select(fifo+1, &rfds, nullptr, nullptr, &tv) == 1);
if (count <= 0) if (count <= 0)
{ {
kak_assert(buffer->flags() & Buffer::Flags::Fifo); kak_assert(buffer->flags() & Buffer::Flags::Fifo);
buffer->flags() &= ~Buffer::Flags::Fifo; buffer->flags() &= ~Buffer::Flags::Fifo;
buffer->flags() &= ~Buffer::Flags::NoUndo; buffer->flags() &= ~Buffer::Flags::NoUndo;
close(watcher.fd()); close(fifo);
delete &watcher; delete &watcher;
} }
}); });