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)
{
static ValueId s_fifo_watcher_id = get_free_value_id();
static ValueId fifo_watcher_id = get_free_value_id();
auto& buffer_manager = BufferManager::instance();
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(
std::move(name), flags | Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
auto watcher_deleter = [buffer](FDWatcher* watcher) {
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
watcher->close_fd();
buffer->run_hook_in_own_context(Hook::BufCloseFifo, "");
buffer->flags() &= ~(Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
delete watcher;
};
struct FifoWatcher : FDWatcher
{
FifoWatcher(int fd, Buffer& buffer, bool scroll)
: FDWatcher(fd, FdEvents::Read,
[](FDWatcher& watcher, FdEvents, EventMode mode) {
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.
ValueId fifo_watcher_id = s_fifo_watcher_id;
~FifoWatcher()
{
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(
new FDWatcher(fd, FdEvents::Read,
[buffer, scroll, fifo_watcher_id](FDWatcher& watcher, FdEvents, EventMode mode) {
if (mode != EventMode::Normal)
return;
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
void read_fifo() const
{
kak_assert(m_buffer.flags() & Buffer::Flags::Fifo);
constexpr size_t buffer_size = 2048;
// 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;
size_t loop = 0;
char data[buffer_size];
BufferCoord insert_coord = buffer->back_coord();
const int fifo = watcher.fd();
BufferCoord insert_coord = m_buffer.back_coord();
const int fifo = fd();
do
{
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;
}
auto pos = buffer->back_coord();
const bool prevent_scrolling = pos == BufferCoord{0,0} and not scroll;
auto pos = m_buffer.back_coord();
const bool prevent_scrolling = pos == BufferCoord{0,0} and not m_scroll;
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)
{
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
// inserted a \n to guarantee its invariant.
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));
if (insert_coord != buffer->back_coord())
{
buffer->run_hook_in_own_context(
if (insert_coord != m_buffer.back_coord())
m_buffer.run_hook_in_own_context(
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)
buffer->values().erase(fifo_watcher_id); // will delete this
}), std::move(watcher_deleter));
m_buffer.values().erase(fifo_watcher_id); // will delete this
}
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->run_hook_in_own_context(Hook::BufOpenFifo, buffer->name());