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,9 +29,15 @@ 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();
timeval tv{ 0, 0 };
fd_set rfds;
ssize_t count = 0;
do
{
count = read(fifo, data, buffer_size);
auto pos = buffer->end()-1; auto pos = buffer->end()-1;
bool prevent_scrolling = pos == buffer->begin() and not scroll; bool prevent_scrolling = pos == buffer->begin() and not scroll;
@ -45,12 +53,17 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
buffer->insert(buffer->end(), "\n"); 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;
} }
}); });