From a5b3c255c355c4744a25126ebec56231e1bf61c3 Mon Sep 17 00:00:00 2001 From: Ttibsi Date: Thu, 6 Mar 2025 15:50:29 +0000 Subject: [PATCH 1/3] Add new build flags from iris --- CMakeLists.txt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 34804f0..46e9c24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,9 +7,13 @@ add_subdirectory(rawterm) if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) option(RUN_TESTS "Run unit tests" OFF) - add_compile_options("-g") - add_compile_options("-Wall") - add_compile_options("-Werror") + add_compile_options(-g) + add_compile_options(-Wall) + add_compile_options(-Wextra) + add_compile_options(-pedantic) + add_compile_options(-Wconversion) + add_compile_options(-Wno-implicit-int-float-conversion) + add_compile_options(-Wimplicit-fallthrough) set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) From d0ea27aa28cb629a6f655bd3c66cfc1ba3ce4699 Mon Sep 17 00:00:00 2001 From: Ttibsi Date: Mon, 10 Mar 2025 10:51:57 +0000 Subject: [PATCH 2/3] almost everything but red_blue_panes passes --- examples/cells.cpp | 4 ++-- examples/game.cpp | 8 ++++---- examples/raw_escapes.cpp | 4 ++-- examples/vertical_menu.cpp | 2 +- rawterm/color.h | 4 ++-- rawterm/extras/border.h | 37 +++++++++++++++++++++---------------- rawterm/extras/menu.h | 22 ++++++++++++---------- rawterm/screen.cpp | 8 ++++---- rawterm/screen.h | 4 ++-- 9 files changed, 50 insertions(+), 43 deletions(-) diff --git a/examples/cells.cpp b/examples/cells.cpp index 313526a..3d52411 100644 --- a/examples/cells.cpp +++ b/examples/cells.cpp @@ -13,8 +13,8 @@ int main() { rawterm::Pos term_size = rawterm::get_term_size(); bool toggle = false; - for (unsigned int i = 1; i <= term_size.horizontal; i++) { - for (unsigned int j = 1; j <= term_size.vertical; j++) { + for (int i = 1; i <= term_size.horizontal; i++) { + for (int j = 1; j <= term_size.vertical; j++) { if (toggle) { std::cout << rawterm::set_background(" ", rawterm::Colors::black); toggle = false; diff --git a/examples/game.cpp b/examples/game.cpp index 4d66671..7e124f6 100644 --- a/examples/game.cpp +++ b/examples/game.cpp @@ -20,7 +20,7 @@ struct Sprite { } }; -std::vector setupCoins(const int& lvl, const rawterm::Pos& term_size) { +std::vector setupCoins(const unsigned int& lvl, const rawterm::Pos& term_size) { // setup random // https://stackoverflow.com/a/7560564 std::random_device rd; // obtain a random number from hardware @@ -30,7 +30,7 @@ std::vector setupCoins(const int& lvl, const rawterm::Pos& term_size) { std::vector coins = {}; - for (int i = 0; i < lvl + 3; i++) { + for (unsigned int i = 0; i < lvl + 3; i++) { coins.emplace_back('0', distr_vert(gen), distr_horiz(gen)); } @@ -80,10 +80,10 @@ int main() { } // Remove collected coin - for (int i = 0; i < coins.size(); i++) { + for (std::size_t i = 0; i < coins.size(); i++) { Sprite* c = &coins.at(i); if (player.vert == c->vert && player.horiz == c->horiz) { - coins.erase(coins.begin() + i); + coins.erase(coins.begin() + static_cast(i)); score++; // Check for next level diff --git a/examples/raw_escapes.cpp b/examples/raw_escapes.cpp index 9e7efc3..5ae8406 100644 --- a/examples/raw_escapes.cpp +++ b/examples/raw_escapes.cpp @@ -17,7 +17,7 @@ int main() { while (true) { std::string seq; - int ret = read(STDIN_FILENO, seq.data(), 32); + long ret = read(STDIN_FILENO, seq.data(), 32); if (ret < 0) { std::perror("ERROR: something went wrong during reading user input"); break; @@ -26,7 +26,7 @@ int main() { std::string code; for (int i = 0; i < ret; ++i) { std::stringstream ss; - ss << std::hex << "\\x" << static_cast(seq[i]); + ss << std::hex << "\\x" << static_cast(seq[i]); code += ss.str(); } diff --git a/examples/vertical_menu.cpp b/examples/vertical_menu.cpp index e7e4787..59e19ac 100644 --- a/examples/vertical_menu.cpp +++ b/examples/vertical_menu.cpp @@ -32,7 +32,7 @@ int main() { // Reset cursor pos cur.move( - {vert_menu.dims.top_left.vertical + vert_menu.active_opt - 1, + {vert_menu.dims.top_left.vertical + static_cast(vert_menu.active_opt) - 1, vert_menu.dims.top_left.horizontal}); }; diff --git a/rawterm/color.h b/rawterm/color.h index aca3cc0..92baf66 100644 --- a/rawterm/color.h +++ b/rawterm/color.h @@ -9,8 +9,8 @@ namespace rawterm { unsigned int green; unsigned int blue; - Color() : red(0), green(0), blue(0) {} - Color(int x, int y, int z) : red(x), green(y), blue(z) {} + Color() : red(0u), green(0u), blue(0u) {} + Color(unsigned int x, unsigned int y, unsigned int z) : red(x), green(y), blue(z) {} Color(const std::string&); const std::string to_hex(); diff --git a/rawterm/extras/border.h b/rawterm/extras/border.h index 7e70f1d..c837e20 100644 --- a/rawterm/extras/border.h +++ b/rawterm/extras/border.h @@ -14,7 +14,7 @@ namespace rawterm { struct Border { Region size; - int border_padding = 0; + std::size_t border_padding = 0; std::optional border_char; std::optional border_title; @@ -30,7 +30,7 @@ namespace rawterm { Border(Region size) : size(size), border_char(std::nullopt) {} Border(Region size, char border_char) : size(size), border_char(border_char) {} - Border& set_padding(int padding) { + Border& set_padding(std::size_t padding) { border_padding = padding; return *this; } @@ -50,37 +50,42 @@ namespace rawterm { return ""; } - const int truncated_length = size.width() - 2; + const std::size_t truncated_length = size.width() - 2; if (truncated_length < 4) { return "..."; } + if (truncated_length >= border_title.value().size()) { return border_title.value(); } - return border_title.value().substr(0, size.width() - 4) + "..."; + + return border_title.value().substr(0, static_cast(size.width() - 4)) + + "..."; } [[nodiscard]] std::vector render(const std::vector* text) const { auto trunc_title = truncated_title(); std::vector render = {""}; - int longest_txt = + std::size_t longest_txt = std::max_element( text->begin(), text->end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); }) ->size() + - border_padding; + std::size_t(border_padding); + if (longest_txt > size.width()) { longest_txt = size.width() - 2; } // Top line - const int post_title_len = (longest_txt + border_padding + 2) - trunc_title.size() - 1; + const std::size_t post_title_len = + static_cast(longest_txt + border_padding + 2) - trunc_title.size() - 1; if (border_char.has_value()) { render.at(0) = border_char.value() + trunc_title; render.at(0) += std::string(post_title_len, border_char.value()); } else { render.at(0) = CORNER_TL + trunc_title; - for (int i = 0; i < post_title_len; i++) { + for (std::size_t i = 0; i < post_title_len; i++) { render.at(0) += HORIZONTAL_BAR; } render.at(0) += CORNER_TR; @@ -96,10 +101,10 @@ namespace rawterm { } std::string drawable_text = line; - if (line.size() > longest_txt) { - drawable_text = line.substr(0, longest_txt); + if (line.size() > static_cast(longest_txt)) { + drawable_text = line.substr(0, static_cast(longest_txt)); } - const int line_buffer = longest_txt - drawable_text.size(); + const std::size_t line_buffer = longest_txt - drawable_text.size(); rendered_line += std::string(border_padding, ' ') + drawable_text + std::string(border_padding + line_buffer, ' '); @@ -118,7 +123,7 @@ namespace rawterm { btm = std::string(render.at(0).size(), border_char.value()); } else { btm = CORNER_BL; - for (int i = 0; i <= longest_txt + border_padding; i++) { + for (std::size_t i = 0; i <= longest_txt + border_padding; i++) { btm += HORIZONTAL_BAR; } btm += CORNER_BR; @@ -137,20 +142,20 @@ namespace rawterm { // Check if terminal size is reasonable and early return if not const Pos current_term_size = get_term_size(); - if (current_term_size.vertical < size.height()) { + if (current_term_size.vertical < int32_t(size.height())) { return; - } else if (current_term_size.horizontal < size.width()) { + } else if (current_term_size.horizontal < int32_t(size.width())) { return; } const std::vector render_lines = render(text); - for (int i = 0; i < render_lines.size(); i++) { + for (int i = 0; i < static_cast(render_lines.size()); i++) { cur.move({size.top_left.vertical + i, size.top_left.horizontal}); if (border_color.has_value()) { std::cout << "\x1b[" << border_color.value(); } - std::cout << render_lines.at(i) << std::flush; + std::cout << render_lines.at(uint32_t(i)) << std::flush; if (border_color.has_value()) { std::cout << Colors::reset; } diff --git a/rawterm/extras/menu.h b/rawterm/extras/menu.h index 22b776d..a175a44 100644 --- a/rawterm/extras/menu.h +++ b/rawterm/extras/menu.h @@ -2,6 +2,7 @@ #define RAWTERM_MENUS_H #include +#include #include #include #include @@ -15,7 +16,7 @@ namespace rawterm { std::vector opts = {}; public: - int active_opt = 0; + std::size_t active_opt = 0; Region dims; Menu(Region r) : dims(r) {}; @@ -50,14 +51,15 @@ namespace rawterm { std::string render() const override { std::string ret = ""; - const int longest_txt = std::max_element( - opts.begin(), opts.end(), - [](const std::string& a, const std::string& b) { - return a.length() < b.length(); - }) - ->size(); - - int view_size = std::min( + const int longest_txt = + static_cast(std::max_element( + opts.begin(), opts.end(), + [](const std::string& a, const std::string& b) { + return a.length() < b.length(); + }) + ->size()); + + const int view_size = std::min( static_cast(opts.size()), (dims.bottom_right.vertical - dims.top_left.vertical)); @@ -69,7 +71,7 @@ namespace rawterm { ret += "\u2510\r\n"; } - for (int i = 0; i < view_size; i++) { + for (std::size_t i = 0; i < uint32_t(view_size); i++) { // TODO: truncate to region if (i == active_opt) { ret += " " + inverse(opts.at(i)) + "\r\n"; diff --git a/rawterm/screen.cpp b/rawterm/screen.cpp index 27cf601..7fb1d37 100644 --- a/rawterm/screen.cpp +++ b/rawterm/screen.cpp @@ -81,12 +81,12 @@ namespace rawterm { return {intersection_top_left, intersection_bottom_right}; } - [[nodiscard]] int Region::width() const { - return bottom_right.horizontal - top_left.horizontal; + [[nodiscard]] std::size_t Region::width() const { + return std::size_t(bottom_right.horizontal - top_left.horizontal); } - [[nodiscard]] int Region::height() const { - return bottom_right.vertical - top_left.vertical; + [[nodiscard]] std::size_t Region::height() const { + return std::size_t(bottom_right.vertical - top_left.vertical); } } // namespace rawterm diff --git a/rawterm/screen.h b/rawterm/screen.h index 98038b1..89d9400 100644 --- a/rawterm/screen.h +++ b/rawterm/screen.h @@ -31,8 +31,8 @@ namespace rawterm { Region(const Pos& tl, const Pos& br) : top_left(tl), bottom_right(br) {} [[nodiscard]] bool contains(const Pos& cmp) const; [[nodiscard]] Region intersect(const Region& other); - [[nodiscard]] int width() const; - [[nodiscard]] int height() const; + [[nodiscard]] std::size_t width() const; + [[nodiscard]] std::size_t height() const; }; } // namespace rawterm From 6c7ab05bc7f90a7be5e066deb0180e89ddd077c9 Mon Sep 17 00:00:00 2001 From: Ttibsi Date: Mon, 10 Mar 2025 11:56:02 +0000 Subject: [PATCH 3/3] Add pragma to ignore pane.h warnings --- rawterm/extras/pane.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rawterm/extras/pane.h b/rawterm/extras/pane.h index be36225..e8ed765 100644 --- a/rawterm/extras/pane.h +++ b/rawterm/extras/pane.h @@ -1,3 +1,16 @@ +#ifndef RAWTERM_PANE_H +#define RAWTERM_PANE_H + +// This pragma block sets this header to be a system header, disabling any +// warnings from being printed when compiling +// I don't want to fix the new warnings on this file as I intend one day +// to come back and rewrite this as it's experimental +#ifdef __clang__ +#pragma clang system_header +#else +#pragma GCC system_header +#endif + #include #include #include @@ -552,3 +565,4 @@ namespace rawterm { }; // class PaneManager } // namespace rawterm +#endif // RAWTERM_PANE_H