From c76612a3c8a2db2bc94cb5b84d4ceda5b2bf3387 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Sun, 13 Mar 2022 22:30:05 +0100 Subject: [PATCH] Fix error with invalid float in the gauge component. (#356) --- src/ftxui/component/component_fuzzer.cpp | 63 +++++++++++++++++++++++- src/ftxui/dom/gauge.cpp | 10 +++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/ftxui/component/component_fuzzer.cpp b/src/ftxui/component/component_fuzzer.cpp index 6611527..ec34dec 100644 --- a/src/ftxui/component/component_fuzzer.cpp +++ b/src/ftxui/component/component_fuzzer.cpp @@ -44,6 +44,63 @@ int GeneratorInt(const char* data, size_t size) { return out; } +Color GeneratorColor(const char* data, size_t size) { + return Color::RGB(GeneratorInt(data, size), GeneratorInt(data, size), + GeneratorInt(data, size)); +} + +AnimatedColorOption GeneratorAnimatedColorOption(const char* data, + size_t size) { + AnimatedColorOption option; + option.enabled = GeneratorBool(data, size); + option.inactive = GeneratorColor(data, size); + option.active = GeneratorColor(data, size); + option.duration = std::chrono::milliseconds(GeneratorInt(data, size)); + return option; +} + +AnimatedColorsOption GeneratorAnimatedColorsOptions(const char* data, + size_t size) { + AnimatedColorsOption option; + option.background = GeneratorAnimatedColorOption(data, size); + option.foreground = GeneratorAnimatedColorOption(data, size); + return option; +} + +ButtonOption GeneratorButtonOption(const char* data, size_t size) { + ButtonOption option; + option.animated_colors = GeneratorAnimatedColorsOptions(data, size); + return option; +} + +UnderlineOption GeneratorUnderlineOption(const char* data, size_t size) { + UnderlineOption option; + option.enabled = GeneratorBool(data, size); + option.color_active = GeneratorColor(data, size); + option.color_inactive = GeneratorColor(data, size); + option.leader_duration = std::chrono::milliseconds(GeneratorInt(data, size)); + option.follower_duration = + std::chrono::milliseconds(GeneratorInt(data, size)); + option.leader_delay = std::chrono::milliseconds(GeneratorInt(data, size)); + option.follower_delay = std::chrono::milliseconds(GeneratorInt(data, size)); + return option; +} + +MenuEntryOption GeneratorMenuEntryOption(const char* data, size_t size) { + MenuEntryOption option; + option.animated_colors = GeneratorAnimatedColorsOptions(data, size); + return option; +} + +MenuOption GeneratorMenuOption(const char* data, size_t size) { + MenuOption option; + option.underline = GeneratorUnderlineOption(data, size); + option.entries = GeneratorMenuEntryOption(data, size); + option.direction = + static_cast(GeneratorInt(data, size) % 4); + return option; +} + bool g_bool; int g_int; std::vector g_list; @@ -60,13 +117,15 @@ Component GeneratorComponent(const char*& data, size_t& size, int depth) { value = (value % value_max + value_max) % value_max; switch (value) { case 0: - return Button(GeneratorString(data, size), [] {}); + return Button( + GeneratorString(data, size), [] {}, + GeneratorButtonOption(data, size)); case 1: return Checkbox(GeneratorString(data, size), &g_bool); case 2: return Input(GeneratorString(data, size), GeneratorString(data, size)); case 3: - return Menu(&g_list, &g_int); + return Menu(&g_list, &g_int, GeneratorMenuOption(data, size)); case 4: return Radiobox(&g_list, &g_int); case 5: diff --git a/src/ftxui/dom/gauge.cpp b/src/ftxui/dom/gauge.cpp index b2fb6f0..d00ab03 100644 --- a/src/ftxui/dom/gauge.cpp +++ b/src/ftxui/dom/gauge.cpp @@ -1,3 +1,4 @@ +#include #include // for max, min #include // for allocator, make_shared #include // for string @@ -40,8 +41,13 @@ static std::string charset_vertical[10] = { class Gauge : public Node { public: Gauge(float progress, GaugeDirection direction) - : progress_(std::min(std::max(progress, 0.f), 1.f)), - direction_(direction) {} + : progress_(progress), direction_(direction) { + // This handle NAN correctly: + if (!(progress_ > 0.f)) + progress_ = 0.f; + if (!(progress_ < 1.f)) + progress_ = 1.f; + } void ComputeRequirement() override { switch (direction_) {