Skip to content

Commit

Permalink
Add -Wold-style-cast to FTXUI_DEV_WARNINGS
Browse files Browse the repository at this point in the history
Add the -Wold-style-cast to the FTXUI_DEV_WARNINGS and fix the compiler
complaints about C casts
  • Loading branch information
StefanRvO committed Aug 16, 2023
1 parent acbdb50 commit b2f1bef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmake/ftxui_set_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function(ftxui_set_options library)
target_compile_options(${library} PRIVATE "-Wpedantic")
target_compile_options(${library} PRIVATE "-Wshadow")
target_compile_options(${library} PRIVATE "-Wunused")
target_compile_options(${library} PRIVATE "-Wold-style-cast")
endif()
endif()

Expand Down
33 changes: 18 additions & 15 deletions src/ftxui/component/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ class InputBase : public ComponentBase, public InputOption {
Elements elements;
const std::vector<std::string> lines = Split(*content);

cursor_position() = util::clamp(cursor_position(), 0, (int)content->size());
cursor_position() =
util::clamp(cursor_position(), 0, static_cast<int>(content->size()));

// Find the line and index of the cursor.
int cursor_line = 0;
int cursor_char_index = cursor_position();
for (const auto& line : lines) {
if (cursor_char_index <= (int)line.size()) {
if (cursor_char_index <= static_cast<int>(line.size())) {
break;
}

Expand All @@ -149,7 +150,7 @@ class InputBase : public ComponentBase, public InputOption {
}

// The cursor is at the end of the line.
if (cursor_char_index >= (int)line.size()) {
if (cursor_char_index >= static_cast<int>(line.size())) {
elements.push_back(hbox({
Text(line),
text(" ") | focused | reflect(cursor_box_),
Expand Down Expand Up @@ -207,7 +208,7 @@ class InputBase : public ComponentBase, public InputOption {
}

bool HandleDelete() {
if (cursor_position() == (int)content->size()) {
if (cursor_position() == static_cast<int>(content->size())) {
return false;
}
const size_t start = cursor_position();
Expand All @@ -226,7 +227,7 @@ class InputBase : public ComponentBase, public InputOption {
}

bool HandleArrowRight() {
if (cursor_position() == (int)content->size()) {
if (cursor_position() == static_cast<int>(content->size())) {
return false;
}

Expand All @@ -253,7 +254,7 @@ class InputBase : public ComponentBase, public InputOption {
// Move the cursor `columns` on the right, if possible.
void MoveCursorColumn(int columns) {
while (columns > 0) {
if (cursor_position() == (int)content().size() ||
if (cursor_position() == static_cast<int>(content().size()) ||
content()[cursor_position()] == '\n') {
return;
}
Expand Down Expand Up @@ -298,7 +299,7 @@ class InputBase : public ComponentBase, public InputOption {
}

bool HandleArrowDown() {
if (cursor_position() == (int)content->size()) {
if (cursor_position() == static_cast<int>(content->size())) {
return false;
}

Expand All @@ -310,7 +311,7 @@ class InputBase : public ComponentBase, public InputOption {
break;
}
cursor_position() = GlyphNext(content(), cursor_position());
if (cursor_position() == (int)content().size()) {
if (cursor_position() == static_cast<int>(content().size())) {
return true;
}
}
Expand Down Expand Up @@ -347,7 +348,8 @@ class InputBase : public ComponentBase, public InputOption {
}

bool OnEvent(Event event) override {
cursor_position() = util::clamp(cursor_position(), 0, (int)content->size());
cursor_position() =
util::clamp(cursor_position(), 0, static_cast<int>(content->size()));

if (event == Event::Return) {
return HandleReturn();
Expand Down Expand Up @@ -417,19 +419,19 @@ class InputBase : public ComponentBase, public InputOption {
}

bool HandleRightCtrl() {
if (cursor_position() == (int)content().size()) {
if (cursor_position() == static_cast<int>(content().size())) {
return false;
}

// Move right, until entering a word.
while (cursor_position() < (int)content().size()) {
while (cursor_position() < static_cast<int>(content().size())) {
cursor_position() = GlyphNext(content(), cursor_position());
if (IsWordCharacter(content(), cursor_position())) {
break;
}
}
// Move right, as long as right is a word character:
while (cursor_position() < (int)content().size()) {
while (cursor_position() < static_cast<int>(content().size())) {
const size_t next = GlyphNext(content(), cursor_position());
if (!IsWordCharacter(content(), cursor_position())) {
break;
Expand Down Expand Up @@ -465,7 +467,7 @@ class InputBase : public ComponentBase, public InputOption {
int cursor_line = 0;
int cursor_char_index = cursor_position();
for (const auto& line : lines) {
if (cursor_char_index <= (int)line.size()) {
if (cursor_char_index <= static_cast<int>(line.size())) {
break;
}

Expand All @@ -479,10 +481,11 @@ class InputBase : public ComponentBase, public InputOption {
int new_cursor_line = cursor_line + event.mouse().y - cursor_box_.y_min;

// Fix the new cursor position:
new_cursor_line = std::max(std::min(new_cursor_line, (int)lines.size()), 0);
new_cursor_line =
std::max(std::min(new_cursor_line, static_cast<int>(lines.size())), 0);

const std::string empty_string;
const std::string& line = new_cursor_line < (int)lines.size()
const std::string& line = new_cursor_line < static_cast<int>(lines.size())
? lines[new_cursor_line]
: empty_string;
new_cursor_column = util::clamp(new_cursor_column, 0, string_width(line));
Expand Down
6 changes: 3 additions & 3 deletions src/ftxui/dom/flexbox_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,23 +260,23 @@ void JustifyContent(Global& g, std::vector<Line> lines) {
}

case FlexboxConfig::JustifyContent::SpaceBetween: {
for (int i = (int)line.blocks.size() - 1; i >= 1; --i) {
for (int i = static_cast<int>(line.blocks.size()) - 1; i >= 1; --i) {
line.blocks[i]->x += remaining_space;
remaining_space = remaining_space * (i - 1) / i;
}
break;
}

case FlexboxConfig::JustifyContent::SpaceAround: {
for (int i = (int)line.blocks.size() - 1; i >= 0; --i) {
for (int i = static_cast<int>(line.blocks.size()) - 1; i >= 0; --i) {
line.blocks[i]->x += remaining_space * (2 * i + 1) / (2 * i + 2);
remaining_space = remaining_space * (2 * i) / (2 * i + 2);
}
break;
}

case FlexboxConfig::JustifyContent::SpaceEvenly: {
for (int i = (int)line.blocks.size() - 1; i >= 0; --i) {
for (int i = static_cast<int>(line.blocks.size()) - 1; i >= 0; --i) {
line.blocks[i]->x += remaining_space * (i + 1) / (i + 2);
remaining_space = remaining_space * (i + 1) / (i + 2);
}
Expand Down

0 comments on commit b2f1bef

Please sign in to comment.