Refactor fifo buffer reader code

This commit is contained in:
Maxime Coste 2019-12-28 10:32:15 +11:00
parent 62b4780e07
commit 456fbd1315

View File

@ -97,7 +97,7 @@ void reload_file_buffer(Buffer& buffer)
Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll) Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll)
{ {
static ValueId s_fifo_watcher_id = get_free_value_id(); static ValueId fifo_watcher_id = get_free_value_id();
auto& buffer_manager = BufferManager::instance(); auto& buffer_manager = BufferManager::instance();
Buffer* buffer = buffer_manager.get_buffer_ifp(name); Buffer* buffer = buffer_manager.get_buffer_ifp(name);
@ -110,24 +110,28 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
buffer = buffer_manager.create_buffer( buffer = buffer_manager.create_buffer(
std::move(name), flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo); std::move(name), flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
auto watcher_deleter = [buffer](FDWatcher* watcher) { struct FifoWatcher : FDWatcher
kak_assert(buffer->flags() & Buffer::Flags::Fifo); {
watcher->close_fd(); FifoWatcher(int fd, Buffer& buffer, bool scroll)
buffer->run_hook_in_own_context(Hook::BufCloseFifo, ""); : FDWatcher(fd, FdEvents::Read,
buffer->flags() &= ~(Buffer::Flags::Fifo | Buffer::Flags::NoUndo); [](FDWatcher& watcher, FdEvents, EventMode mode) {
delete watcher; if (mode == EventMode::Normal)
}; static_cast<FifoWatcher&>(watcher).read_fifo();
}),
m_buffer(buffer), m_scroll(scroll)
{}
// capture a non static one to silence a warning. ~FifoWatcher()
ValueId fifo_watcher_id = s_fifo_watcher_id; {
kak_assert(m_buffer.flags() & Buffer::Flags::Fifo);
close_fd();
m_buffer.run_hook_in_own_context(Hook::BufCloseFifo, "");
m_buffer.flags() &= ~(Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
}
std::unique_ptr<FDWatcher, decltype(watcher_deleter)> watcher( void read_fifo() const
new FDWatcher(fd, FdEvents::Read, {
[buffer, scroll, fifo_watcher_id](FDWatcher& watcher, FdEvents, EventMode mode) { kak_assert(m_buffer.flags() & Buffer::Flags::Fifo);
if (mode != EventMode::Normal)
return;
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
constexpr size_t buffer_size = 2048; constexpr size_t buffer_size = 2048;
// if we read data slower than it arrives in the fifo, limiting the // if we read data slower than it arrives in the fifo, limiting the
@ -137,8 +141,8 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
bool closed = false; bool closed = false;
size_t loop = 0; size_t loop = 0;
char data[buffer_size]; char data[buffer_size];
BufferCoord insert_coord = buffer->back_coord(); BufferCoord insert_coord = m_buffer.back_coord();
const int fifo = watcher.fd(); const int fifo = fd();
do do
{ {
const ssize_t count = ::read(fifo, data, buffer_size); const ssize_t count = ::read(fifo, data, buffer_size);
@ -148,36 +152,38 @@ Buffer* create_fifo_buffer(String name, int fd, Buffer::Flags flags, bool scroll
break; break;
} }
auto pos = buffer->back_coord(); auto pos = m_buffer.back_coord();
const bool prevent_scrolling = pos == BufferCoord{0,0} and not scroll; const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll;
if (prevent_scrolling) if (prevent_scrolling)
pos = buffer->next(pos); pos = m_buffer.next(pos);
buffer->insert(pos, StringView(data, data+count)); m_buffer.insert(pos, StringView(data, data+count));
if (prevent_scrolling) if (prevent_scrolling)
{ {
buffer->erase({0,0}, buffer->next({0,0})); m_buffer.erase({0,0}, m_buffer.next({0,0}));
// in the other case, the buffer will have automatically // in the other case, the buffer will have automatically
// inserted a \n to guarantee its invariant. // inserted a \n to guarantee its invariant.
if (data[count-1] == '\n') if (data[count-1] == '\n')
buffer->insert(buffer->end_coord(), "\n"); m_buffer.insert(m_buffer.end_coord(), "\n");
} }
} }
while (++loop < max_loop and fd_readable(fifo)); while (++loop < max_loop and fd_readable(fifo));
if (insert_coord != buffer->back_coord()) if (insert_coord != m_buffer.back_coord())
{ m_buffer.run_hook_in_own_context(
buffer->run_hook_in_own_context(
Hook::BufReadFifo, Hook::BufReadFifo,
selection_to_string(ColumnType::Byte, *buffer, {insert_coord, buffer->back_coord()})); selection_to_string(ColumnType::Byte, m_buffer, {insert_coord, m_buffer.back_coord()}));
}
if (closed) if (closed)
buffer->values().erase(fifo_watcher_id); // will delete this m_buffer.values().erase(fifo_watcher_id); // will delete this
}), std::move(watcher_deleter)); }
buffer->values()[fifo_watcher_id] = Value(std::move(watcher)); Buffer& m_buffer;
bool m_scroll;
};
buffer->values()[fifo_watcher_id] = Value(std::make_unique<FifoWatcher>(fd, *buffer, scroll));
buffer->flags() = flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo; buffer->flags() = flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo;
buffer->run_hook_in_own_context(Hook::BufOpenFifo, buffer->name()); buffer->run_hook_in_own_context(Hook::BufOpenFifo, buffer->name());