diff --git a/examples/dom/CMakeLists.txt b/examples/dom/CMakeLists.txt index 2936ec7..4654e95 100644 --- a/examples/dom/CMakeLists.txt +++ b/examples/dom/CMakeLists.txt @@ -13,6 +13,7 @@ example(gauge) example(inverted) example(package_manager) example(separator) -example(underlined) +example(spinner) example(style_gallery) +example(underlined) example(vbox_hbox) diff --git a/examples/dom/spinner.cpp b/examples/dom/spinner.cpp new file mode 100644 index 0000000..cc01487 --- /dev/null +++ b/examples/dom/spinner.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + +#include "ftxui/screen/screen.hpp" +#include "ftxui/dom/elements.hpp" +#include "ftxui/util/string.hpp" + +int main(int argc, const char *argv[]) +{ + using namespace ftxui; + using namespace ftxui::dom; + using namespace ftxui::screen; + using namespace std::chrono_literals; + + std::string reset_position; + for(int index = 0; index < 200; ++index) { + std::vector entries; + for(int i = 0; i<22; ++i) { + if (i != 0) + entries.push_back(separator()); + entries.push_back( + hbox( + text(to_wstring(i)) | size(5,1), + spinner(i, index) | bold + ) + ); + } + auto document = hbox(vbox(std::move(entries)) | frame, filler()); + auto screen = Screen::TerminalOutput(document); + Render(screen, document.get()); + std::cout << reset_position << screen.ToString() << std::flush; + reset_position = screen.ResetPosition(); + + std::this_thread::sleep_for(0.1s); + } + std::cout << std::endl; +} diff --git a/ftxui/CMakeLists.txt b/ftxui/CMakeLists.txt index e77020f..f72a844 100644 --- a/ftxui/CMakeLists.txt +++ b/ftxui/CMakeLists.txt @@ -38,6 +38,7 @@ add_library(dom src/ftxui/dom/node_decorator.cpp src/ftxui/dom/separator.cpp src/ftxui/dom/size.cpp + src/ftxui/dom/spinner.cpp src/ftxui/dom/text.cpp src/ftxui/dom/underlined.cpp src/ftxui/dom/util.cpp diff --git a/ftxui/include/ftxui/dom/elements.hpp b/ftxui/include/ftxui/dom/elements.hpp index 52eaac3..8c3b0b0 100644 --- a/ftxui/include/ftxui/dom/elements.hpp +++ b/ftxui/include/ftxui/dom/elements.hpp @@ -30,6 +30,7 @@ Element separator(); Element gauge(float ratio); Element frame(Element); Element window(Child title, Child content); +Element spinner(int charset_index, size_t image_index); // -- Decorator --- Element bold(Element); @@ -37,10 +38,11 @@ Element dim(Element); Element inverted(Element); Element underlined(Element); Element blink(Element); -Element color(Color, Element); -Element bgcolor(Color, Element); + Decorator color(Color); Decorator bgcolor(Color); +Element color(Color, Element); +Element bgcolor(Color, Element); // --- Util --- Element hcenter(Element); diff --git a/ftxui/src/ftxui/dom/spinner.cpp b/ftxui/src/ftxui/dom/spinner.cpp new file mode 100644 index 0000000..f4e1679 --- /dev/null +++ b/ftxui/src/ftxui/dom/spinner.cpp @@ -0,0 +1,279 @@ +#include "ftxui/dom/node.hpp" +#include "ftxui/dom/elements.hpp" + +namespace ftxui::dom { + +using namespace ftxui::screen; + +static const std::vector>> elements = { + { + {L"Replaced by the gauge"}, + }, + { + {L". "}, + {L".. "}, + {L"..."}, + }, + { + {L"|"}, + {L"/"}, + {L"-"}, + {L"\\"}, + }, + { + {L"+"}, + {L"x"}, + }, + { + {L"| "}, + {L"|| "}, + {L"|||"}, + }, + { + {L"←"}, + {L"↖"}, + {L"↑"}, + {L"↗"}, + {L"→"}, + {L"↘"}, + {L"↓"}, + {L"↙"}, + }, + { + {L"▁"}, + {L"▂"}, + {L"▃"}, + {L"▄"}, + {L"▅"}, + {L"▆"}, + {L"▇"}, + {L"█"}, + {L"▇"}, + {L"▆"}, + {L"▅"}, + {L"▄"}, + {L"▃"}, + {L"▁"}, + }, + { + {L"▉"}, + {L"▊"}, + {L"▋"}, + {L"▌"}, + {L"▍"}, + {L"▎"}, + {L"▏"}, + {L"▎"}, + {L"▍"}, + {L"▌"}, + {L"▋"}, + {L"▊"}, + }, + { + {L"▖"}, + {L"▘"}, + {L"▝"}, + {L"▗"}, + }, + { + {L"◢"}, + {L"◣"}, + {L"◤"}, + {L"◥"}, + }, + { + {L"◰"}, + {L"◳"}, + {L"◲"}, + {L"◱"}, + }, + { + {L"◴"}, + {L"◷"}, + {L"◶"}, + {L"◵"}, + }, + { + {L"◐"}, + {L"◓"}, + {L"◑"}, + {L"◒"}, + }, + { + {L"◡"}, + {L"⊙"}, + {L"◠"}, + }, + { + {L"⠁"}, + {L"⠂"}, + {L"⠄"}, + {L"⡀"}, + {L"⢀"}, + {L"⠠"}, + {L"⠐"}, + {L"⠈"}, + }, + { + {L"⠋"}, + {L"⠙"}, + {L"⠹"}, + {L"⠸"}, + {L"⠼"}, + {L"⠴"}, + {L"⠦"}, + {L"⠧"}, + {L"⠇"}, + {L"⠏"}, + }, + { + {L"(*----------)"}, + {L"(-*---------)"}, + {L"(--*--------)"}, + {L"(---*-------)"}, + {L"(----*------)"}, + {L"(-----*-----)"}, + {L"(------*----)"}, + {L"(-------*---)"}, + {L"(--------*--)"}, + {L"(---------*-)"}, + {L"(----------*)"}, + {L"(---------*-)"}, + {L"(--------*--)"}, + {L"(-------*---)"}, + {L"(------*----)"}, + {L"(-----*-----)"}, + {L"(----*------)"}, + {L"(---*-------)"}, + {L"(--*--------)"}, + {L"(-*---------)"}, + }, + { + {L"[ ]"}, + {L"[= ]"}, + {L"[== ]"}, + {L"[=== ]"}, + {L"[==== ]"}, + {L"[===== ]"}, + {L"[======]"}, + {L"[===== ]"}, + {L"[==== ]"}, + {L"[=== ]"}, + {L"[== ]"}, + {L"[= ]"}, + }, + { + {L"[ ]"}, + {L"[= ]"}, + {L"[== ]"}, + {L"[=== ]"}, + {L"[==== ]"}, + {L"[===== ]"}, + {L"[======]"}, + {L"[ =====]"}, + {L"[ ====]"}, + {L"[ ===]"}, + {L"[ ==]"}, + {L"[ =]"}, + }, + { + {L"[== ]"}, + {L"[== ]"}, + {L"[== ]"}, + {L"[== ]"}, + {L"[== ]"}, + {L" [== ]"}, + {L"[ == ]"}, + {L"[ == ]"}, + {L"[ ==]"}, + {L"[ ==]"}, + {L"[ ==]"}, + {L"[ ==]"}, + {L"[ ==]"}, + {L"[ ==] "}, + {L"[ == ]"}, + {L"[ == ]"}, + }, + { + { + L" ─╮", + L" │", + L" ", + }, + { + L" ╮", + L" │", + L" ╯", + }, + { + L" ", + L" │", + L" ─╯", + }, + { + L" ", + L" ", + L"╰─╯", + }, + { + L" ", + L"│ ", + L"╰─ ", + }, + { + L"╭ ", + L"│ ", + L"╰ ", + }, + { + L"╭─ ", + L"│ ", + L" ", + }, + { + L"╭─╮", + L" ", + L" ", + } + }, + { + { + L" /\\O ", + L" /\\/ ", + L" /\\ ", + L" / \\ ", + L"LOL LOL", + }, + { + L" _O ", + L" //|_ ", + L" | ", + L" /| ", + L" LLOL ", + }, + { + L" O ", + L" /_ ", + L" |\\ ", + L" / | ", + L" LOLLOL ", + } + } +}; + +std::unique_ptr spinner(int c, size_t index) { + if (c == 0) { + index %= 40; + if (index > 20) + index = 40-index; + return gauge(index * 0.05); + } + c %= elements.size(); + index %= elements[c].size(); + std::vector lines; + for(const auto& it : elements[c][index]) + lines.push_back(text(it)); + return vbox(std::move(lines)); +} + +}; // namespace ftxui::dom