From b30a695ffc330ef69e5d688d8b2f283103a65d1e Mon Sep 17 00:00:00 2001 From: Raphael Robatsch Date: Wed, 27 Oct 2021 18:20:14 +0200 Subject: [PATCH] packaging --- src/{config.hpp => config.def.hpp} | 3 +- src/line_buffer.hpp | 60 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) rename src/{config.hpp => config.def.hpp} (89%) create mode 100644 src/line_buffer.hpp diff --git a/src/config.hpp b/src/config.def.hpp similarity index 89% rename from src/config.hpp rename to src/config.def.hpp index 8ef8813..38f4909 100644 --- a/src/config.hpp +++ b/src/config.def.hpp @@ -10,14 +10,13 @@ constexpr int paddingX = 10; constexpr int paddingY = 3; // See https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html -constexpr const char *font = "Source Sans Pro 12"; +constexpr const char *font = "Sans 12"; constexpr ColorScheme colorInactive = {Color(0xbb, 0xbb, 0xbb), Color(0x22, 0x22, 0x22)}; constexpr ColorScheme colorActive = {Color(0xee, 0xee, 0xee), Color(0x00, 0x55, 0x77)}; constexpr const char *termcmd[] = {"foot", nullptr}; constexpr Button buttons[] = { - { ClkTagBar, BTN_LEFT, toggleview, {0} }, { ClkTagBar, BTN_LEFT, view, {0} }, { ClkTagBar, BTN_RIGHT, tag, {0} }, { ClkTagBar, BTN_MIDDLE, toggletag, {0} }, diff --git a/src/line_buffer.hpp b/src/line_buffer.hpp new file mode 100644 index 0000000..37f4e3f --- /dev/null +++ b/src/line_buffer.hpp @@ -0,0 +1,60 @@ +// somebar - dwl bar +// See LICENSE file for copyright and license details. + +#pragma once +#include +#include + +// reads data from Reader, and passes complete lines to Handler. +template +class LineBuffer { + std::array _buffer; + size_t _bytesBuffered {0}; + size_t _bytesConsumed {0}; + bool _discardLine {0}; +public: + template + size_t readLines(const Reader& reader, const Handler& handler) + { + auto d = _buffer.data(); + while (true) { + auto bytesRead = reader(d + _bytesBuffered, _buffer.size() - _bytesBuffered); + if (bytesRead <= 0) { + return bytesRead; + } + + _bytesBuffered += bytesRead; + char* linePosition = nullptr; + do { + char* lineStart = d + _bytesConsumed; + linePosition = static_cast(memchr( + lineStart, + '\n', + _bytesBuffered - _bytesConsumed)); + + if (linePosition) { + int lineLength = linePosition - lineStart; + if (!_discardLine) { + handler(lineStart, lineLength); + } + _bytesConsumed += lineLength + 1; + _discardLine = false; + } + } while (linePosition); + + size_t bytesRemaining = _bytesBuffered - _bytesConsumed; + if (bytesRemaining == _buffer.size()) { + // line too long + _discardLine = true; + _bytesBuffered = 0; + _bytesConsumed = 0; + } else if (bytesRemaining > 0 && _bytesConsumed > 0) { + // move the last partial message to the front of the buffer, so a full-sized + // message will fit + memmove(d, d+_bytesConsumed, bytesRemaining); + _bytesBuffered = bytesRemaining; + _bytesConsumed = 0; + } + } + } +};