Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/cells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions examples/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Sprite {
}
};

std::vector<Sprite> setupCoins(const int& lvl, const rawterm::Pos& term_size) {
std::vector<Sprite> 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
Expand All @@ -30,7 +30,7 @@ std::vector<Sprite> setupCoins(const int& lvl, const rawterm::Pos& term_size) {

std::vector<Sprite> 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));
}

Expand Down Expand Up @@ -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<int>(i));
score++;

// Check for next level
Expand Down
4 changes: 2 additions & 2 deletions examples/raw_escapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<unsigned int>(seq[i]);
ss << std::hex << "\\x" << static_cast<unsigned char>(seq[i]);
code += ss.str();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/vertical_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(vert_menu.active_opt) - 1,
vert_menu.dims.top_left.horizontal});
};

Expand Down
4 changes: 2 additions & 2 deletions rawterm/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
37 changes: 21 additions & 16 deletions rawterm/extras/border.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace rawterm {
struct Border {
Region size;
int border_padding = 0;
std::size_t border_padding = 0;

std::optional<char> border_char;
std::optional<std::string> border_title;
Expand All @@ -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;
}
Expand All @@ -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<std::size_t>(size.width() - 4)) +
"...";
}

[[nodiscard]] std::vector<std::string> render(const std::vector<std::string>* text) const {
auto trunc_title = truncated_title();
std::vector<std::string> 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<std::size_t>(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;
Expand All @@ -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<std::size_t>(longest_txt)) {
drawable_text = line.substr(0, static_cast<std::size_t>(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, ' ');

Expand All @@ -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;
Expand All @@ -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<std::string> render_lines = render(text);
for (int i = 0; i < render_lines.size(); i++) {
for (int i = 0; i < static_cast<int>(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;
}
Expand Down
22 changes: 12 additions & 10 deletions rawterm/extras/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RAWTERM_MENUS_H

#include <algorithm>
#include <cstdint>
#include <string>
#include <string_view>
#include <vector>
Expand All @@ -15,7 +16,7 @@ namespace rawterm {
std::vector<std::string> opts = {};

public:
int active_opt = 0;
std::size_t active_opt = 0;
Region dims;

Menu(Region r) : dims(r) {};
Expand Down Expand Up @@ -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<int>(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<int>(opts.size()),
(dims.bottom_right.vertical - dims.top_left.vertical));

Expand All @@ -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";
Expand Down
14 changes: 14 additions & 0 deletions rawterm/extras/pane.h
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <array>
#include <functional>
Expand Down Expand Up @@ -552,3 +565,4 @@ namespace rawterm {
}; // class PaneManager

} // namespace rawterm
#endif // RAWTERM_PANE_H
8 changes: 4 additions & 4 deletions rawterm/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions rawterm/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading