From 8f1426ada14f0ad0dd643bed80158816d326e9d2 Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Tue, 29 Mar 2022 10:35:32 -0400 Subject: Small optimizations and bugfixes Remove un-necessary sstream from ANSI print Add cout err clearing at end of flush and redraw lines Reduce move calls in flush Add temp var for cell in flush --- LICENSE | 19 +++++++++++++++++++ straw.hpp | 41 +++++++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..83c0855 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2022 Jon Santmyer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/straw.hpp b/straw.hpp index 0b33c84..1db28d5 100644 --- a/straw.hpp +++ b/straw.hpp @@ -16,12 +16,13 @@ namespace straw { struct color { - std::uint8_t r, g, b; + std::uint8_t r{}, g{}, b{}; constexpr color(std::uint8_t R, std::uint8_t G, std::uint8_t B) : r(R), g(G), b(B) {} constexpr color(std::uint8_t A) : r(A), g(A), b(A) {} constexpr uint32_t single() { return (uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b; } - friend constexpr bool operator==(const color &lhs, const color &rhs) = default; + + constexpr bool operator==(const color &o) const = default; }; struct attribs { @@ -32,7 +33,7 @@ struct attribs { constexpr attribs(color Bg, color Fg) : bg(Bg), fg(Fg) {} constexpr attribs(color Bg, color Fg, bool B, bool U) : bg(Bg), fg(Fg), bold(B), underline(U) {} - friend constexpr bool operator==(const attribs &lhs, const attribs &rhs) = default; + constexpr bool operator==(const attribs &o) const = default; }; template @@ -50,13 +51,13 @@ struct cell { constexpr cell(chartype Chr, color Bg, color Fg, bool B, bool U) : chr(Chr), attr(Bg, Fg, B, U) {} constexpr cell(chartype Chr, attribs Attr) : chr(Chr), attr(Attr) {} - friend constexpr bool operator==(const cell &lhs, const cell &rhs) = default; + constexpr bool operator==(const cell &o) const = default; }; -const std::string ANSI_ESCAPE = "\033["; -static void ANSI_MOVE(unsigned x, unsigned y) { std::stringstream ss; ss << ANSI_ESCAPE << y+1 << ';' << x+1 << 'H'; std::cout << ss.str(); } -static void ANSI_COLOR_FG(color c) { std::stringstream ss; ss << ANSI_ESCAPE << "38;2;" << c.r << ';' << c.g << ';' << c.b << 'm'; std::cout << ss.str(); } -static void ANSI_COLOR_BG(color c) { std::stringstream ss; ss << ANSI_ESCAPE << "38;2;" << c.r << ';' << c.g << ';' << c.b << 'm'; std::cout << ss.str(); } +const std::string ANSI_ESCAPE = "\E["; +static void ANSI_MOVE(unsigned x, unsigned y) { std::cout << ANSI_ESCAPE << y+1 << ';' << x+1 << 'H' << std::flush; } +static void ANSI_COLOR_FG(color c) { std::cout << ANSI_ESCAPE << "38;2;" << c.r << ';' << c.g << ';' << c.b << 'm' << std::flush; } +static void ANSI_COLOR_BG(color c) { std::cout << ANSI_ESCAPE << "48;2;" << c.r << ';' << c.g << ';' << c.b << 'm' << std::flush; } class screen_command_base {}; @@ -127,17 +128,19 @@ public: void redraw() { attribs cattr = (*this)[0][0].attr; + ANSI_COLOR_BG(cattr.bg); + ANSI_COLOR_FG(cattr.fg); for(unsigned y = 0; y < m_height; y++) { ANSI_MOVE(0, y); for(cell c : (*this)[y]) { - if(c.attr != cattr) { - ANSI_COLOR_FG(c.attr.fg); - ANSI_COLOR_BG(c.attr.bg); + if(cattr != c.attr) { cattr = c.attr; + ANSI_COLOR_FG(cattr.fg); + ANSI_COLOR_BG(cattr.bg); } std::cout << c.chr; } - std::cout << std::flush; + std::cout.clear(); } m_back = m_front; } @@ -150,16 +153,18 @@ public: auto frontspan = (*this)[y]; if(std::equal(backspan.begin(), backspan.end(), frontspan.begin(), frontspan.end())) continue; for(unsigned x = 0; x < m_width; x++) { - if(frontspan[x] == backspan[x]) continue; + cell c = frontspan[x]; + if(c == backspan[x]) continue; ANSI_MOVE(x, y); - if(frontspan[x].attr != backspan[x].attr) { - ANSI_COLOR_FG(frontspan[x].attr.fg); - ANSI_COLOR_BG(frontspan[x].attr.bg); + if(c.attr != backspan[x].attr) { + ANSI_COLOR_FG(c.attr.fg); + ANSI_COLOR_BG(c.attr.bg); } - std::cout << frontspan[x].chr; + std::cout << c.chr; } - std::cout << std::flush; + std::cout.clear(); } + m_back = m_front; } std::span> operator[](std::size_t i) { -- cgit v1.2.1